oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Boilerplate for nullable additional properties is invalid

Open D4isDAVID-island opened this issue 1 year ago • 1 comments

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.

D4isDAVID-island avatar Nov 10 '24 09:11 D4isDAVID-island

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 :)

jerrysdev avatar Oct 07 '25 21:10 jerrysdev