Skip to content

Commit 3e0eed0

Browse files
authored
generate: handle map[string]any types correctly. (#325)
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 3e0eed0

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

internal/generate/types.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,25 @@ 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+
// See the spec for details: https://spec.openapis.org/oas/v3.0.3.html#x4-7-24-3-3-model-with-map-dictionary-properties.
495+
//
496+
// TODO correctness: Currently our API spec does not specify
497+
// what type the key will be, so we set it to string to avoid
498+
// errors. If the type of the key is defined in our spec in
499+
// the future, this should be changed to reflect that type.
500+
if v.Value.AdditionalProperties.Schema != nil {
501+
if v.Value.AdditionalProperties.Schema.Value.Type.Is("array") {
502+
// When `additionalProperties` has a schema of
503+
// type "array", use a map of string to a slice
504+
// of the nested type.
505+
typeName = fmt.Sprintf("map[string][]%s", typeName)
506+
} else {
507+
// If the schema type isn't explicitly set to
508+
// "array", use a map of string to the nested
509+
// type.
510+
typeName = fmt.Sprintf("map[string]%s", typeName)
511+
}
501512
}
502513

503514
// 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)