swagger-api / swagger-api/swagger-codegen
[JAVA] Empty array not getting picked as default for the defined model in Open API AKA Swagger generated code
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
While designing the API, I am defining a UserGeo model that contains two fields - domain (string array) and country (string array).
An empty List [] should be used in the request body if no value is provided for the domain.
But on defining the default property as [] the generates Spring code does not assign empty ArrayList<> aka [].
Swagger-codegen - Same as that on swagger.io
Swagger declaration file content or url
Attaching the code samples:
Swagger Definition:
definitions:
UserGeo:
type: "object"
properties:
country:
type: "array"
items:
type: "string"
domain:
type: "array"
items:
type: "string"
default: []
This does not default the domain value to an empty list, check this generated Spring/Java code:
.
.
public class Classification {
@JsonProperty("country")
@Valid
private List<String> country = null;
@JsonProperty("domain")
@Valid
private List<String> domain = null;
.
.
Whereas when I define the domain field as required without even defining it with default, the generated code assigns an empty list as default for the domain.
definitions:
UserGeo:
type: "object"
required:
- "domain"
properties:
country:
type: "array"
items:
type: "string"
domain:
type: "array"
items:
type: "string"
And as mentioned the generated code has an empty list assigned for the domain.
public class UserGeo {
@JsonProperty("country")
@Valid
private List<String> country = null;
@JsonProperty("domain")
@Valid
private List<String> domain = new ArrayList<String>();
Command line used for generation
Steps to reproduce
Go to https://editor.swagger.io/ , paste the YAML in definitions and generate server code -> spring
Suggest a fix/enhancement
If a field is required in the model (even of type array), the generated code should not automatically assign an empty array when the field is missing.
And defaults should only be used when they are specifically asked to.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied OpenAPI definition and compare the generated Spring/Java model for an array with default: [] against one where the array is required. Clarify the intended distinction between explicit defaults and required fields, then verify that generated model initialization follows that decision.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100