-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[JAVA] Manage List<Integer> datatype for enum #75
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you a lot for this contribution.
In general I do not think that re-parsing the type to get the type defined in the generic is a good approach. We have all the information we need, we just need to see how we can pass it to the method.
* @return the contained datatype if datatype is List | ||
*/ | ||
private String sanitizeForEnum(String datatype){ | ||
Pattern p = Pattern.compile("List<(.*)>"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work with array of array.
@@ -1036,19 +1037,34 @@ public String toEnumVarName(String value, String datatype) { | |||
|
|||
@Override | |||
public String toEnumValue(String value, String datatype) { | |||
if ("Integer".equals(datatype) || "Double".equals(datatype)) { | |||
String sanitzedDataType = sanitizeForEnum(datatype); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who is calling this method?
I did not check it yet, I think we could add a parameter and provide the mostInnerItems type (will be added with #66)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method is called into updateCodegenPropertyEnum.
I've done a second try where I search the most inner datatype on this method, so I think you're right, I can replace my call to findMostInnerDatatype method with a simple var.mostInnerItems.datatype with #66 .
This is a very nice contribution. Thank you a lot. I have used this spec to test it: openapi: 3.0.1
info:
title: OpenAPI Tree Pots
description: Example spec
version: 1.0.0
servers:
- url: 'http://api.company.xyz/v2'
paths:
/pull75:
get:
operationId: op
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/ObjWithEnums'
components:
schemas:
ObjWithEnums:
type: object
properties:
IProp:
$ref: "#/components/schemas/IntEnum"
LProp:
$ref: "#/components/schemas/LongEnum"
SProp:
$ref: "#/components/schemas/StringEnum"
IntEnum:
type: integer
format: int32
enum:
- 1
- 2
- 3
LongEnum:
type: integer
format: int64
enum:
- 20
- 30
- 40
StringEnum:
type: string
enum:
- "c"
- "b"
- "a" With the java generator (directly no CLI or maven):
And I get this compile error:
Corresponding code: public static class Adapter extends TypeAdapter<IntEnum> {
@Override
public void write(final JsonWriter jsonWriter, final IntEnum enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public IntEnum read(final JsonReader jsonReader) throws IOException {
Integer value = jsonReader.nextInteger(); //HERE ERROR, SHOULD BE "nextInt()"
return IntEnum.fromValue(String.valueOf(value));
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice contribution. I am looking forward being able to merge it.
We need to find a solution for the compilation error that is created (see previous comment)
I also found an import that now can be removed.
If you need help, let us know
@@ -50,6 +50,7 @@ | |||
import java.util.List; | |||
import java.util.ListIterator; | |||
import java.util.Map; | |||
import java.util.regex.Matcher; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an unused import
I've added the missing "isInteger" to the CodegenModel, but I'm not sure it's the right solution because it adds a java enum specific requirement into a generic model. |
This is not a problem, in my opinion we should rework the Codegen classes to have more stuff in common ( But I am not sure that the way you have implemented it will work. You should be aware that each language is allowed to change the value in |
Ok, I've had it as property. Maybe dataType must be wrap into a valueObject during #20 to simplify the model and avoid having the possibility of an Integer that respond "false" to isInteger. |
Thank you for the changes. I tested it with OAS3 spec mentioned in my comment, and I got several problems. Java client (library public static class Adapter extends TypeAdapter<LongEnum> {
@Override
public void write(final JsonWriter jsonWriter, final LongEnum enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public LongEnum read(final JsonReader jsonReader) throws IOException {
Long value = jsonReader.nextInt(); //COMPILE ERROR
return LongEnum.fromValue(String.valueOf(value));
}
} I did not look into the code, but maybe you will need Server (generator name I got a lot of compile error for @XmlType(name="")
@XmlEnum(Long.class)
public enum LongEnum {
@XmlEnumValue(20l) NUMBER_20(Long.valueOf(20l)), @XmlEnumValue(30l) NUMBER_30(Long.valueOf(30l)), @XmlEnumValue(40l) NUMBER_40(Long.valueOf(40l));
private Long value;
LongEnum(Long v) {
value = v;
}
public String value() { //RETURN TYPE SHOULD BE 'Long'
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static LongEnum fromValue(String v) { //PARAMETER TYPE SHOULD BE 'Long'
for (LongEnum b : LongEnum.values()) {
if (String.valueOf(b.value).equals(v)) { //I THINK, SHOULD BE 'Long.valueOf(..)' ?
return b;
}
}
return null;
}
} |
Fixed for java, jaxrs-cxf-cdi and kotlin. |
I will have a look at your changes later today. Thank you a lot. |
An addition on
Is that allowed for a patch release ( In my opinion it can be added, but I am not sure about the change policy. |
@jmini I wouldn't consider this a breaking change unless it would cause custom templates with by users to break (as far as I can tell, that's not the case). I do think it's weird adding Java specific code to the shared CodegenModel. When I've had to do things like this in Kotlin and C#, I've done it on those generators base types as a vendor extension. Putting it on CodegenModel means all templates can consume it and making changes will be difficult. I would recommend that to keep this change, the public fields should be getter/setters. |
@Zomzog can you share a minimal spec to reproduce the issue? I may have a better approach to address the problem |
@wing328 : what is wrong with the one I have published as comment in this PR: #75 (comment) You have an enum for String (to ensure no regression), for Long and and for Integer. I did not checked the last changes here, but each time I look at the changes @Zomzog is getting closer to the result. Let me perform some tests before I can approve this PR, but the work so far looks good to me.
This is not java specific at all... It is valid OAS and needs potentially to be implemented everywhere. @Zomzog did a great job by implementing:
Other language maintainers interested in such a feature will need to pick the new exposed stuff in their templates. @jimschubert: thank you for your definition of a breaking change. As this is an addition only, we will be able to merge it on |
@jmini looking at the generated code, I'd consider this a breaking change as users will no longer be able to generate Kotlin code without modifying existing code. |
@jmini thanks for the pointer. I'll use that to repeat the issue. |
I have verified this change, I think it now works well. Thank you a lot for this contribution. As discussed earlier here, I propose to merge it on
This change is well covered with unit tests, you can still improve the implementation later. |
Conflicts: modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
.Description of the PR
Add method call for manage datatype List<> when creating enum value in java.
Must fix #49.
Technical committee:
@bbdouglas (2017/07) @JFCote (2017/08) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01)