swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

default for Primitive types instead of wrapper classes

Open lucatrev opened this issue 9 years ago • 7 comments

Description

I would like to generate java code (retrofit2 library) with java primitive types instead of java wrapper classes.

Integer -> int Boolean -> boolean Float -> float

Swagger-codegen version

2.2.1

Swagger declaration file content or url
  ForbiddenResponse:
    properties:
      success:
        type: boolean
        default: false
      data:
        type: object
        properties:
          code:
            type: integer
            default: 0
Command line used for generation
java
  -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar  
    generate 
      -i path/to/api 
      -l java 
      -c retrofit2.json 
      -o path/to/out
      --language-specific-primitives boolean,int,float 
      --type-mappings Integer=int,Boolean=boolean,Float=float

Generated code:

Default for boolean primitive is correctly handled with

public class ForbiddenResponse {
  @SerializedName("success")
  private boolean success = false;

Default for int primitive is not correctly handler (null instead of 0, set as default)

public class ForbiddenResponseData {
  @SerializedName("code")
  private int code = null;

Desired code:

public class ForbiddenResponse {
  @SerializedName("success")
  private boolean success = false;
public class ForbiddenResponseData {
  @SerializedName("code")
  private int code = 0;

lucatrev avatar Dec 30 '16 18:12 lucatrev

@luke83 thanks for the feedback. It's definitely reasonable to use primitive type instead of wrapper class. In your swagger/openapi spec, have you set a proper default value for the property?

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/pojo.mustache#L30

wing328 avatar Jan 04 '17 08:01 wing328

@wing328 thanks for your response. I have an integer property (code) and i set its default this way (this is a snippet):

  ForbiddenResponse:
    properties:
      success:
        type: boolean
        default: false
      data:
        type: object
        properties:
          code:
            type: integer
            default: 0

Reading the spec i found only references to default, is the definition above correct for code property? The definition above give me null instead of 0 for property code, same result if i move code definition one level up, directly inside the properties of ForbiddenResponse (model/pojo) definition - only to exclude it is not a "nesting" problem.

Another (simpler) example:

  Competition:
    properties:
      id:
        type: integer
        default: 0

will generate:

public class Competition {
  @SerializedName("id")
  private int id = null;

instead of:

public class Competition {
  @SerializedName("id")
  private int id = 0;

lucatrev avatar Jan 04 '17 08:01 lucatrev

@luke83 it'll be hard to do this within the generator because of how we treat nulls, etc. I'd suggest looking into sending a PR to add this.

fehguy avatar Feb 03 '17 22:02 fehguy

@luke83 @fehguy @wing328 This issue exactly doesn't seem to be related to swagger codegen, the problem is that BaseIntegerProperty (type: integer without format) doesn't include support for default values: https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/models/properties/BaseIntegerProperty.java

For example:

Competition:
    properties:
      id:
        type: integer
        format: int32
        default: 0

will lead to:

public class Competition {
  @SerializedName("id")
  private int id = 0;

Do you know why BaseIntegerProperty doesn't support default values?

capsur avatar Feb 28 '18 19:02 capsur

Actually there already seems to be a related issue in swagger-core: https://github.com/swagger-api/swagger-core/issues/2178

capsur avatar Feb 28 '18 19:02 capsur

Thanks @capsur for the "workaround" and its explanation!

lucatrev avatar Mar 01 '18 08:03 lucatrev

any update on this?

nalmeida84 avatar Jul 26 '23 12:07 nalmeida84