swagger-api / swagger-codegen-generators

Apache License 2.0
280 stars 418 forks source link

`@NotNull` is added by default for fields that are not required #1295

Open yeikel opened 1 month ago

yeikel commented 1 month ago

Given the following step:

Swagger Definition:


address_start_date:
        type: string
        format: 'date-time'
        example: "2011-12-03 10:15:30"
        description: |
          Start Date for address

swagger-codegen-maven-plugin 3.0.56 generates the following model:

   /**
   * Start Date for address 
   * @return addressStartDate
  **/
  @Valid
  @Schema(description = "Start Date for address ")
  public LocalDateTime getAddressStartDate() {
    return addressStartDate;
  }

While swagger-codegen-maven-plugin 3.0.57 generates this:


   /**
   * Start Date for address 
   * @return addressStartDate
  **/
  @NotNull // <--- This is added by default
  @Valid
  @Schema(description = "Start Date for address ")
  public LocalDateTime getAddressStartDate() {
    return addressStartDate;
  }

It seems that this is what introduced with https://github.com/swagger-api/swagger-codegen-generators/pull/1291

lukw4l commented 3 days ago

I found out that you can restore the old behavior (no @NotNull annotation for not explicitly required fields) if you set the new configOption useNullableForNotNull to false.

The description of this option is "Add @NotNull depending on nullable property instead of required" and is set to true by default. It seems to completely ignore if a field is required or not.

@frantuma would it be enough to change the default to false so one can enable this feature if desired or should this new option be enhanced to work correctly (from my understanding) with not required as well as nullable fields?