Skip to content

Commit

Permalink
encoding/jsonschema: decode required properties as required fields
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
haoqixu authored and mvdan committed Aug 7, 2024
1 parent 252a666 commit c4697bd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 25 deletions.
22 changes: 11 additions & 11 deletions cmd/cue/cmd/testdata/script/def_openapi.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ info: {
version: *"v1alpha1" | string
}
#Bar: {
foo: #Foo
foo!: #Foo
...
}
#Foo: {
a: int
b: uint & <10
a!: int
b!: uint & <10
...
}
-- expect-cue2 --
Expand All @@ -232,12 +232,12 @@ info: {
version: *"v1" | string
}
#Bar: {
foo: #Foo
foo!: #Foo
...
}
#Foo: {
a: int
b: uint & <10
a!: int
b!: uint & <10
...
}
-- expect-cue3 --
Expand All @@ -251,17 +251,17 @@ info: {
version: (*"v1alpha1" | string) & (*"v1alpha1" | string)
}
#Bar: {
foo: #Foo
foo!: #Foo
...
}
#Foo: {
a: int
b: uint & <10
a!: int
b!: uint & <10
...
}
#Baz: {
a: int
b: uint & <10
a!: int
b!: uint & <10
...
}
-- expect-out.cue --
Expand Down
10 changes: 5 additions & 5 deletions cmd/cue/cmd/testdata/script/embed.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ f: {
version: "v1alpha1"
}
#Bar: {
foo: {
a: int
b: uint & <10
foo!: {
a!: int
b!: uint & <10
}
}
#Foo: {
a: int
b: uint & <10
a!: int
b!: uint & <10
}
}
g: {
Expand Down
6 changes: 3 additions & 3 deletions cmd/cue/cmd/testdata/script/import_auto.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ info: {
}

#Foo: {
a: int
b: int & <10 & >=0
a!: int
b!: int & <10 & >=0
...
}
#Bar: {
foo: #Foo
foo!: #Foo
...
}
-- openapi.yaml --
Expand Down
6 changes: 4 additions & 2 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,9 @@ var constraints = []*constraint{
f := fields[str]
if f == nil && ok {
f := &ast.Field{
Label: ast.NewString(str),
Value: ast.NewIdent("_"),
Label: ast.NewString(str),
Value: ast.NewIdent("_"),
Constraint: token.NOT,
}
fields[str] = f
obj.Elts = append(obj.Elts, f)
Expand All @@ -526,6 +527,7 @@ var constraints = []*constraint{
if f.Optional == token.NoPos {
s.errf(n, "duplicate required field %q", str)
}
f.Constraint = token.NOT
f.Optional = token.NoPos
}
}),
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import "strings"

// A person is a human being.
person?: {
name: string
name!: string

// where does this person live?
address?: strings.MinRunes(4) & strings.MaxRunes(20)
Expand Down
6 changes: 3 additions & 3 deletions encoding/jsonschema/testdata/def.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ billing_address?: #address
shipping_address?: #address

#address: {
street_address: string
city: string
state: string
street_address!: string
city!: string
state!: string
...
}

Expand Down
30 changes: 30 additions & 0 deletions encoding/jsonschema/testdata/required.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- schema.json --
{
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"id": "https://example.test/example",
"required": ["x", "z"],
"title": "example jsonschema",
"type": "object",
"properties": {
"x": {
"description": "A required field",
"type": "number"
},
"y": {
"description": "An optional field",
"type": "number"
}
}
}

-- out/decode/cue --
// example jsonschema
@jsonschema(schema="http://json-schema.org/draft-04/schema#")

// A required field
x!: number

// An optional field
y?: number
z!: _

0 comments on commit c4697bd

Please sign in to comment.