diff --git a/internal/generate/types.go b/internal/generate/types.go index 9a65d71..1a3117b 100644 --- a/internal/generate/types.go +++ b/internal/generate/types.go @@ -490,14 +490,25 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin typeName = fmt.Sprintf("%s%s", name, strcase.ToCamel(k)) } - // When additional properties is set and is type array, it means - // the type will be a map. - // TODO correctness: Currently our API spec does not specify what - // type the key will be, so we set it to string to avoid errors. - // If the type of the key is defined in our spec in the future, - // this should be changed to reflect that type. - if isObjectArray(v) { - typeName = fmt.Sprintf("map[string][]%s", typeName) + // When `additionalProperties` is set, the type will be a map. + // See the spec for details: https://spec.openapis.org/oas/v3.0.3.html#x4-7-24-3-3-model-with-map-dictionary-properties. + // + // TODO correctness: Currently our API spec does not specify + // what type the key will be, so we set it to string to avoid + // errors. If the type of the key is defined in our spec in + // the future, this should be changed to reflect that type. + if v.Value.AdditionalProperties.Schema != nil { + if v.Value.AdditionalProperties.Schema.Value.Type.Is("array") { + // When `additionalProperties` has a schema of + // type "array", use a map of string to a slice + // of the nested type. + typeName = fmt.Sprintf("map[string][]%s", typeName) + } else { + // If the schema type isn't explicitly set to + // "array", use a map of string to the nested + // type. + typeName = fmt.Sprintf("map[string]%s", typeName) + } } // If a type is nullable we'll want a pointer diff --git a/internal/generate/utils.go b/internal/generate/utils.go index 609f54c..bd1e5f6 100644 --- a/internal/generate/utils.go +++ b/internal/generate/utils.go @@ -53,14 +53,6 @@ func isLocalObject(v *openapi3.SchemaRef) bool { return v.Ref == "" && v.Value.Type.Is("object") && len(v.Value.Properties) > 0 } -func isObjectArray(v *openapi3.SchemaRef) bool { - if v.Value.AdditionalProperties.Schema != nil { - return v.Value.AdditionalProperties.Schema.Value.Type.Is("array") - } - - return false -} - func isNullableArray(v *openapi3.SchemaRef) bool { return v.Value.Type.Is("array") && v.Value.Nullable } diff --git a/oxide/types.go b/oxide/types.go index c916b36..f87cedb 100644 --- a/oxide/types.go +++ b/oxide/types.go @@ -6248,7 +6248,7 @@ type Switch struct { // - Switch type SwitchBgpHistory struct { // History is message history indexed by peer address. - History BgpMessageHistory `json:"history,omitempty" yaml:"history,omitempty"` + History map[string]BgpMessageHistory `json:"history,omitempty" yaml:"history,omitempty"` // Switch is switch this message history is associated with. Switch SwitchLocation `json:"switch,omitempty" yaml:"switch,omitempty"` } @@ -6649,8 +6649,8 @@ type SystemMetricName string // - Name // - Timeseries type Table struct { - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Timeseries Timeseries `json:"timeseries,omitempty" yaml:"timeseries,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Timeseries map[string]Timeseries `json:"timeseries,omitempty" yaml:"timeseries,omitempty"` } // TargetRelease is view of a system software target release. @@ -6709,7 +6709,7 @@ type TimeAndIdSortMode string // - Fields // - Points type Timeseries struct { - Fields FieldValue `json:"fields,omitempty" yaml:"fields,omitempty"` + Fields map[string]FieldValue `json:"fields,omitempty" yaml:"fields,omitempty"` // Points is timepoints and values for one timeseries. Points Points `json:"points,omitempty" yaml:"points,omitempty"` }