Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/openapi: regression vs evalv2 where additionalProperties and defaults are no longer produced #3722

Open
myitcv opened this issue Feb 3, 2025 · 1 comment
Labels
evalv3 issues affecting only the evaluator version 3 jsonschema / openapi NeedsInvestigation

Comments

@myitcv
Copy link
Member

myitcv commented Feb 3, 2025

What version of CUE are you using (cue version)?

$ cue version
cue version v0.0.0-20250203094823-0265d7d62829

go version go1.23.5
      -buildmode exe
       -compiler gc
  DefaultGODEBUG asynctimerchan=1,gotypesalias=0,httpservecontentkeepheaders=1,tls3des=1,tlskyber=0,x509keypairleaf=0,x509negativeserial=1
     CGO_ENABLED 1
          GOARCH arm64
            GOOS linux
         GOARM64 v8.0
             vcs git
    vcs.revision 0265d7d62829c1002e3ea58dd6c5b992fea5f2e3
        vcs.time 2025-02-03T09:48:23Z
    vcs.modified false
cue.lang.version v0.13.0

Does this issue reproduce with the latest release?

Yes

What did you do?

go mod edit -replace=cuelang.org/go=/home/myitcv/dev/cuelang/cue
go mod tidy

# -- evalv2 --
env CUE_EXPERIMENT=evalv3=0

# Go API
exec go mod tidy
exec go run main.go
cmp stdout stdout.golden

# -- evalv3 --
env CUE_EXPERIMENT=evalv3=1

# Go API
exec go mod tidy
exec go run main.go
cmp stdout stdout.golden

-- go.mod --
module mod.example
-- deps.go --
package deps
import _ "cuelang.org/go/cmd/cue"
-- types.cue --
package openapi

#Tags: {
    owner: string | *"sre"
    role: string | *"control-plane"
    ...
}

#Spec: {
    name: string
    tags?: #Tags | *#Tags
}

-- go.mod --
module example.com/openapi

go 1.23

require cuelang.org/go v0.12.0

-- main.go --
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"encoding/json"

	"cuelang.org/go/cue/cuecontext"
	"cuelang.org/go/encoding/openapi"
)

func run() error {
	runtime := cuecontext.New()

	contents, err := os.ReadFile("types.cue")
	if err != nil {
	    return err
	}

	val := runtime.CompileString(string(contents))
	if val.Err() != nil {
		return val.Err()
	}

	b, err := openapi.Gen(val, &openapi.Config{
		Info: map[string]any{
			"title":       "XRD schemas",
			"description": "general",
			"version":     "0.1.0",
		},
		ExpandReferences: true,
	})
	if err != nil {
		return err
	}
	var res bytes.Buffer
	err = json.Indent(&res, b, "", "  ")
	if err != nil {
		return err
	}
	fmt.Println(res.String())
	return nil
}

func main() {
	if err := run(); err != nil {
		log.Fatalln(err)
	}
}
-- stdout.golden --
{
  "openapi": "3.0.0",
  "info": {
    "description": "general",
    "title": "XRD schemas",
    "version": "0.1.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "Spec": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "tags": {
            "type": "object",
            "required": [
              "owner",
              "role"
            ],
            "properties": {
              "owner": {
                "type": "string",
                "default": "sre"
              },
              "role": {
                "type": "string",
                "default": "control-plane"
              }
            },
            "additionalProperties": {}
          }
        }
      },
      "Tags": {
        "type": "object",
        "required": [
          "owner",
          "role"
        ],
        "properties": {
          "owner": {
            "type": "string",
            "default": "sre"
          },
          "role": {
            "type": "string",
            "default": "control-plane"
          }
        },
        "additionalProperties": {}
      }
    }
  }
}

What did you expect to see?

Passing test.

What did you see instead?

# -- evalv2 -- (0.000s)
# Go API (0.319s)
# -- evalv3 -- (0.000s)
# Go API (0.305s)
> exec go mod tidy
> exec go run main.go
[stdout]
{
  "openapi": "3.0.0",
  "info": {
    "description": "general",
    "title": "XRD schemas",
    "version": "0.1.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "Spec": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "tags": {
            "type": "object",
            "required": [
              "owner",
              "role"
            ],
            "properties": {
              "owner": {
                "type": "string",
                "default": "sre"
              },
              "role": {
                "type": "string",
                "default": "control-plane"
              }
            },
            "default": {
              "owner": "sre",
              "role": "control-plane"
            }
          }
        }
      },
      "Tags": {
        "type": "object",
        "required": [
          "owner",
          "role"
        ],
        "properties": {
          "owner": {
            "type": "string",
            "default": "sre"
          },
          "role": {
            "type": "string",
            "default": "control-plane"
          }
        }
      }
    }
  }
}
> cmp stdout stdout.golden
diff stdout stdout.golden
--- stdout
+++ stdout.golden
@@ -33,10 +33,7 @@
                 "default": "control-plane"
               }
             },
-            "default": {
-              "owner": "sre",
-              "role": "control-plane"
-            }
+            "additionalProperties": {}
           }
         }
       },
@@ -55,7 +52,8 @@
             "type": "string",
             "default": "control-plane"
           }
-        }
+        },
+        "additionalProperties": {}
       }
     }
   }

FAIL: /tmp/testscript515885163/repro.txtar/script.txtar:18: stdout and stdout.golden differ
error running repro.txtar in /tmp/testscript515885163/repro.txtar

It's highly likely that the missing additionalProperites is related to #3720.

The missing defaults could well be a function of the work that @rogpeppe did regarding defaults and JSON Schema.

@rogpeppe
Copy link
Member

rogpeppe commented Feb 4, 2025

The missing defaults could well be a function of the work that @rogpeppe did regarding defaults and JSON Schema.

At first glance that seems unlikely, as the code above is generating OpenAPI from CUE but encoding/jsonschema is only concerned with generating CUE from JSON Schema/OpenAPI, at least for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
evalv3 issues affecting only the evaluator version 3 jsonschema / openapi NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

2 participants