Merge pull request #71137 from sttts/sttts-crd-openapi-spec-recursive-v2-prune

apiextensions: prune {any,one}Of + Not recursively on OpenAPI v2 conversion
This commit is contained in:
k8s-ci-robot 2018-11-16 13:20:25 -08:00 committed by GitHub
commit 3ea3cfc3be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 220 additions and 134 deletions

View File

@ -36,7 +36,6 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
informers "k8s.io/apiextensions-apiserver/pkg/client/informers/internalversion/apiextensions/internalversion" informers "k8s.io/apiextensions-apiserver/pkg/client/informers/internalversion/apiextensions/internalversion"
listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion" listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion"
apiextensionsfeatures "k8s.io/apiextensions-apiserver/pkg/features" apiextensionsfeatures "k8s.io/apiextensions-apiserver/pkg/features"
@ -131,18 +130,10 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {
if err != nil { if err != nil {
return err return err
} }
// Convert internal CustomResourceValidation to versioned CustomResourceValidation
versionedSchema := new(v1beta1.CustomResourceValidation)
if validationSchema == nil {
versionedSchema = nil
} else {
if err := v1beta1.Convert_apiextensions_CustomResourceValidation_To_v1beta1_CustomResourceValidation(validationSchema, versionedSchema, nil); err != nil {
return err
}
}
// We aggregate the schema even if it's nil as it maybe a removal of the schema for this CRD, // We aggregate the schema even if it's nil as it maybe a removal of the schema for this CRD,
// and the aggreated OpenAPI spec should reflect this change. // and the aggreated OpenAPI spec should reflect this change.
crdspec, etag, err := apiextensionsopenapi.CustomResourceDefinitionOpenAPISpec(&crd.Spec, version.Version, versionedSchema) crdspec, etag, err := apiextensionsopenapi.CustomResourceDefinitionOpenAPISpec(&crd.Spec, version.Version, validationSchema)
if err != nil { if err != nil {
return err return err
} }

View File

@ -50,8 +50,17 @@ func ValidateCustomResource(customResource interface{}, validator *validate.Sche
return nil return nil
} }
// ConvertJSONSchemaProps converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema // ConvertJSONSchemaProps converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema.
func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema) error { func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema) error {
return ConvertJSONSchemaPropsWithPostProcess(in, out, nil)
}
// PostProcessFunc post-processes one node of a spec.Schema.
type PostProcessFunc func(*spec.Schema) error
// ConvertJSONSchemaPropsWithPostProcess converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema
// and run a post process step on each JSONSchemaProps node.
func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, out *spec.Schema, postProcess PostProcessFunc) error {
if in == nil { if in == nil {
return nil return nil
} }
@ -86,41 +95,43 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
out.Example = *(in.Example) out.Example = *(in.Example)
} }
out.Enum = make([]interface{}, len(in.Enum)) if in.Enum != nil {
for k, v := range in.Enum { out.Enum = make([]interface{}, len(in.Enum))
out.Enum[k] = v for k, v := range in.Enum {
out.Enum[k] = v
}
} }
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf); err != nil { if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf, postProcess); err != nil {
return err return err
} }
if err := convertSliceOfJSONSchemaProps(&in.OneOf, &out.OneOf); err != nil { if err := convertSliceOfJSONSchemaProps(&in.OneOf, &out.OneOf, postProcess); err != nil {
return err return err
} }
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf); err != nil { if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf, postProcess); err != nil {
return err return err
} }
if in.Not != nil { if in.Not != nil {
in, out := &in.Not, &out.Not in, out := &in.Not, &out.Not
*out = new(spec.Schema) *out = new(spec.Schema)
if err := ConvertJSONSchemaProps(*in, *out); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err return err
} }
} }
var err error var err error
out.Properties, err = convertMapOfJSONSchemaProps(in.Properties) out.Properties, err = convertMapOfJSONSchemaProps(in.Properties, postProcess)
if err != nil { if err != nil {
return err return err
} }
out.PatternProperties, err = convertMapOfJSONSchemaProps(in.PatternProperties) out.PatternProperties, err = convertMapOfJSONSchemaProps(in.PatternProperties, postProcess)
if err != nil { if err != nil {
return err return err
} }
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions) out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions, postProcess)
if err != nil { if err != nil {
return err return err
} }
@ -135,7 +146,7 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
if in.AdditionalProperties != nil { if in.AdditionalProperties != nil {
in, out := &in.AdditionalProperties, &out.AdditionalProperties in, out := &in.AdditionalProperties, &out.AdditionalProperties
*out = new(spec.SchemaOrBool) *out = new(spec.SchemaOrBool)
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil { if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
return err return err
} }
} }
@ -143,7 +154,7 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
if in.AdditionalItems != nil { if in.AdditionalItems != nil {
in, out := &in.AdditionalItems, &out.AdditionalItems in, out := &in.AdditionalItems, &out.AdditionalItems
*out = new(spec.SchemaOrBool) *out = new(spec.SchemaOrBool)
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil { if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
return err return err
} }
} }
@ -151,7 +162,7 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
if in.Items != nil { if in.Items != nil {
in, out := &in.Items, &out.Items in, out := &in.Items, &out.Items
*out = new(spec.SchemaOrArray) *out = new(spec.SchemaOrArray)
if err := convertJSONSchemaPropsOrArray(*in, *out); err != nil { if err := convertJSONSchemaPropsOrArray(*in, *out, postProcess); err != nil {
return err return err
} }
} }
@ -161,7 +172,7 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
*out = make(spec.Dependencies, len(*in)) *out = make(spec.Dependencies, len(*in))
for key, val := range *in { for key, val := range *in {
newVal := new(spec.SchemaOrStringArray) newVal := new(spec.SchemaOrStringArray)
if err := convertJSONSchemaPropsOrStringArray(&val, newVal); err != nil { if err := convertJSONSchemaPropsOrStringArray(&val, newVal, postProcess); err != nil {
return err return err
} }
(*out)[key] = *newVal (*out)[key] = *newVal
@ -174,14 +185,20 @@ func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
out.ExternalDocs.URL = in.ExternalDocs.URL out.ExternalDocs.URL = in.ExternalDocs.URL
} }
if postProcess != nil {
if err := postProcess(out); err != nil {
return err
}
}
return nil return nil
} }
func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]spec.Schema) error { func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]spec.Schema, postProcess PostProcessFunc) error {
if in != nil { if in != nil {
for _, jsonSchemaProps := range *in { for _, jsonSchemaProps := range *in {
schema := spec.Schema{} schema := spec.Schema{}
if err := ConvertJSONSchemaProps(&jsonSchemaProps, &schema); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
return err return err
} }
*out = append(*out, schema) *out = append(*out, schema)
@ -190,25 +207,27 @@ func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]s
return nil return nil
} }
func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps) (map[string]spec.Schema, error) { func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps, postProcess PostProcessFunc) (map[string]spec.Schema, error) {
if in == nil {
return nil, nil
}
out := make(map[string]spec.Schema) out := make(map[string]spec.Schema)
if len(in) != 0 { for k, jsonSchemaProps := range in {
for k, jsonSchemaProps := range in { schema := spec.Schema{}
schema := spec.Schema{} if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
if err := ConvertJSONSchemaProps(&jsonSchemaProps, &schema); err != nil { return nil, err
return nil, err
}
out[k] = schema
} }
out[k] = schema
} }
return out, nil return out, nil
} }
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray) error { func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray, postProcess PostProcessFunc) error {
if in.Schema != nil { if in.Schema != nil {
in, out := &in.Schema, &out.Schema in, out := &in.Schema, &out.Schema
*out = new(spec.Schema) *out = new(spec.Schema)
if err := ConvertJSONSchemaProps(*in, *out); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err return err
} }
} }
@ -216,7 +235,7 @@ func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out
in, out := &in.JSONSchemas, &out.Schemas in, out := &in.JSONSchemas, &out.Schemas
*out = make([]spec.Schema, len(*in)) *out = make([]spec.Schema, len(*in))
for i := range *in { for i := range *in {
if err := ConvertJSONSchemaProps(&(*in)[i], &(*out)[i]); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(&(*in)[i], &(*out)[i], postProcess); err != nil {
return err return err
} }
} }
@ -224,24 +243,24 @@ func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out
return nil return nil
} }
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool) error { func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool, postProcess PostProcessFunc) error {
out.Allows = in.Allows out.Allows = in.Allows
if in.Schema != nil { if in.Schema != nil {
in, out := &in.Schema, &out.Schema in, out := &in.Schema, &out.Schema
*out = new(spec.Schema) *out = new(spec.Schema)
if err := ConvertJSONSchemaProps(*in, *out); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err return err
} }
} }
return nil return nil
} }
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray) error { func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray, postProcess PostProcessFunc) error {
out.Property = in.Property out.Property = in.Property
if in.Schema != nil { if in.Schema != nil {
in, out := &in.Schema, &out.Schema in, out := &in.Schema, &out.Schema
*out = new(spec.Schema) *out = new(spec.Schema)
if err := ConvertJSONSchemaProps(*in, *out); err != nil { if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err return err
} }
} }

