Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

Empty array not getting picked as default for the defined model in Open API AKA Swagger generated code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...