Skip to content

Commit

Permalink
encoding/jsonschema: fix dropping of struct for additionalProperties:…
Browse files Browse the repository at this point in the history
… true

Fixes #734

Change-Id: I4ff1ee63d894bcc76d25bc406c52eeda59deab90
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8781
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 17, 2021
1 parent ae43812 commit 430c3dd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 18 deletions.
4 changes: 3 additions & 1 deletion cmd/cue/cmd/testdata/script/cmd_github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,9 @@ import "strings"

#event: "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_dispatch" | "workflow_run" | "repository_dispatch"

#eventObject: null
#eventObject: null | {
...
}

#expressionSyntax: =~"^\\$\\{\\{.*\\}\\}$"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ import "strings"

#event: "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_dispatch" | "workflow_run" | "repository_dispatch"

#eventObject: null
#eventObject: null | {
...
}

#expressionSyntax: =~"^\\$\\{\\{.*\\}\\}$"

Expand Down
37 changes: 25 additions & 12 deletions cue/testdata/eval/github.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,9 @@ import "strings"

#event: "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_dispatch" | "workflow_run" | "repository_dispatch"

#eventObject: null
#eventObject: null | {
...
}

#expressionSyntax: =~"^\\$\\{\\{.*\\}\\}$"

Expand Down Expand Up @@ -1075,7 +1077,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1305,7 +1308,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1430,7 +1434,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1559,7 +1564,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1625,7 +1631,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1699,7 +1706,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -1926,7 +1934,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -2048,7 +2057,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -2120,7 +2130,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -2246,7 +2257,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down Expand Up @@ -2309,7 +2321,8 @@ import "strings"
name: (string){ string }
}
#event: (string){ |((string){ "check_run" }, (string){ "check_suite" }, (string){ "create" }, (string){ "delete" }, (string){ "deployment" }, (string){ "deployment_status" }, (string){ "fork" }, (string){ "gollum" }, (string){ "issue_comment" }, (string){ "issues" }, (string){ "label" }, (string){ "member" }, (string){ "milestone" }, (string){ "page_build" }, (string){ "project" }, (string){ "project_card" }, (string){ "project_column" }, (string){ "public" }, (string){ "pull_request" }, (string){ "pull_request_review" }, (string){ "pull_request_review_comment" }, (string){ "pull_request_target" }, (string){ "push" }, (string){ "registry_package" }, (string){ "release" }, (string){ "status" }, (string){ "watch" }, (string){ "workflow_dispatch" }, (string){ "workflow_run" }, (string){ "repository_dispatch" }) }
#eventObject: (null){ null }
#eventObject: ((null|struct)){ |((null){ null }, (#struct){
}) }
#expressionSyntax: (string){ =~"^\\$\\{\\{.*\\}\\}$" }
#globs: (list){
0: (string){ strings.MinRunes(1) }
Expand Down
3 changes: 0 additions & 3 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,6 @@ var constraints = []*constraint{
switch n.Kind() {
case cue.BoolKind:
s.closeStruct = !s.boolValue(n)
if !s.closeStruct {
s.usedTypes |= cue.StructKind
}

case cue.StructKind:
s.usedTypes |= cue.StructKind
Expand Down
36 changes: 36 additions & 0 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/yaml"
"cuelang.org/go/internal"
"cuelang.org/go/internal/cuetest"
_ "cuelang.org/go/pkg"
)
Expand Down Expand Up @@ -148,3 +149,38 @@ func TestDecode(t *testing.T) {
})
assert.NoError(t, err)
}

func TestX(t *testing.T) {
t.Skip()
data := `
-- schema.json --
`

a := txtar.Parse([]byte(data))

r := &cue.Runtime{}
var in *cue.Instance
var err error
for _, f := range a.Files {
switch path.Ext(f.Name) {
case ".json":
in, err = json.Decode(r, f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
case ".yaml":
in, err = yaml.Decode(r, f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
}
}

cfg := &Config{ID: "test"}
expr, err := Extract(in, cfg)
if err != nil {
t.Fatal(err)
}

t.Fatal(internal.DebugStr(expr))
}
36 changes: 36 additions & 0 deletions encoding/jsonschema/testdata/emptyobj.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Objects without properties should convert correctly.
//
// Issue #734
-- github-workflow.json --
{
"$schema": "http://json-schema.org/draft-07/schema",
"definitions": {
"obj1": {
"oneOf": [
{
"type": "object"
},
{
"type": "null"
}
],
"additionalProperties": true
},
"obj2": {
"type": [ "object", "null" ],
"additionalProperties": true
}
}
}
-- out.err --
-- out.cue --
@jsonschema(schema="http://json-schema.org/draft-07/schema")
_

#obj1: null | {
...
}

#obj2: null | {
...
}
4 changes: 3 additions & 1 deletion encoding/jsonschema/testdata/github.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,9 @@ null | bool | number | string | [...] | {

#event: "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_dispatch" | "workflow_run" | "repository_dispatch"

#eventObject: null
#eventObject: null | {
...
}

#expressionSyntax: =~"^\\$\\{\\{.*\\}\\}$"

Expand Down

0 comments on commit 430c3dd

Please sign in to comment.