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..7a1e9e988 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 ( @@ -51,6 +53,43 @@ const ( floatTag = "!!float" ) +var any = &MapItem{ + Key: "oneOf", + Value: []map[string]interface{}{ + { + typeKey: "integer", + defaultKey: nil, + nullableKey: true, + }, + { + typeKey: "number", + defaultKey: nil, + nullableKey: true, + }, + { + typeKey: "boolean", + defaultKey: nil, + nullableKey: true, + }, + { + typeKey: "string", + defaultKey: nil, + nullableKey: true, + }, + { + typeKey: "object", + defaultKey: nil, + nullableKey: true, + }, + { + typeKey: "array", + defaultKey: nil, + nullableKey: true, + itemsKey: map[string]string{}, + }, + }, +} + type MapItem struct { Key string Value interface{} @@ -211,10 +250,14 @@ func (h HelmValuesSchemaGen) calculateProperties(key *yaml3.Node, value *yaml3.N if err != nil { return nil, err } - 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 { - apiKeys = append(apiKeys, &MapItem{Key: formatKey, Value: floatVal}) + if value.Tag == nullTag { + apiKeys = append(apiKeys, any) + } else { + 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 { + apiKeys = append(apiKeys, &MapItem{Key: formatKey, Value: floatVal}) + } } case yaml3.AliasNode: return h.calculateProperties(key, value.Alias) @@ -266,7 +309,6 @@ func (h HelmValuesSchemaGen) openAPIType(tag, value string) string { } } return "string" - } func (h HelmValuesSchemaGen) getDefaultValue(tag, value string) (interface{}, error) { 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 {