Skip to content

Commit

Permalink
encoding/openapi: make title and version defaults
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Apr 22, 2020
1 parent 4d8d154 commit a85238c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
10 changes: 6 additions & 4 deletions cmd/cue/cmd/testdata/script/def_openapi.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cue def openapi+cue: expect-cue-out -o -

cue def foo.cue -o openapi:-
cmp stdout expect-json-out

Expand Down Expand Up @@ -172,8 +174,8 @@ components: schemas: {
package foo

info: {
title: "My OpenAPI"
version: "v1alpha1"
title: *"My OpenAPI" | string
version: *"v1alpha1" | string
}

Bar :: {
Expand All @@ -187,8 +189,8 @@ Foo :: {
}
-- expect-cue2 --
info: {
title: "Some clever title."
version: "v1"
title: *"Some clever title." | string
version: *"v1" | string
}
Bar :: {
foo: Foo
Expand Down
4 changes: 2 additions & 2 deletions cmd/cue/cmd/testdata/script/import_auto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ cmp stdout expect-openapi
// An OpenAPI file

info: {
title: "An OpenAPI file"
version: "v1beta1"
title: *"An OpenAPI file" | string
version: *"v1beta1" | string
}

Foo :: {
Expand Down
30 changes: 25 additions & 5 deletions encoding/openapi/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Extract(data *cue.Instance, c *Config) (*ast.File, error) {
p := &ast.Package{Name: ast.NewIdent(c.PkgName)}
p.AddComment(cg)
add(p)
} else if cg != nil {
} else {
add(cg)
}

Expand All @@ -86,10 +86,30 @@ func Extract(data *cue.Instance, c *Config) (*ast.File, error) {
// }

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) {
Expand Down
1 change: 0 additions & 1 deletion encoding/openapi/testdata/openapi.cue
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// An OpenAPI testing package.
package openapi

Expand Down
4 changes: 2 additions & 2 deletions encoding/openapi/testdata/script/basics.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ components:
package foo

info: {
title: "Users schema"
version: "v1beta1"
title: *"Users schema" | string
version: *"v1beta1" | string
contact: {
name: "The CUE Authors"
url: "https://cuelang.org"
Expand Down

0 comments on commit a85238c

Please sign in to comment.