encoding/openapi: make title and version defaults
The title and version may cause a conflict when merging two
OpenAPI schema. Make them default values to allow merging
OpenAPI shema in such a case.
This is orthogonal to an option to omit certain sections.
Change-Id: If65beee3cb9c5da835c77f9c1bd448b68f97e62c
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5744
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/testdata/script/def_openapi.txt b/cmd/cue/cmd/testdata/script/def_openapi.txt
index fc38380..47d0b95 100644
--- a/cmd/cue/cmd/testdata/script/def_openapi.txt
+++ b/cmd/cue/cmd/testdata/script/def_openapi.txt
@@ -1,3 +1,5 @@
+cue def openapi+cue: expect-cue-out -o -
+
cue def foo.cue -o openapi:-
cmp stdout expect-json-out
@@ -172,8 +174,8 @@
package foo
info: {
- title: "My OpenAPI"
- version: "v1alpha1"
+ title: *"My OpenAPI" | string
+ version: *"v1alpha1" | string
}
Bar :: {
@@ -187,8 +189,8 @@
}
-- expect-cue2 --
info: {
- title: "Some clever title."
- version: "v1"
+ title: *"Some clever title." | string
+ version: *"v1" | string
}
Bar :: {
foo: Foo
diff --git a/cmd/cue/cmd/testdata/script/import_auto.txt b/cmd/cue/cmd/testdata/script/import_auto.txt
index 3011603..bef7dab 100644
--- a/cmd/cue/cmd/testdata/script/import_auto.txt
+++ b/cmd/cue/cmd/testdata/script/import_auto.txt
@@ -5,8 +5,8 @@
// An OpenAPI file
info: {
- title: "An OpenAPI file"
- version: "v1beta1"
+ title: *"An OpenAPI file" | string
+ version: *"v1beta1" | string
}
Foo :: {
diff --git a/encoding/openapi/decode.go b/encoding/openapi/decode.go
index 23a0cc1..8f24dbe 100644
--- a/encoding/openapi/decode.go
+++ b/encoding/openapi/decode.go
@@ -61,7 +61,7 @@
p := &ast.Package{Name: ast.NewIdent(c.PkgName)}
p.AddComment(cg)
add(p)
- } else if cg != nil {
+ } else {
add(cg)
}
@@ -86,10 +86,30 @@
// }
if info := v.Lookup("info"); info.Exists() {
- add(&ast.Field{
- Label: ast.NewIdent("info"),
- Value: info.Syntax().(ast.Expr),
- })
+ decls := []interface{}{}
+ if st, ok := info.Syntax().(*ast.StructLit); ok {
+ // Remove title.
+ for _, d := range st.Elts {
+ if f, ok := d.(*ast.Field); ok {
+ switch name, _, _ := ast.LabelName(f.Label); name {
+ case "title", "version":
+ // title: *"title" | string
+ decls = append(decls, &ast.Field{
+ Label: f.Label,
+ Value: ast.NewBinExpr(token.OR,
+ &ast.UnaryExpr{Op: token.MUL, X: f.Value},
+ ast.NewIdent("string")),
+ })
+ continue
+ }
+ }
+ decls = append(decls, d)
+ }
+ add(&ast.Field{
+ Label: ast.NewIdent("info"),
+ Value: ast.NewStruct(decls...),
+ })
+ }
}
if i < len(js.Decls) {
diff --git a/encoding/openapi/testdata/openapi.cue b/encoding/openapi/testdata/openapi.cue
index 2ed3fab..9295d70 100644
--- a/encoding/openapi/testdata/openapi.cue
+++ b/encoding/openapi/testdata/openapi.cue
@@ -1,4 +1,3 @@
-
// An OpenAPI testing package.
package openapi
diff --git a/encoding/openapi/testdata/script/basics.txtar b/encoding/openapi/testdata/script/basics.txtar
index 1d5c153..4056564 100644
--- a/encoding/openapi/testdata/script/basics.txtar
+++ b/encoding/openapi/testdata/script/basics.txtar
@@ -28,8 +28,8 @@
package foo
info: {
- title: "Users schema"
- version: "v1beta1"
+ title: *"Users schema" | string
+ version: *"v1beta1" | string
contact: {
name: "The CUE Authors"
url: "https://cuelang.org"