View File

@ -14,7 +14,7 @@ go_library(
deps = [ deps = [
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library", "//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions:go_default_library", "//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions:go_default_library",
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library", "//staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library", "//vendor/github.com/go-openapi/spec:go_default_library",
@ -28,7 +28,8 @@ go_test(
srcs = ["conversion_test.go"], srcs = ["conversion_test.go"],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = [ deps = [
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library", "//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library", "//vendor/github.com/go-openapi/spec:go_default_library",
], ],
) )

View File

@ -17,33 +17,29 @@ limitations under the License.
package openapi package openapi
import ( import (
"encoding/json"
"github.com/go-openapi/spec" "github.com/go-openapi/spec"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
) )
// ConvertJSONSchemaPropsToOpenAPIv2Schema converts our internal OpenAPI v3 schema // ConvertJSONSchemaPropsToOpenAPIv2Schema converts our internal OpenAPI v3 schema
// (*apiextensions.JSONSchemaProps) to an OpenAPI v2 schema (*spec.Schema). // (*apiextensions.JSONSchemaProps) to an OpenAPI v2 schema (*spec.Schema).
// NOTE: we use versioned type (v1beta1) here so that we can properly marshal the object // NOTE: we use versioned type (v1beta1) here so that we can properly marshal the object
// using the JSON tags // using the JSON tags
func ConvertJSONSchemaPropsToOpenAPIv2Schema(in *v1beta1.JSONSchemaProps) (*spec.Schema, error) { func ConvertJSONSchemaPropsToOpenAPIv2Schema(in *apiextensions.JSONSchemaProps) (*spec.Schema, error) {
if in == nil { if in == nil {
return nil, nil return nil, nil
} }
// Marshal JSONSchemaProps into JSON and unmarshal the data into spec.Schema
data, err := json.Marshal(*in)
if err != nil {
return nil, err
}
out := new(spec.Schema) out := new(spec.Schema)
if err := out.UnmarshalJSON(data); err != nil { validation.ConvertJSONSchemaPropsWithPostProcess(in, out, func(p *spec.Schema) error {
return nil, err // Remove unsupported fields in OpenAPI v2
} p.OneOf = nil
// Remove unsupported fields in OpenAPI v2 p.AnyOf = nil
out.OneOf = nil p.Not = nil
out.AnyOf = nil
out.Not = nil return nil
})
return out, nil return out, nil
} }

View File

@ -17,12 +17,12 @@ limitations under the License.
package openapi package openapi
import ( import (
"encoding/json"
"reflect" "reflect"
"testing" "testing"
"github.com/go-openapi/spec" "github.com/go-openapi/spec"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apimachinery/pkg/util/diff"
) )
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) { func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
@ -30,18 +30,16 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
testStr2 := "test2" testStr2 := "test2"
testFloat64 := float64(6.4) testFloat64 := float64(6.4)
testInt64 := int64(64) testInt64 := int64(64)
raw, _ := json.Marshal(testStr) testApiextensionsJSON := apiextensions.JSON(testStr)
raw2, _ := json.Marshal(testStr2)
testApiextensionsJSON := v1beta1.JSON{Raw: raw}
tests := []struct { tests := []struct {
name string name string
in *v1beta1.JSONSchemaProps in *apiextensions.JSONSchemaProps
expected *spec.Schema expected *spec.Schema
}{ }{
{ {
name: "id", name: "id",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
ID: testStr, ID: testStr,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -49,7 +47,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "$schema", name: "$schema",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Schema: "test", Schema: "test",
}, },
expected: &spec.Schema{ expected: &spec.Schema{
@ -60,14 +58,14 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "$ref", name: "$ref",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Ref: &testStr, Ref: &testStr,
}, },
expected: spec.RefSchema(testStr), expected: spec.RefSchema(testStr),
}, },
{ {
name: "description", name: "description",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Description: testStr, Description: testStr,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -75,7 +73,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "type and format", name: "type and format",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Type: testStr, Type: testStr,
Format: testStr2, Format: testStr2,
}, },
@ -84,7 +82,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "title", name: "title",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Title: testStr, Title: testStr,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -92,7 +90,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "default", name: "default",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Default: &testApiextensionsJSON, Default: &testApiextensionsJSON,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -100,7 +98,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "maximum and exclusiveMaximum", name: "maximum and exclusiveMaximum",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Maximum: &testFloat64, Maximum: &testFloat64,
ExclusiveMaximum: true, ExclusiveMaximum: true,
}, },
@ -109,7 +107,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "minimum and exclusiveMinimum", name: "minimum and exclusiveMinimum",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Minimum: &testFloat64, Minimum: &testFloat64,
ExclusiveMinimum: true, ExclusiveMinimum: true,
}, },
@ -118,7 +116,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "maxLength", name: "maxLength",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MaxLength: &testInt64, MaxLength: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -126,7 +124,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "minLength", name: "minLength",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MinLength: &testInt64, MinLength: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -134,7 +132,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "pattern", name: "pattern",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Pattern: testStr, Pattern: testStr,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -142,7 +140,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "maxItems", name: "maxItems",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MaxItems: &testInt64, MaxItems: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -150,7 +148,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "minItems", name: "minItems",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MinItems: &testInt64, MinItems: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -158,7 +156,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "uniqueItems", name: "uniqueItems",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
UniqueItems: true, UniqueItems: true,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -166,7 +164,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "multipleOf", name: "multipleOf",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MultipleOf: &testFloat64, MultipleOf: &testFloat64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -174,15 +172,15 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "enum", name: "enum",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Enum: []v1beta1.JSON{{Raw: raw}, {Raw: raw2}}, Enum: []apiextensions.JSON{apiextensions.JSON(testStr), apiextensions.JSON(testStr2)},
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
WithEnum(testStr, testStr2), WithEnum(testStr, testStr2),
}, },
{ {
name: "maxProperties", name: "maxProperties",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MaxProperties: &testInt64, MaxProperties: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -190,7 +188,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "minProperties", name: "minProperties",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
MinProperties: &testInt64, MinProperties: &testInt64,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -198,7 +196,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "required", name: "required",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Required: []string{testStr, testStr2}, Required: []string{testStr, testStr2},
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -206,9 +204,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "items single props", name: "items single props",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Items: &v1beta1.JSONSchemaPropsOrArray{ Items: &apiextensions.JSONSchemaPropsOrArray{
Schema: &v1beta1.JSONSchemaProps{ Schema: &apiextensions.JSONSchemaProps{
Type: "boolean", Type: "boolean",
}, },
}, },
@ -223,9 +221,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "items array props", name: "items array props",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Items: &v1beta1.JSONSchemaPropsOrArray{ Items: &apiextensions.JSONSchemaPropsOrArray{
JSONSchemas: []v1beta1.JSONSchemaProps{ JSONSchemas: []apiextensions.JSONSchemaProps{
{Type: "boolean"}, {Type: "boolean"},
{Type: "string"}, {Type: "string"},
}, },
@ -244,8 +242,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "allOf", name: "allOf",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
AllOf: []v1beta1.JSONSchemaProps{ AllOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"}, {Type: "boolean"},
{Type: "string"}, {Type: "string"},
}, },
@ -255,8 +253,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "oneOf", name: "oneOf",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
OneOf: []v1beta1.JSONSchemaProps{ OneOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"}, {Type: "boolean"},
{Type: "string"}, {Type: "string"},
}, },
@ -273,8 +271,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "anyOf", name: "anyOf",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
AnyOf: []v1beta1.JSONSchemaProps{ AnyOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"}, {Type: "boolean"},
{Type: "string"}, {Type: "string"},
}, },
@ -291,8 +289,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "not", name: "not",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Not: &v1beta1.JSONSchemaProps{ Not: &apiextensions.JSONSchemaProps{
Type: "boolean", Type: "boolean",
}, },
}, },
@ -303,10 +301,90 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
// }, // },
// }, // },
}, },
{
name: "nested logic",
in: &apiextensions.JSONSchemaProps{
AllOf: []apiextensions.JSONSchemaProps{
{
Not: &apiextensions.JSONSchemaProps{
Type: "boolean",
},
},
{
AnyOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{
OneOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{Type: "string"},
},
AnyOf: []apiextensions.JSONSchemaProps{
{
Not: &apiextensions.JSONSchemaProps{
Type: "boolean",
},
},
{
AnyOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{
OneOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{Type: "string"},
},
OneOf: []apiextensions.JSONSchemaProps{
{
Not: &apiextensions.JSONSchemaProps{
Type: "boolean",
},
},
{
AnyOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{
OneOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
{Type: "string"},
},
Not: &apiextensions.JSONSchemaProps{
Not: &apiextensions.JSONSchemaProps{
Type: "boolean",
},
AnyOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
OneOf: []apiextensions.JSONSchemaProps{
{Type: "boolean"},
{Type: "string"},
},
},
},
expected: new(spec.Schema).
WithAllOf(spec.Schema{}, spec.Schema{}, spec.Schema{}, *spec.StringProperty()),
},
{ {
name: "properties", name: "properties",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Properties: map[string]v1beta1.JSONSchemaProps{ Properties: map[string]apiextensions.JSONSchemaProps{
testStr: {Type: "boolean"}, testStr: {Type: "boolean"},
}, },
}, },
@ -315,10 +393,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "additionalProperties", name: "additionalProperties",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{ AdditionalProperties: &apiextensions.JSONSchemaPropsOrBool{
Allows: true, Allows: true,
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"}, Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
}, },
}, },
expected: &spec.Schema{ expected: &spec.Schema{
@ -332,8 +410,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "patternProperties", name: "patternProperties",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
PatternProperties: map[string]v1beta1.JSONSchemaProps{ PatternProperties: map[string]apiextensions.JSONSchemaProps{
testStr: {Type: "boolean"}, testStr: {Type: "boolean"},
}, },
}, },
@ -347,10 +425,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "dependencies schema", name: "dependencies schema",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Dependencies: v1beta1.JSONSchemaDependencies{ Dependencies: apiextensions.JSONSchemaDependencies{
testStr: v1beta1.JSONSchemaPropsOrStringArray{ testStr: apiextensions.JSONSchemaPropsOrStringArray{
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"}, Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
}, },
}, },
}, },
@ -366,9 +444,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "dependencies string array", name: "dependencies string array",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Dependencies: v1beta1.JSONSchemaDependencies{ Dependencies: apiextensions.JSONSchemaDependencies{
testStr: v1beta1.JSONSchemaPropsOrStringArray{ testStr: apiextensions.JSONSchemaPropsOrStringArray{
Property: []string{testStr2}, Property: []string{testStr2},
}, },
}, },
@ -385,10 +463,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "additionalItems", name: "additionalItems",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
AdditionalItems: &v1beta1.JSONSchemaPropsOrBool{ AdditionalItems: &apiextensions.JSONSchemaPropsOrBool{
Allows: true, Allows: true,
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"}, Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
}, },
}, },
expected: &spec.Schema{ expected: &spec.Schema{
@ -402,9 +480,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "definitions", name: "definitions",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Definitions: v1beta1.JSONSchemaDefinitions{ Definitions: apiextensions.JSONSchemaDefinitions{
testStr: v1beta1.JSONSchemaProps{Type: "boolean"}, testStr: apiextensions.JSONSchemaProps{Type: "boolean"},
}, },
}, },
expected: &spec.Schema{ expected: &spec.Schema{
@ -417,8 +495,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "externalDocs", name: "externalDocs",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
ExternalDocs: &v1beta1.ExternalDocumentation{ ExternalDocs: &apiextensions.ExternalDocumentation{
Description: testStr, Description: testStr,
URL: testStr2, URL: testStr2,
}, },
@ -428,7 +506,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
}, },
{ {
name: "example", name: "example",
in: &v1beta1.JSONSchemaProps{ in: &apiextensions.JSONSchemaProps{
Example: &testApiextensionsJSON, Example: &testApiextensionsJSON,
}, },
expected: new(spec.Schema). expected: new(spec.Schema).
@ -437,12 +515,14 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
out, err := ConvertJSONSchemaPropsToOpenAPIv2Schema(test.in) t.Run(test.name, func(t *testing.T) {
if err != nil { out, err := ConvertJSONSchemaPropsToOpenAPIv2Schema(test.in)
t.Errorf("unexpected error in converting openapi schema: %v", test.name) if err != nil {
} t.Fatalf("unexpected error in converting openapi schema: %v", err)
if !reflect.DeepEqual(out, test.expected) { }
t.Errorf("result of conversion test '%v' didn't match, want: %v; got: %v", test.name, *test.expected, *out) if !reflect.DeepEqual(*out, *test.expected) {
} t.Errorf("unexpected result:\n want=%v\n got=%v\n\n%s", *test.expected, *out, diff.ObjectDiff(*test.expected, *out))
}
})
} }
} }

View File

@ -24,7 +24,6 @@ import (
"github.com/go-openapi/spec" "github.com/go-openapi/spec"
autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv1 "k8s.io/api/autoscaling/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
@ -206,7 +205,7 @@ func scaleStatusSchema() *spec.Schema {
// NOTE: in apiserver we general operates on internal types. We are using versioned (v1beta1) // NOTE: in apiserver we general operates on internal types. We are using versioned (v1beta1)
// validation schema here because we need the json tags to properly marshal the object to // validation schema here because we need the json tags to properly marshal the object to
// JSON. // JSON.
func CustomResourceDefinitionOpenAPISpec(crdSpec *apiextensions.CustomResourceDefinitionSpec, version string, validationSchema *v1beta1.CustomResourceValidation) (*spec.Swagger, string, error) { func CustomResourceDefinitionOpenAPISpec(crdSpec *apiextensions.CustomResourceDefinitionSpec, version string, validationSchema *apiextensions.CustomResourceValidation) (*spec.Swagger, string, error) {
schema := &spec.Schema{} schema := &spec.Schema{}
if validationSchema != nil && validationSchema.OpenAPIV3Schema != nil { if validationSchema != nil && validationSchema.OpenAPIV3Schema != nil {
var err error var err error