Skip to content

encoding/jsonschema: panic when importing an empty object #2287

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

Closed
jpluscplusm opened this issue Mar 2, 2023 · 2 comments
Closed

encoding/jsonschema: panic when importing an empty object #2287

jpluscplusm opened this issue Mar 2, 2023 · 2 comments

Comments

@jpluscplusm
Copy link
Collaborator

jpluscplusm commented Mar 2, 2023

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

cue version v0.5.0-beta.5

go version go1.19.3
       -compiler gc
       -trimpath true
     CGO_ENABLED 0
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Does this issue reproduce with the latest stable release?

Yes (0.4.3)

What did you do?

I imported a JSONSchema file that contains a schema constraining an object to be empty. CUE panicked:

exec cue import jsonschema: schema.json
-- schema.json --
{
    "description": "an empty object",
    "type": "object",
    "properties": {},
    "additionalProperties": false
}

What did you expect to see?

I expected to see some CUE output that declared and/or constrained the field to be {}. Ideally with a close() around it, due to the additionalProperties: false, but I'm aware that question is tracked in #750.

Removing the additionalProperties: false field stops the panic, but results in an output which is literally just ..., which IMHO is precisely the /opposite/ of the output that I believe the JSONSchema is representing.

What did you see instead?

> exec cue import jsonschema: schema.json
[stderr]
panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
cuelang.org/go/cmd/cue/cmd.recoverError(0xc00030fe90)
	cuelang.org/[email protected]/cmd/cue/cmd/root.go:261 +0x7b
panic({0xbf2e00, 0xc00003e258})
	runtime/panic.go:884 +0x212
cuelang.org/go/encoding/jsonschema.(*decoder).schema(0xc0000f1a40, {0x0, 0x0, 0x698900?}, {0xc00034e690?, 0xc0000f8ab0?, 0x0?})
	cuelang.org/[email protected]/encoding/jsonschema/decode.go:153 +0xedb
cuelang.org/go/encoding/jsonschema.(*decoder).decode(0xc0000f1a40, {0xc00034e690?, 0xc0000f8ab0?, 0x0?})
	cuelang.org/[email protected]/encoding/jsonschema/decode.go:73 +0x23f
cuelang.org/go/encoding/jsonschema.Extract({0xd74718, 0xc000395110}, 0xc0000f57c0)
	cuelang.org/[email protected]/encoding/jsonschema/jsonschema.go:47 +0x9e
cuelang.org/go/internal/encoding.jsonSchemaFunc.func1(0xc00034e690?)
	cuelang.org/[email protected]/internal/encoding/encoding.go:301 +0x199
cuelang.org/go/internal/encoding.(*Decoder).doInterpret(0xc0000f8990)
	cuelang.org/[email protected]/internal/encoding/encoding.go:114 +0x1ac
cuelang.org/go/internal/encoding.(*Decoder).Next(0xc0000f8990)
	cuelang.org/[email protected]/internal/encoding/encoding.go:93 +0xc9
cuelang.org/go/internal/encoding.NewDecoder(0xc000394d90, 0xde0000c000012010?)
	cuelang.org/[email protected]/internal/encoding/encoding.go:246 +0xda5
cuelang.org/go/cmd/cue/cmd.(*buildPlan).getDecoders(0xc0002e05b0, 0xc00024c1e0)
	cuelang.org/[email protected]/cmd/cue/cmd/common.go:476 +0x585
cuelang.org/go/cmd/cue/cmd.parseArgs(0xc0002b6ff0, {0xc0002b3340, 0x2, 0x2}, 0xc0002c2c30)
	cuelang.org/[email protected]/cmd/cue/cmd/common.go:566 +0x69c
cuelang.org/go/cmd/cue/cmd.runImport(0xc0002b6ff0?, {0xc0002b3340, 0x2, 0x2})
	cuelang.org/[email protected]/cmd/cue/cmd/import.go:315 +0x96a
cuelang.org/go/cmd/cue/cmd.mkRunE.func1(0xc0002da780?, {0xc0002b3340, 0x2, 0x2})
	cuelang.org/[email protected]/cmd/cue/cmd/root.go:72 +0xb3
github.com/spf13/cobra.(*Command).execute(0xc0002da780, {0xc0002b32e0, 0x2, 0x2})
	github.com/spf13/[email protected]/command.go:856 +0x67c
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002c6c80)
	github.com/spf13/[email protected]/command.go:974 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/[email protected]/command.go:902
cuelang.org/go/cmd/cue/cmd.(*Command).Run(0xc0002b6ff0, {0x3?, 0x3?})
	cuelang.org/[email protected]/cmd/cue/cmd/root.go:246 +0x6b
cuelang.org/go/cmd/cue/cmd.mainErr({0xd79070, 0xc00003a090}, {0xc000034090?, 0x60?, 0x11ee380?})
	cuelang.org/[email protected]/cmd/cue/cmd/root.go:183 +0x55
cuelang.org/go/cmd/cue/cmd.Main()
	cuelang.org/[email protected]/cmd/cue/cmd/root.go:165 +0x7c
main.main()
	cuelang.org/[email protected]/cmd/cue/main.go:24 +0x19
[exit status 2]
FAIL: /tmp/testscript609928878/panic.txtar/script.txtar:1: unexpected command failure
error running panic.txtar in /tmp/testscript609928878/panic.txtar
@jpluscplusm jpluscplusm added NeedsInvestigation Triage Requires triage/attention labels Mar 2, 2023
@myitcv myitcv changed the title jsonschema: panic when importing an empty object encoding/jsonschema: panic when importing an empty object Apr 6, 2023
@myitcv myitcv removed the Triage Requires triage/attention label Apr 6, 2023
@myitcv
Copy link
Member

myitcv commented Apr 6, 2023

Thanks for the report, @jpluscplusm

@myitcv myitcv added the zGarden label Jun 13, 2023
@myitcv myitcv removed the zGarden label Jun 13, 2023
@myitcv myitcv added this to the v0.6.x milestone Jun 13, 2023
@mvdan
Copy link
Member

mvdan commented Aug 14, 2024

cc @rogpeppe

cueckoo pushed a commit that referenced this issue Aug 19, 2024
Issue #3354 demonstrates that aliases are used when they're
not actually needed. When investigating the fix for #2287, I
realised where the problem might be, and this is the result.

The problem was that all the self-references need to reference
the same AST node, but they were not doing so. Fix that by
creating the struct node to be embedded when we know that
we need a self-reference. We can also use the presence of
that node to signal that a self-reference is needed, removing the
need for `hasSelfReference`.

Fixes #3354

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ie886b5819c612cbd64abca62d3231aedd530e2bf
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199626
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants