Open RPGillespie6 opened 3 weeks ago
I was able to workaround the issue with:
func removeSchemaNulls(schema map[string]any) error {
if _, ok := schema["$ref"].(string); ok {
return nil
}
typeArray, ok := schema["type"].([]any)
if ok {
fmt.Println("ding ding ding, ", typeArray)
actualType := ""
for _, t := range typeArray {
tStr := t.(string)
if tStr != "null" {
actualType = tStr
break
}
}
if actualType == "" {
return fmt.Errorf("missing non-null type: %v", schema["type"])
}
schema["type"] = actualType
schema["nullable"] = true
}
componentType, ok := schema["type"].(string)
if !ok {
return fmt.Errorf("invalid type: %v", componentType)
}
switch componentType {
case "object":
return objectRemoveSchemaNulls(schema)
case "array":
return arrayRemoveSchemaNulls(schema)
default:
return nil
}
}
func objectRemoveSchemaNulls(schema map[string]any) error {
properties, ok := schema["properties"].(map[string]any)
if !ok {
properties = map[string]any{}
}
for property, propItem := range properties {
propSchema, ok := propItem.(map[string]any)
if !ok {
return fmt.Errorf("invalid property schema: %v", propItem)
}
err := removeSchemaNulls(propSchema)
if err != nil {
return fmt.Errorf("%s: %v", property, err)
}
}
return nil
}
func arrayRemoveSchemaNulls(schema map[string]any) error {
items, ok := schema["items"].(map[string]any)
if !ok {
return fmt.Errorf("missing items: %v", schema["items"])
}
err := removeSchemaNulls(items)
if err != nil {
return err
}
return nil
}
Run the following code:
Expected result:
Actual result:
See: https://swagger.io/docs/specification/data-models/data-types/#null