Skip to content

Commit

Permalink
Treat null value as any type when generating schema from Helm chart
Browse files Browse the repository at this point in the history
fixes #1630

Signed-off-by: Max Brauer <mbrauer@vmware.com>
  • Loading branch information
mamachanko committed Jan 23, 2025
1 parent a1c5d04 commit 668024c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
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

0 comments on commit 668024c

Please sign in to comment.