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 []
.
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>();
And the problem is if a field is required them I want to mark the request as BadRequest when the key is not available, instead of assigning an empty array to it.
Can anyone tell me how to make an array use the default []
without being required? and how can I make sure that my required marked array item raises a BadRequest instead of assigning default []
to the property?
question from:
https://stackoverflow.com/questions/65875102/empty-array-not-getting-picked-as-default-for-the-defined-model-in-open-api-aka 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…