swagger-api / swagger-api/swagger-codegen

default for Primitive types instead of wrapper classes

Open
#4,475 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java General: Suggestion help wanted
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied Swagger declaration, retrofit2 configuration, and generation command, then inspect the Java generation templates and primitive type-mapping logic. Compare the boolean and integer default handling; done means generated primitive integer fields use 0 instead of null while boolean defaults remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.