oapi-codegen
oapi-codegen copied to clipboard
Boilerplate for nullable additional properties is invalid
Given a schema such as:
"MyModel": {
"type": "object",
"additionalProperties": {
"nullable": true
},
"properties": {
"blabla": {
"type": "string"
}
}
}
This type will be generated:
// MyModel defines model for MyModel.
type MyModel struct {
Blabla *string `json:"blabla,omitempty"`
AdditionalProperties map[string]*interface{} `json:"-"`
}
As seen above, additional properties is generated as map[string]*interface{}. However, the boilerplate generated via additional-properties.tmpl assumes interface{} and map[string]interface{}.
For example:
// Setter for additional properties for MyModel
func (a *MyModel) Set(fieldName string, value interface{}) {
if a.AdditionalProperties == nil {
a.AdditionalProperties = make(map[string]interface{})
}
a.AdditionalProperties[fieldName] = value
}
This code is 100% valid for non-nullable additional properties, however nullable additional properties don't have valid boilerplate.
Ran into this as well. Sidestepped by pre-processing the JSON spec and setting additionalProperties to false to avoid having to correct code after every generation, and being able to have CI checks. Would be nice to have it fixed though :)