Swagger-codegen: [Java] Bug: boolean getter used for non-primitive Boolean

Created on 27 Dec 2017  ·  12Comments  ·  Source: swagger-api/swagger-codegen

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 https://github.com/swagger-api/swagger-codegen/pull/6177

Swagger-codegen version

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

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/pull/6177

Suggest a fix/enhancement

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

Java Bug

Most helpful comment

I plan to add the "getMyProperty" method.

All 12 comments

cc @wing328

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

Your suggestion sounds good to me. I didn't realize it would break Spring BeanWrapper usage.

Please file a PR so that we can review.

@wing328 would it be possible to revert the change at first instance and release a patch version 2.3.1 and later on implement the additional isMyProperty() method, since adding the additional method requires some work

Likely we will need to release 2.3.1 as we just found out it's not working for Java7 (https://github.com/swagger-api/swagger-codegen/issues/7259).

I can think of a quick fix. If you've not started, I'll give it a try tonight, ok?

@wing328 ok, I will wait for your feedback. so you will revert the method naming?

I plan to add the "getMyProperty" method.

@wing328 excellent

cc @bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet

Sorry too busy. Will give it another try tomorrow and update you guys.

@wing328 could you please have a look at this one again?

👍 for me. Either add getXXX or revert the change to isXXX

@macjohnny sorry for the delay. I've filed https://github.com/swagger-api/swagger-codegen/pull/7344. Please take a look.

@cbornet my proposed solution allows users to customize the templates to use "is", "get", "has", etc for boolean property

Was this page helpful?
0 / 5 - 0 ratings