Skip to content

Commit

Permalink
encoding/jsonschema: fix panic on impossible enum
Browse files Browse the repository at this point in the history
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 <[email protected]>
Change-Id: I0836d0444a24df62f6392261a5d5733387529ff6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199410
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent 6694c55 commit 4760f3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions encoding/jsonschema/testdata/enumexcluded.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- type.json --
{
"type": "object",
"enum": ["x"]
}
-- out/decode/err --
constraints are not possible to satisfy:
type.json:1:1

0 comments on commit 4760f3b

Please sign in to comment.