Skip to content

Commit 0ad2119

Browse files
haoqixurogpeppe
authored andcommitted
encoding/jsonschema: close structs and mark fields as required in enum and const
The validation of the keywords `enum` and `const` should succeed only when the values of structs are exactly the same. This closes structs and marks the fields as required. Fixes #3561 Change-Id: Ib27dc1edd52e13ef1d4153bec3ccfd0af6e7808b Signed-off-by: haoqixu <hq.xu0o0@gmail.com> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1204477 Reviewed-by: Roger Peppe <rogpeppe@gmail.com> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
1 parent 407cccd commit 0ad2119

File tree

19 files changed

+64
-74
lines changed

19 files changed

+64
-74
lines changed

encoding/jsonschema/constraints_generic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func constraintComment(key string, n cue.Value, s *state) {
6161
}
6262

6363
func constraintConst(key string, n cue.Value, s *state) {
64-
s.all.add(n, s.value(n))
64+
s.all.add(n, s.constValue(n))
6565
s.allowedTypes &= n.Kind()
6666
s.knownTypes &= n.Kind()
6767
}
@@ -93,7 +93,7 @@ func constraintEnum(key string, n cue.Value, s *state) {
9393
// not in the allowed type set.
9494
continue
9595
}
96-
a = append(a, s.value(x))
96+
a = append(a, s.constValue(x))
9797
types |= x.Kind()
9898
}
9999
s.knownTypes &= types

encoding/jsonschema/decode.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,34 @@ func (s0 *state) schemaState(n cue.Value, types cue.Kind, idRef []label) (ast.Ex
801801
return s.finalize(), s
802802
}
803803

804+
func (s *state) constValue(n cue.Value) ast.Expr {
805+
k := n.Kind()
806+
switch k {
807+
case cue.ListKind:
808+
a := []ast.Expr{}
809+
for i, _ := n.List(); i.Next(); {
810+
a = append(a, s.constValue(i.Value()))
811+
}
812+
return setPos(ast.NewList(a...), n)
813+
814+
case cue.StructKind:
815+
a := []ast.Decl{}
816+
s.processMap(n, func(key string, n cue.Value) {
817+
a = append(a, &ast.Field{
818+
Label: ast.NewString(key),
819+
Value: s.constValue(n),
820+
Constraint: token.NOT,
821+
})
822+
})
823+
return setPos(ast.NewCall(ast.NewIdent("close"), &ast.StructLit{Elts: a}), n)
824+
default:
825+
if !n.IsConcrete() {
826+
s.errf(n, "invalid non-concrete value")
827+
}
828+
return n.Syntax(cue.Final()).(ast.Expr)
829+
}
830+
}
831+
804832
func (s *state) value(n cue.Value) ast.Expr {
805833
k := n.Kind()
806834
switch k {

encoding/jsonschema/external_teststats.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Generated by CUE_UPDATE=1 go test. DO NOT EDIT
22
v2:
33
schema extract (pass / total): 1013 / 1363 = 74.3%
4-
tests (pass / total): 3646 / 4803 = 75.9%
5-
tests on extracted schemas (pass / total): 3646 / 3865 = 94.3%
4+
tests (pass / total): 3659 / 4803 = 76.2%
5+
tests on extracted schemas (pass / total): 3659 / 3865 = 94.7%
66

77
v3:
88
schema extract (pass / total): 1013 / 1363 = 74.3%
9-
tests (pass / total): 3636 / 4803 = 75.7%
10-
tests on extracted schemas (pass / total): 3636 / 3865 = 94.1%
9+
tests (pass / total): 3649 / 4803 = 76.0%
10+
tests on extracted schemas (pass / total): 3649 / 3865 = 94.4%
1111

1212
Optional tests
1313

encoding/jsonschema/testdata/external/tests/draft2019-09/const.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@
5454
"data": {
5555
"foo": "bar"
5656
},
57-
"valid": false,
58-
"skip": {
59-
"v2": "unexpected success",
60-
"v3": "unexpected success"
61-
}
57+
"valid": false
6258
},
6359
{
6460
"description": "another type is invalid",

encoding/jsonschema/testdata/external/tests/draft2019-09/enum.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@
6767
"foo": 12,
6868
"boo": 42
6969
},
70-
"valid": false,
71-
"skip": {
72-
"v2": "unexpected success",
73-
"v3": "unexpected success"
74-
}
70+
"valid": false
7571
}
7672
]
7773
},

encoding/jsonschema/testdata/external/tests/draft2019-09/ref.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,7 @@
665665
"data": {
666666
"type": "string"
667667
},
668-
"valid": false,
669-
"skip": {
670-
"v2": "unexpected success",
671-
"v3": "unexpected success"
672-
}
668+
"valid": false
673669
},
674670
{
675671
"description": "match the enum exactly",

encoding/jsonschema/testdata/external/tests/draft2020-12/const.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@
5454
"data": {
5555
"foo": "bar"
5656
},
57-
"valid": false,
58-
"skip": {
59-
"v2": "unexpected success",
60-
"v3": "unexpected success"
61-
}
57+
"valid": false
6258
},
6359
{
6460
"description": "another type is invalid",

encoding/jsonschema/testdata/external/tests/draft2020-12/enum.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@
6767
"foo": 12,
6868
"boo": 42
6969
},
70-
"valid": false,
71-
"skip": {
72-
"v2": "unexpected success",
73-
"v3": "unexpected success"
74-
}
70+
"valid": false
7571
}
7672
]
7773
},

encoding/jsonschema/testdata/external/tests/draft2020-12/ref.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,7 @@
665665
"data": {
666666
"type": "string"
667667
},
668-
"valid": false,
669-
"skip": {
670-
"v2": "unexpected success",
671-
"v3": "unexpected success"
672-
}
668+
"valid": false
673669
},
674670
{
675671
"description": "match the enum exactly",

encoding/jsonschema/testdata/external/tests/draft4/enum.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@
6565
"foo": 12,
6666
"boo": 42
6767
},
68-
"valid": false,
69-
"skip": {
70-
"v2": "unexpected success",
71-
"v3": "unexpected success"
72-
}
68+
"valid": false
7369
}
7470
]
7571
},

0 commit comments

Comments
 (0)