diff --git a/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen.go b/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen.go index 373af5f9e..4a89f6c8d 100644 --- a/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen.go +++ b/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen.go @@ -26,6 +26,7 @@ const ( itemsKey = "items" propertiesKey = "properties" defaultKey = "default" + nullableKey = "nullable" ) var keyOrder = map[string]int{ @@ -35,6 +36,7 @@ var keyOrder = map[string]int{ itemsKey: 4, propertiesKey: 5, defaultKey: 6, + nullableKey: 7, } const ( @@ -211,6 +213,11 @@ func (h HelmValuesSchemaGen) calculateProperties(key *yaml3.Node, value *yaml3.N if err != nil { return nil, err } + if value.Tag == nullTag { + // We cannot infer a key's type from a null value and must assume "any". + apiKeys = append(apiKeys, newAnyType()) + break + } apiKeys = append(apiKeys, &MapItem{Key: typeKey, Value: h.openAPIType(value.Tag, value.Value)}) apiKeys = append(apiKeys, &MapItem{Key: defaultKey, Value: defaultVal}) if value.Tag == floatTag { @@ -266,7 +273,6 @@ func (h HelmValuesSchemaGen) openAPIType(tag, value string) string { } } return "string" - } func (h HelmValuesSchemaGen) getDefaultValue(tag, value string) (interface{}, error) { @@ -281,3 +287,29 @@ func (h HelmValuesSchemaGen) getDefaultValue(tag, value string) (interface{}, er return value, nil } } + +func newAnyType() *MapItem { + nullable := func(t string) map[string]interface{} { + n := map[string]interface{}{ + typeKey: t, + defaultKey: nil, + nullableKey: true, + } + if t == "array" { + n["items"] = map[string]string{} + } + return n + } + return &MapItem{ + Key: "oneOf", + Value: []map[string]interface{}{ + nullable("integer"), + nullable("number"), + nullable("boolean"), + nullable("string"), + nullable("object"), + nullable("array"), + }, + } +} + diff --git a/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen_test.go b/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen_test.go index fb0d8446c..58c7c4672 100644 --- a/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen_test.go +++ b/cli/pkg/kctrl/cmd/package/release/schemagenerator/helm_openapi_schema_gen_test.go @@ -69,7 +69,8 @@ arrKeyEmpty: [] type: string type: array type: object -`}, +`, + }, { name: "object with different values", input: ` @@ -123,7 +124,8 @@ objExample: {} description: Object example type: object type: object -`}, +`, + }, { name: "nested complex object", input: ` @@ -176,7 +178,8 @@ containers: type: string type: object type: object -`}, +`, + }, { name: "Alias Node", input: ` @@ -223,7 +226,40 @@ aliasEx: type: object type: array type: object -`}, +`, + }, + { + name: "unknown type", + input: ` +# a field without a type +anything: null +`, + want: `properties: + anything: + description: a field without a type + oneOf: + - default: null + nullable: true + type: integer + - default: null + nullable: true + type: number + - default: null + nullable: true + type: boolean + - default: null + nullable: true + type: string + - default: null + nullable: true + type: object + - default: null + items: {} + nullable: true + type: array +type: object +`, + }, } for _, test := range tests { diff --git a/cli/test/e2e/package_authoring_e2e_test.go b/cli/test/e2e/package_authoring_e2e_test.go index 99986c9f8..92684dd58 100644 --- a/cli/test/e2e/package_authoring_e2e_test.go +++ b/cli/test/e2e/package_authoring_e2e_test.go @@ -336,8 +336,26 @@ spec: default: quay.io/mongodb type: string imagePullSecrets: - default: "" - type: string + oneOf: + - default: null + nullable: true + type: integer + - default: null + nullable: true + type: number + - default: null + nullable: true + type: boolean + - default: null + nullable: true + type: string + - default: null + nullable: true + type: object + - default: null + items: {} + nullable: true + type: array initAppDb: default: quay.io/mongodb type: string