Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat null value as any type when generating schema from Helm chart #1677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
itemsKey = "items"
propertiesKey = "properties"
defaultKey = "default"
nullableKey = "nullable"
)

var keyOrder = map[string]int{
Expand All @@ -35,6 +36,7 @@ var keyOrder = map[string]int{
itemsKey: 4,
propertiesKey: 5,
defaultKey: 6,
nullableKey: 7,
}

const (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -266,7 +273,6 @@ func (h HelmValuesSchemaGen) openAPIType(tag, value string) string {
}
}
return "string"

}

func (h HelmValuesSchemaGen) getDefaultValue(tag, value string) (interface{}, error) {
Expand All @@ -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"),
},
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ arrKeyEmpty: []
type: string
type: array
type: object
`},
`,
},
{
name: "object with different values",
input: `
Expand Down Expand Up @@ -123,7 +124,8 @@ objExample: {}
description: Object example
type: object
type: object
`},
`,
},
{
name: "nested complex object",
input: `
Expand Down Expand Up @@ -176,7 +178,8 @@ containers:
type: string
type: object
type: object
`},
`,
},
{
name: "Alias Node",
input: `
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 20 additions & 2 deletions cli/test/e2e/package_authoring_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading