cue icon indicating copy to clipboard operation
cue copied to clipboard

encoding/jsonschema: constraint `type: "number"` used with an enum of integers produces error on import

Open folliehiyuki opened this issue 1 year ago • 1 comments

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

$ cue version
cue version v0.8.2

go version go1.22.3
      -buildmode exe
       -compiler gc
        -ldflags -s -w
  DefaultGODEBUG httplaxcontentlength=1,httpmuxgo121=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Does this issue reproduce with the latest stable release?

Yes

What did you do?

Given this simple jsonschema, I tried to convert it to CUE with the command: cue import -f -l "#Test:" -o test.cue test.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "test": {
      "type": "object",
      "properties": {
        "version": {
          "type": "number",
          "enum": [3, 4]
        }
      }
    }
  }
}

What did you expect to see?

#Test: {
	@jsonschema(schema="http://json-schema.org/draft-04/schema#")
	_

	#test: {
		version?: (3 | 4) & number
		...
	}
}

The constraint of version field is valid, since number type in CUE is the superset of int.

What did you see instead?

constraint not allowed because type number is excluded:
    ./test.json:8:11

NOTE: the command will succeed if I either change the type field to integer or use a float number inside enum (.e.g [3, 4.1])

folliehiyuki avatar May 23 '24 17:05 folliehiyuki

Confirmed, this indeed looks like an encoding/jsonschema related bug.

The following fails when it should succeed:

# integer
exec cue import jsonschema: integer.json
cmp integer.cue integer.cue.golden

# number
exec cue import jsonschema: number.json
cmp number.cue number.cue.golden

-- integer.json --
{
	"enum": [3, 4],
	"type": "integer"
}
-- number.json --
{
	"enum": [3, 4],
	"type": "number"
}
-- integer.cue.golden --

(3 | 4) & int
-- number.cue.golden --

(3 | 4) & number

Output is:

# integer (0.013s)
# number (0.011s)
> exec cue import jsonschema: number.json
[stderr]
constraint not allowed because type number is excluded:
    ./number.json:3:2
[exit status 1]
FAIL: /tmp/testscript2283897366/repro.txtar/script.txtar:6: unexpected command failure

myitcv avatar May 27 '24 05:05 myitcv

Here's another example directly derived from an example found in the wild, which seems related:

exec cue def schema.json

-- schema.json --
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": [
    "string",
    "null"
  ],
  "enum": [
    "a",
    "b"
  ],
  "description": "example"
}

Output:

> exec cue def schema.json
[stderr]
constraint not allowed because type null is excluded:
    ./schema.json:5:5
[exit status 1]
FAIL: /tmp/testscript3851595353/y.txtar/script.txtar:1: unexpected command failure

rogpeppe avatar Jul 24 '24 11:07 rogpeppe