Skip to content

[Java] Bug: boolean getter used for non-primitive Boolean #7261

@macjohnny

Description

@macjohnny
Description

When generating a model from a swagger file with

  myProperty:
    type: boolean

the generated model file will contain

  @JsonProperty("myProperty")
  private Boolean myProperty= null;

  @ApiModelProperty()
  public Boolean isMyProperty() {
    return myProperty;
  }

  public void setMyProperty(Boolean myProperty) {
    this.myProperty= myProperty;
  }

The problematic line is the following:

  public Boolean isMyProperty() {

since the JavaBeans Specification - section 8.3.2 states that

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This “is” method may be provided instead of a “get” method,
or it may be provided in addition to a “get” method.

And in the example above, the isMyProperty() method is generated for a non-primitive Boolean property, which should not be the case.

First, this is a breaking change that was introduced with 2.3.0, which could easily have been avoided by adding the isMyProperty() in addition to a getMyProperty() method.

Second, some bean property mappers may not recognized the property as a readable attribute, e.g.
in the Spring BeanWrapper: https://jira.spring.io/browse/SPR-9482

This change was introduced in #6177

Swagger-codegen version

affected: 2.3.0
type: regression, used to work in 2.2.3

Related issues/PRs

#6177

Suggest a fix/enhancement

I suggest to have the isMyProperty() method in addition to the getMyProperty() method instead of replacing it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions