Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
encoding/openapi: implement structural schema
Browse files Browse the repository at this point in the history
See https://kubernetes.io/blog/2019/06/20/crd-structural-schema/

This is needed to make generated schema compliant with CRDs.

Structural schema are momentarily enabled by requesting to
expand references. Even when not expanding, the generator
will strive to normalize the schema somewhat, however.

Change-Id: I36fc8bc0d0e41d1b47b8bed55462ab9d07cfc26f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2803
Reviewed-by: Jason Wang <jasonwzm@google.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Aug 13, 2019
1 parent 35abfa7 commit 9fad62f
Show file tree
Hide file tree
Showing 13 changed files with 909 additions and 396 deletions.
274 changes: 202 additions & 72 deletions encoding/openapi/build.go

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions encoding/openapi/crd.go
@@ -0,0 +1,167 @@
// Copyright 2019 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openapi

// This file contains functionality for structural schema, a subset of OpenAPI
// used for CRDs.
//
// See https://kubernetes.io/blog/2019/06/20/crd-structural-schema/ for details.
//
// Insofar definitions are compatible, openapi normalizes to structural whenever
// possible.
//
// A core structural schema is only made out of the following fields:
//
// - properties
// - items
// - additionalProperties
// - type
// - nullable
// - title
// - descriptions.
//
// Where the types must be defined for all fields.
//
// In addition, the value validations constraints may be used as defined in
// OpenAPI, with the restriction that
// - within the logical constraints anyOf, allOf, oneOf, and not
// additionalProperties, type, nullable, title, and description may not be used.
// - all mentioned fields must be defined in the core schema.
//
// It appears that CRDs do not allow references.
//

import (
"cuelang.org/go/cue"
)

// newCoreBuilder returns a builder that represents a structural schema.
func newCoreBuilder(c *buildContext) *builder {
b := newRootBuilder(c)
b.properties = map[string]*builder{}
return b
}

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

switch b.kind {
case cue.ListKind:
if b.items != nil {
b.setType("array", "")
schema := b.items.coreSchema("*")
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))
}
if len(p.kvs) > 0 || b.items != nil {
b.setType("object", "")
}
if len(p.kvs) > 0 {
b.setSingle("properties", p, false)
}
// TODO: in Structural schema only one of these is allowed.
if b.items != nil {
schema := b.items.coreSchema("*")
b.setSingle("additionalProperties", schema, false)
}
}

// If there was only a single value associated with this node, we can
// safely assume there were no disjunctions etc. In structural mode this
// is the only chance we get to set certain properties.
if len(b.values) == 1 {
return b.fillSchema(b.values[0])
}

// TODO: do type analysis if we have multiple values and piece out more
// information that applies to all possible instances.

return b.finish()
}

// buildCore collects the CUE values for the structural OpenAPI tree.
// To this extent, all fields of both conjunctions and disjunctions are
// collected in a single properties map.
func (b *builder) buildCore(v cue.Value) {
if !b.ctx.expandRefs {
_, r := v.Reference()
if len(r) > 0 {
return
}
}
b.getDoc(v)
format := extractFormat(v)
if format != "" {
b.format = format
} else {
v = v.Eval()
b.kind = v.IncompleteKind() &^ cue.BottomKind

switch b.kind {
case cue.StructKind:
if typ, ok := v.Elem(); ok {
if b.items == nil {
b.items = newCoreBuilder(b.ctx)
}
b.items.buildCore(typ)
}
b.buildCoreStruct(v)

case cue.ListKind:
if typ, ok := v.Elem(); ok {
if b.items == nil {
b.items = newCoreBuilder(b.ctx)
}
b.items.buildCore(typ)
}
}
}

for _, bv := range b.values {
if bv.Equals(v) {
return
}
}
b.values = append(b.values, v)
}

func (b *builder) buildCoreStruct(v cue.Value) {
op, args := v.Expr()
switch op {
case cue.OrOp, cue.AndOp:
for _, v := range args {
b.buildCore(v)
}
}
for i, _ := v.Fields(cue.Optional(true), cue.Hidden(false)); i.Next(); {
label := i.Label()
sub, ok := b.properties[label]
if !ok {
sub = newCoreBuilder(b.ctx)
b.properties[label] = sub
b.keys = append(b.keys, label)
}
sub.buildCore(i.Value())
}
}
28 changes: 28 additions & 0 deletions encoding/openapi/openapi_test.go
Expand Up @@ -39,6 +39,10 @@ func TestParseDefinitions(t *testing.T) {
in, out string
config *Config
}{{
"structural.cue",
"structural.json",
resolveRefs,
}, {
"simple.cue",
"simple.json",
resolveRefs,
Expand Down Expand Up @@ -138,3 +142,27 @@ func TestParseDefinitions(t *testing.T) {
})
}
}

// This is for debugging purposes. Do not remove.
func TestX(t *testing.T) {
t.Skip()

var r cue.Runtime
inst, err := r.Compile("test", `
AnyField: "any value"
`)
if err != nil {
t.Fatal(err)
}

b, err := Gen(inst, &Config{
ExpandReferences: true,
})
if err != nil {
t.Fatal(err)
}

var out = &bytes.Buffer{}
_ = json.Indent(out, b, "", " ")
t.Error(out.String())
}
20 changes: 12 additions & 8 deletions encoding/openapi/testdata/array.json
Expand Up @@ -9,12 +9,13 @@
"bar": {
"type": "array",
"items": {
"default": "1",
"type": "string",
"enum": [
"1",
"2",
"3"
]
],
"default": "1"
}
},
"foo": {
Expand All @@ -28,12 +29,13 @@
"e": {
"type": "array",
"items": {
"default": "1",
"type": "string",
"enum": [
"1",
"2",
"3"
]
],
"default": "1"
}
}
}
Expand All @@ -52,12 +54,13 @@
},
"MyEnum": {
"description": "MyEnum",
"default": "1",
"type": "string",
"enum": [
"1",
"2",
"3"
]
],
"default": "1"
},
"MyStruct": {
"description": "MyStruct",
Expand All @@ -69,12 +72,13 @@
"e": {
"type": "array",
"items": {
"default": "1",
"type": "string",
"enum": [
"1",
"2",
"3"
]
],
"default": "1"
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions encoding/openapi/testdata/nums.json
Expand Up @@ -10,14 +10,11 @@
"neq": {
"type": "number",
"not": {
"type": "number",
"allOff": [
{
"type": "number",
"minimum": 4
},
{
"type": "number",
"maximum": 4
}
]
Expand Down
3 changes: 1 addition & 2 deletions encoding/openapi/testdata/oneof-funcs.json
Expand Up @@ -8,9 +8,9 @@
"schemas": {
"MYSTRING": {
"description": "Randomly picked description from a set of size one.",
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"exact"
],
Expand All @@ -23,7 +23,6 @@
}
},
{
"type": "object",
"required": [
"regex"
],
Expand Down

0 comments on commit 9fad62f

Please sign in to comment.