Skip to content

Commit e5cf274

Browse files
committed
Handle nullable array items.
Openapi represents an array of nullable items using an allOf with exactly one reference entry (items.allOf[0].$ref), rather than the usual items.$ref. We currently treat these types as any, which means users have to cast or marshal and unmarshal data to the correct type. This patch adds a check for this edge case and replaces the any with the appropriate concrete type.
1 parent 1866144 commit e5cf274

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

internal/generate/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ func convertToValidGoType(property, typeName string, r *openapi3.SchemaRef) stri
144144
}
145145

146146
func schemaValueToGoType(schemaValue *openapi3.Schema, property string) string {
147+
// In the special case of an array of nullable items, we represent the
148+
// item type with an allOf of length 1. In this case, return the schema
149+
// of the 0th entry in anyOf.
150+
if len(schemaValue.AllOf) == 1 {
151+
value := schemaValue.AllOf[0]
152+
reference := getReferenceSchema(value)
153+
if reference == "" {
154+
fmt.Printf("[WARN] TODO: handle allOf %+v for %q, marking as any for now\n", value, property)
155+
return "any"
156+
}
157+
if schemaValue.Nullable {
158+
reference = fmt.Sprintf("*%s", reference)
159+
}
160+
return reference
161+
}
162+
147163
if schemaValue.Type == nil {
148164
fmt.Printf("[WARN] TODO: handle nil type for %q, marking as any for now\n", property)
149165
return "any"

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)