I would like to generate java code (retrofit2 library) with java primitive types instead of java wrapper classes.
Integer -> int
Boolean -> boolean
Float -> float
2.2.1
ForbiddenResponse:
properties:
success:
type: boolean
default: false
data:
type: object
properties:
code:
type: integer
default: 0
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
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;
public class ForbiddenResponse {
@SerializedName("success")
private boolean success = false;
public class ForbiddenResponseData {
@SerializedName("code")
private int code = 0;
@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?
@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;
@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.
@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?
Actually there already seems to be a related issue in swagger-core: https://github.com/swagger-api/swagger-core/issues/2178
Thanks @capsur for the "_workaround_" and its explanation!
Most helpful comment
@luke83 @fehguy @wing328 This issue exactly doesn't seem to be related to swagger codegen, the problem is that
BaseIntegerProperty(type: integerwithoutformat) 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.javaFor example:
will lead to:
Do you know why
BaseIntegerPropertydoesn't support default values?