Skip to content

Commit c4697bd

Browse files
haoqixumvdan
authored andcommitted
encoding/jsonschema: decode required properties as required fields
Decode a jsonschema required property as a CUE required field instead of a regular field. Fixes #3318 Change-Id: Idc856b888e682cd0ea9124277371681d0a83898c Signed-off-by: haoqixu <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198831 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 252a666 commit c4697bd

File tree

7 files changed

+57
-25
lines changed

7 files changed

+57
-25
lines changed

cmd/cue/cmd/testdata/script/def_openapi.txtar

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ info: {
218218
version: *"v1alpha1" | string
219219
}
220220
#Bar: {
221-
foo: #Foo
221+
foo!: #Foo
222222
...
223223
}
224224
#Foo: {
225-
a: int
226-
b: uint & <10
225+
a!: int
226+
b!: uint & <10
227227
...
228228
}
229229
-- expect-cue2 --
@@ -232,12 +232,12 @@ info: {
232232
version: *"v1" | string
233233
}
234234
#Bar: {
235-
foo: #Foo
235+
foo!: #Foo
236236
...
237237
}
238238
#Foo: {
239-
a: int
240-
b: uint & <10
239+
a!: int
240+
b!: uint & <10
241241
...
242242
}
243243
-- expect-cue3 --
@@ -251,17 +251,17 @@ info: {
251251
version: (*"v1alpha1" | string) & (*"v1alpha1" | string)
252252
}
253253
#Bar: {
254-
foo: #Foo
254+
foo!: #Foo
255255
...
256256
}
257257
#Foo: {
258-
a: int
259-
b: uint & <10
258+
a!: int
259+
b!: uint & <10
260260
...
261261
}
262262
#Baz: {
263-
a: int
264-
b: uint & <10
263+
a!: int
264+
b!: uint & <10
265265
...
266266
}
267267
-- expect-out.cue --

cmd/cue/cmd/testdata/script/embed.txtar

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ f: {
149149
version: "v1alpha1"
150150
}
151151
#Bar: {
152-
foo: {
153-
a: int
154-
b: uint & <10
152+
foo!: {
153+
a!: int
154+
b!: uint & <10
155155
}
156156
}
157157
#Foo: {
158-
a: int
159-
b: uint & <10
158+
a!: int
159+
b!: uint & <10
160160
}
161161
}
162162
g: {

cmd/cue/cmd/testdata/script/import_auto.txtar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ info: {
2222
}
2323

2424
#Foo: {
25-
a: int
26-
b: int & <10 & >=0
25+
a!: int
26+
b!: int & <10 & >=0
2727
...
2828
}
2929
#Bar: {
30-
foo: #Foo
30+
foo!: #Foo
3131
...
3232
}
3333
-- openapi.yaml --

encoding/jsonschema/constraints.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,9 @@ var constraints = []*constraint{
516516
f := fields[str]
517517
if f == nil && ok {
518518
f := &ast.Field{
519-
Label: ast.NewString(str),
520-
Value: ast.NewIdent("_"),
519+
Label: ast.NewString(str),
520+
Value: ast.NewIdent("_"),
521+
Constraint: token.NOT,
521522
}
522523
fields[str] = f
523524
obj.Elts = append(obj.Elts, f)
@@ -526,6 +527,7 @@ var constraints = []*constraint{
526527
if f.Optional == token.NoPos {
527528
s.errf(n, "duplicate required field %q", str)
528529
}
530+
f.Constraint = token.NOT
529531
f.Optional = token.NoPos
530532
}
531533
}),

encoding/jsonschema/testdata/basic.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import "strings"
4747

4848
// A person is a human being.
4949
person?: {
50-
name: string
50+
name!: string
5151

5252
// where does this person live?
5353
address?: strings.MinRunes(4) & strings.MaxRunes(20)

encoding/jsonschema/testdata/def.txtar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ billing_address?: #address
4646
shipping_address?: #address
4747

4848
#address: {
49-
street_address: string
50-
city: string
51-
state: string
49+
street_address!: string
50+
city!: string
51+
state!: string
5252
...
5353
}
5454

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- schema.json --
2+
{
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"additionalProperties": false,
5+
"id": "https://example.test/example",
6+
"required": ["x", "z"],
7+
"title": "example jsonschema",
8+
"type": "object",
9+
"properties": {
10+
"x": {
11+
"description": "A required field",
12+
"type": "number"
13+
},
14+
"y": {
15+
"description": "An optional field",
16+
"type": "number"
17+
}
18+
}
19+
}
20+
21+
-- out/decode/cue --
22+
// example jsonschema
23+
@jsonschema(schema="http://json-schema.org/draft-04/schema#")
24+
25+
// A required field
26+
x!: number
27+
28+
// An optional field
29+
y?: number
30+
z!: _

0 commit comments

Comments
 (0)