Skip to content

Commit

Permalink
encoding/openapi: fix generation bug in CRD path
Browse files Browse the repository at this point in the history
This could lead to duplicate path elements.

Tested in new evaluator code.

Change-Id: I0875d135c483bcab772d8fe656f41ed88a57cb20
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6584
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 17, 2020
1 parent 29fa119 commit 616d9e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func (b *builder) schema(core *builder, name string, v cue.Value) *ast.StructLit
var c *builder
if core == nil && b.ctx.structural {
c = newCoreBuilder(b.ctx)
c.buildCore(v) // initialize core structure
c.coreSchema(name) // build the
c.buildCore(v) // initialize core structure
c.coreSchema()
} else {
c = newRootBuilder(b.ctx)
c.core = core
Expand Down
16 changes: 10 additions & 6 deletions encoding/openapi/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,29 @@ func newCoreBuilder(c *buildContext) *builder {
return b
}

// coreSchema creates the core part of a structural OpenAPI.
func (b *builder) coreSchema(name string) *ast.StructLit {
func (b *builder) coreSchemaWithName(name string) *ast.StructLit {
oldPath := b.ctx.path
b.ctx.path = append(b.ctx.path, name)
defer func() { b.ctx.path = oldPath }()
s := b.coreSchema()
b.ctx.path = oldPath
return s
}

// coreSchema creates the core part of a structural OpenAPI.
func (b *builder) coreSchema() *ast.StructLit {
switch b.kind {
case cue.ListKind:
if b.items != nil {
b.setType("array", "")
schema := b.items.coreSchema("*")
schema := b.items.coreSchemaWithName("*")
b.setSingle("items", schema, false)
}

case cue.StructKind:
p := &OrderedMap{}
for _, k := range b.keys {
sub := b.properties[k]
p.Set(k, sub.coreSchema(k))
p.Set(k, sub.coreSchemaWithName(k))
}
if p.len() > 0 || b.items != nil {
b.setType("object", "")
Expand All @@ -83,7 +87,7 @@ func (b *builder) coreSchema(name string) *ast.StructLit {
}
// TODO: in Structural schema only one of these is allowed.
if b.items != nil {
schema := b.items.coreSchema("*")
schema := b.items.coreSchemaWithName("*")
b.setSingle("additionalProperties", schema, false)
}
}
Expand Down

0 comments on commit 616d9e8

Please sign in to comment.