Skip to content

Commit 8d42079

Browse files
committed
generate: handle map[string]any types correctly.
When an openapi field includes an `additionalProperties` key, it represents a map to the type specified in the schema reference. We already correctly handle the special case of mapping a string to a slice of the referenced type as a `map[string][]type`, but not the case where the nested type isn't a slice. This patch adds logic for the missing `map[string[type` case.
1 parent d5356df commit 8d42079

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

internal/generate/types.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,24 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
490490
typeName = fmt.Sprintf("%s%s", name, strcase.ToCamel(k))
491491
}
492492

493-
// When additional properties is set and is type array, it means
494-
// the type will be a map.
495-
// TODO correctness: Currently our API spec does not specify what
496-
// type the key will be, so we set it to string to avoid errors.
497-
// If the type of the key is defined in our spec in the future,
498-
// this should be changed to reflect that type.
499-
if isObjectArray(v) {
500-
typeName = fmt.Sprintf("map[string][]%s", typeName)
493+
// When `additionalProperties` is set, the type will be a map.
494+
//
495+
// TODO correctness: Currently our API spec does not specify
496+
// what type the key will be, so we set it to string to avoid
497+
// errors. If the type of the key is defined in our spec in
498+
// the future, this should be changed to reflect that type.
499+
if v.Value.AdditionalProperties.Schema != nil {
500+
if v.Value.AdditionalProperties.Schema.Value.Type.Is("array") {
501+
// When `additionalProperties` has a schema of
502+
// type "array", use a map of string to a slice
503+
// of the nested type.
504+
typeName = fmt.Sprintf("map[string][]%s", typeName)
505+
} else {
506+
// If the schema type isn't explicitly set to
507+
// "array", use a map of string to the nested
508+
// type.
509+
typeName = fmt.Sprintf("map[string]%s", typeName)
510+
}
501511
}
502512

503513
// If a type is nullable we'll want a pointer

internal/generate/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ func isLocalObject(v *openapi3.SchemaRef) bool {
5353
return v.Ref == "" && v.Value.Type.Is("object") && len(v.Value.Properties) > 0
5454
}
5555

56-
func isObjectArray(v *openapi3.SchemaRef) bool {
57-
if v.Value.AdditionalProperties.Schema != nil {
58-
return v.Value.AdditionalProperties.Schema.Value.Type.Is("array")
59-
}
60-
61-
return false
62-
}
63-
6456
func isNullableArray(v *openapi3.SchemaRef) bool {
6557
return v.Value.Type.Is("array") && v.Value.Nullable
6658
}

oxide/types.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)