-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faulty Transformation of Element with Type Boolean + Enum #10546
Comments
In addition: A similar problem exists when using the Number:
type: number
description: numbers
enum:
- 0
- 1
- 2
- 3 The public enum Number {
_0("0"),
_1("1"),
_2("2"),
_3("3");
private BigDecimal value;
Number(BigDecimal value) {
this.value = value;
}
public BigDecimal getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static Number fromValue(String text) {
for (Number b : Number.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
public static class Adapter extends TypeAdapter<Number> {
@Override
public void write(final JsonWriter jsonWriter, final Number enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public Number read(final JsonReader jsonReader) throws IOException {
BigDecimal value = jsonReader.nextBigDecimal();
return Number.fromValue(String.valueOf(value));
}
}
} Again, this is not a valid Java code. The number values are interpreted as string values, whereas the constructor request a value of type And, a similar picture is when using the Integer:
type: integer
description: integers
enum:
- 0
- 1
- 2
- 3 public enum Integer {
NUMBER_0(0),
NUMBER_1(1),
NUMBER_2(2),
NUMBER_3(3);
private Integer value;
Integer(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static Integer fromValue(String text) {
for (Integer b : Integer.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
public static class Adapter extends TypeAdapter<Integer> {
@Override
public void write(final JsonWriter jsonWriter, final Integer enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public Integer read(final JsonReader jsonReader) throws IOException {
Integer value = jsonReader.nextInteger();
return Integer.fromValue(String.valueOf(value));
}
}
} Here, |
Unfortunately, the provided fix does not solve the problem! The generated Java code still looks as follows: /**
* True or False indicator
*/
@JsonAdapter(ModelBoolean.Adapter.class)
public enum ModelBoolean {
TRUE("true"),
FALSE("false");
private Boolean value;
ModelBoolean(Boolean value) {
this.value = value;
}
public Boolean getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static ModelBoolean fromValue(String text) {
for (ModelBoolean b : ModelBoolean.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
public static class Adapter extends TypeAdapter<ModelBoolean> {
@Override
public void write(final JsonWriter jsonWriter, final ModelBoolean enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public ModelBoolean read(final JsonReader jsonReader) throws IOException {
Boolean value = jsonReader.nextBoolean();
return ModelBoolean.fromValue(String.valueOf(value));
}
}
} This is not a valid Java code. The constructor requires a value of type Boolean Tested with Swagegr Codegen Version 2.4.18 and version 3.0.24. Both sould include the fix. |
Description
We are using an API defining a Boolean-like-element build upon an enum containing boolean values true and false. The element is defined as follows:
In general,
true
andfalse
are interpreted as the boolean values true and false. However, the code-generated generates the following code fragment:Here,
true
andfalse
are interpreted as string values, whereas the constructor ofModelBoolean
requests a Boolean value. In short, this is not a valid Java code.Note: A similar result appears when using JavaScript as the target language.
Swagger-codegen version
Version 2.4.17 (swagger-codegen-maven-plugin) or SwaggerHub
Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
The text was updated successfully, but these errors were encountered: