Skip to content

Commit

Permalink
WIP treat null type as any
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 22, 2025
1 parent 7442d92 commit 2e22d0d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 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 All @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -266,7 +309,6 @@ func (h HelmValuesSchemaGen) openAPIType(tag, value string) string {
}
}
return "string"

}

func (h HelmValuesSchemaGen) getDefaultValue(tag, value string) (interface{}, error) {
Expand Down
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 2e22d0d

Please sign in to comment.