From 4760f3b751de27cc53479692771e9014b491df24 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Tue, 13 Aug 2024 16:34:16 +0100 Subject: [PATCH] encoding/jsonschema: fix panic on impossible enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When all possible values of an enum are excluded by the allowed types we were invoking s.all.add with a nil expression which resulted in a panic. Fix that by avoiding adding the constraint in that case: the allowedTypes logic will catch the "impossible constraint" option when finalize is called. Signed-off-by: Roger Peppe Change-Id: I0836d0444a24df62f6392261a5d5733387529ff6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199410 Reviewed-by: Daniel Martí Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo --- encoding/jsonschema/constraints.go | 4 +++- encoding/jsonschema/testdata/enumexcluded.txtar | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 encoding/jsonschema/testdata/enumexcluded.txtar diff --git a/encoding/jsonschema/constraints.go b/encoding/jsonschema/constraints.go index f7fe70c4c6c..dd91e5ee0b0 100644 --- a/encoding/jsonschema/constraints.go +++ b/encoding/jsonschema/constraints.go @@ -204,7 +204,9 @@ var constraints = []*constraint{ } s.knownTypes &= types s.allowedTypes &= types - s.all.add(n, ast.NewBinExpr(token.OR, a...)) + if len(a) > 0 { + s.all.add(n, ast.NewBinExpr(token.OR, a...)) + } }), // TODO: only allow for OpenAPI. diff --git a/encoding/jsonschema/testdata/enumexcluded.txtar b/encoding/jsonschema/testdata/enumexcluded.txtar new file mode 100644 index 00000000000..8826045c127 --- /dev/null +++ b/encoding/jsonschema/testdata/enumexcluded.txtar @@ -0,0 +1,8 @@ +-- type.json -- +{ + "type": "object", + "enum": ["x"] +} +-- out/decode/err -- +constraints are not possible to satisfy: + type.json:1:1