Skip to content

Commit

Permalink
encoding/openapi: few tweaks in the output
Browse files Browse the repository at this point in the history
- use oneOf instead of anyOf, as all values are
  derived from proto anyway for now.
- don't include empty list default

Change-Id: Ifd22e6f8bbd7d168123d00cc596cd7bb83ac517f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2349
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 25, 2019
1 parent ebf960e commit fdd176c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
39 changes: 32 additions & 7 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func components(inst *cue.Instance, cfg *Config) (comps *orderedMap, err error)
for i.Next() {
// message, enum, or constant.
label := i.Label()
if c.isInternal(label) {
continue
}
c.schemas.Set(label, c.build(label, i.Value()))
}
return comps, nil
Expand All @@ -81,9 +84,16 @@ func (c *buildContext) build(name string, v cue.Value) *oaSchema {
return newRootBuilder(c).schema(name, v)
}

// isInternal reports whether or not to include this type.
func (c *buildContext) isInternal(name string) bool {
// TODO: allow a regexp filter in Config. If we have closed structs and
// definitions, this will likely be unnecessary.
return strings.HasSuffix(name, "_value")
}

// shouldExpand reports is the given identifier is not exported.
func (c *buildContext) shouldExpand(name string) bool {
return c.expandRefs
return c.expandRefs || c.isInternal(name)
}

func (b *builder) failf(v cue.Value, format string, args ...interface{}) {
Expand Down Expand Up @@ -115,6 +125,7 @@ func (b *builder) schema(name string, v cue.Value) *oaSchema {

func (b *builder) value(v cue.Value, f typeFunc) {
count := 0
disallowDefault := false
var values cue.Value
if b.ctx.shouldExpand(strings.Join(v.Reference(), ".")) {
values = v
Expand All @@ -126,6 +137,7 @@ func (b *builder) value(v cue.Value, f typeFunc) {
switch r := v.Reference(); {
case r != nil:
b.addRef(r)
disallowDefault = true
default:
count++
values = values.Unify(v)
Expand Down Expand Up @@ -165,9 +177,20 @@ func (b *builder) value(v cue.Value, f typeFunc) {
}
}

if v, ok := v.Default(); ok && v.IsConcrete() {
v.Default()
b.set("default", v)
if v, ok := v.Default(); ok && v.IsConcrete() && !disallowDefault {
// TODO: should we show the empty list default? This would be correct
// but perhaps a bit too pedantic and noisy.
switch {
case v.Kind() == cue.ListKind:
iter, _ := v.List()
if !iter.Next() {
// Don't show default for empty list.
break
}
fallthrough
default:
b.set("default", v)
}
}
}

Expand Down Expand Up @@ -273,7 +296,9 @@ func (b *builder) disjunction(a []cue.Value, f typeFunc) {
anyOf = append(anyOf, c.finish())
}

b.set("anyOf", anyOf)
// TODO: analyze CUE structs to figure out if it should be oneOf or
// anyOf. As the source is protobuf for now, it is always oneOf.
b.set("oneOf", anyOf)
if nullable {
b.set("nullable", true)
}
Expand Down Expand Up @@ -569,7 +594,7 @@ func (b *builder) string(v cue.Value) {
// - maxLength
// - minLength

case cue.NoOp:
case cue.NoOp, cue.SelectorOp:
// TODO: determine formats from specific types.

default:
Expand Down Expand Up @@ -600,7 +625,7 @@ func (b *builder) bytes(v cue.Value) {
// - maxLength
// - minLength

case cue.NoOp:
case cue.NoOp, cue.SelectorOp:

default:
b.failf(v, "unsupported op %v for bytes type", op)
Expand Down
5 changes: 2 additions & 3 deletions encoding/openapi/testdata/oneof.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"components": {
"schema": {
"MyString": {
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -46,8 +46,7 @@
"type": "array",
"items": {
"$ref": "#/components/schema/MyString"
},
"default": []
}
},
"include": {
"$ref": "#/components/schema/MyString"
Expand Down
26 changes: 11 additions & 15 deletions encoding/openapi/testdata/openapi-norefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"foo": {
"type": "number",
Expand All @@ -35,8 +34,7 @@
"type": "array",
"items": {
"type": "integer"
},
"default": []
}
},
"port": {
"type": "integer"
Expand All @@ -47,7 +45,7 @@
},
{
"type": "object",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -89,8 +87,7 @@
"type": "array",
"items": {
"type": "integer"
},
"default": []
}
},
"port": {
"type": "integer"
Expand All @@ -103,7 +100,7 @@
"maximum": 2147483647
},
"YourMessage": {
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -137,7 +134,7 @@
"YourMessage2": {
"allOf": [
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand All @@ -163,7 +160,7 @@
]
},
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand All @@ -189,7 +186,7 @@
]
},
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -217,7 +214,7 @@
]
},
"Msg2": {
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -263,7 +260,7 @@
"DefaultStruct": {
"allOf": [
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand All @@ -275,8 +272,7 @@
"type": "array",
"items": {
"type": "integer"
},
"default": []
}
},
"port": {
"type": "integer"
Expand Down
20 changes: 9 additions & 11 deletions encoding/openapi/testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"foo": {
"allOf": [
Expand All @@ -39,7 +38,7 @@
},
{
"type": "object",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -81,8 +80,7 @@
"type": "array",
"items": {
"type": "integer"
},
"default": []
}
},
"port": {
"type": "integer"
Expand All @@ -95,7 +93,7 @@
"maximum": 2147483647
},
"YourMessage": {
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -129,7 +127,7 @@
"YourMessage2": {
"allOf": [
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand All @@ -155,7 +153,7 @@
]
},
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand All @@ -181,7 +179,7 @@
]
},
{
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -209,7 +207,7 @@
]
},
"Msg2": {
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -255,7 +253,7 @@
"DefaultStruct": {
"allOf": [
{
"anyOf": [
"oneOf": [
{
"$ref": "#/components/schema/Port"
},
Expand Down

0 comments on commit fdd176c

Please sign in to comment.