fix(java): generate correct enum constructor when string format is uri#16929
fix(java): generate correct enum constructor when string format is uri#16929wing328 merged 1 commit intoOpenAPITools:masterfrom hhomar:enum-with-uri-format
Conversation
Before:
COM_V1_SCHEMA_JSON("https://example.com/v1/schema.json")
After:
COM_V1_SCHEMA_JSON(URI.create("https://example.com/v1/schema.json"),
|
I did a test with the spec you provided but got the following errors: Did you get the same issue in your environment? command: |
|
Hi I was testing with: java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java --library resttemplate -i /tmp/a.yaml -o /tmp/enumSorry, I wasn't aware that the enum model was generated differently depending on the library. I guess this should have gone to the Java Spring team. I've dug into it a bit and found the issue in the Java/modelEnum.mustache and Java/modelInnerEnum.mustache templates. I've updated the templates to check for isUri and now they generate the correct code: public static class Adapter extends TypeAdapter<DataSchemaEnum> {
@Override
public void write(final JsonWriter jsonWriter, final DataSchemaEnum enumeration) throws IOException {
jsonWriter.value(enumeration.getValue().toASCIIString());
}
@Override
public DataSchemaEnum read(final JsonReader jsonReader) throws IOException {
URI value = URI.create(jsonReader.nextString());
return DataSchemaEnum.fromValue(value);
}
}The modelEnum template update was a bit trickier as the isUri check was returning false. I had to add some additional code to the CodegenModel to check if the format is set as URI. I followed the same code as isUuid, but noticed a comment in DefaultCodeGen about UUID being a rare use case and may be removed in future DefaultCodegen.java#L3016, so I don't know if this change is the best approach. There'ss also a native/modelEnum.mustache template, but it looks like the serialization library is fixed to Jackson JavaClientCodegen.java#L633, so the incorrect code can never be generated. I've made all these changes on a separate branch (along with some tests). I'm not sure whether to pull those changes into this PR or raise as a separate PR? |
|
confirmed this fix the issue in
yes please |
Using the following spec:
The generated DataSchemaEnum is:
Causing the following compilation errors:
With the fix applied:
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)