Skip to content

Commit 6ee94d6

Browse files
xtroceSebastian Droeppelmann
andauthored
issue-20718 fixed code generation for jersey2/3 and okhttp-gson when using modelMapping with a package and anyOf/oneOf (#20721)
Co-authored-by: Sebastian Droeppelmann <[email protected]>
1 parent bdb063f commit 6ee94d6

File tree

10 files changed

+218
-35
lines changed

10 files changed

+218
-35
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ public void processOpts() {
667667
additionalProperties.put("removeAnnotations", (Mustache.Lambda) (fragment, writer) -> {
668668
writer.write(removeAnnotations(fragment.execute()));
669669
});
670+
additionalProperties.put("sanitizeDataType", (Mustache.Lambda) (fragment, writer) -> {
671+
writer.write(sanitizeDataType(fragment.execute()));
672+
});
670673
}
671674

672675
/**
@@ -1843,6 +1846,22 @@ public String removeAnnotations(String dataType) {
18431846
return dataType;
18441847
}
18451848

1849+
/**
1850+
* Sanitize the datatype.
1851+
* This will remove all characters except alphanumeric ones.
1852+
* It will also first use {{@link #removeAnnotations(String)}} to remove the annotations added to the datatype
1853+
* @param dataType the data type string
1854+
* @return the data type string without annotations and any characters except alphanumeric ones
1855+
*/
1856+
public String sanitizeDataType(String dataType) {
1857+
String content = removeAnnotations(dataType);
1858+
if (content != null && content.matches(".*\\P{Alnum}.*")) {
1859+
content = content.replaceAll("\\P{Alnum}", "");
1860+
}
1861+
return content;
1862+
}
1863+
1864+
18461865
@Override
18471866
public ModelsMap postProcessModels(ModelsMap objs) {
18481867
// recursively add import for mapping one type to multiple imports

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/anyof_model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
218218
* @return The actual instance of `{{{dataType}}}`
219219
* @throws ClassCastException if the instance is not `{{{dataType}}}`
220220
*/
221-
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
221+
public {{{dataType}}} get{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}() throws ClassCastException {
222222
return ({{{dataType}}})super.getActualInstance();
223223
}
224224

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/oneof_model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
280280
* @return The actual instance of `{{{dataType}}}`
281281
* @throws ClassCastException if the instance is not `{{{dataType}}}`
282282
*/
283-
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
283+
public {{{dataType}}} get{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}() throws ClassCastException {
284284
return ({{{dataType}}})super.getActualInstance();
285285
}
286286

modules/openapi-generator/src/main/resources/Java/libraries/jersey3/anyof_model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
218218
* @return The actual instance of `{{{dataType}}}`
219219
* @throws ClassCastException if the instance is not `{{{dataType}}}`
220220
*/
221-
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
221+
public {{{dataType}}} get{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}() throws ClassCastException {
222222
return ({{{dataType}}})super.getActualInstance();
223223
}
224224

modules/openapi-generator/src/main/resources/Java/libraries/jersey3/oneof_model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
280280
* @return The actual instance of `{{{dataType}}}`
281281
* @throws ClassCastException if the instance is not `{{{dataType}}}`
282282
*/
283-
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
283+
public {{{dataType}}} get{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}() throws ClassCastException {
284284
return ({{{dataType}}})super.getActualInstance();
285285
}
286286

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/anyof_model.mustache

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
5454
{{/isArray}}
5555
{{#isArray}}
5656

57-
final Type typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = new TypeToken<{{{dataType}}}>(){}.getType();
58-
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}));
57+
final Type typeInstance{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}} = new TypeToken<{{{dataType}}}>(){}.getType();
58+
final TypeAdapter<{{{dataType}}}> adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}} = (TypeAdapter<{{{dataType}}}>) gson.getDelegateAdapter(this, TypeToken.get(typeInstance{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}));
5959
{{/isArray}}
6060
{{/anyOf}}
6161
{{/composedSchemas}}
@@ -74,23 +74,23 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
7474
// check if the actual instance is of the type `{{{dataType}}}`
7575
if (value.getActualInstance() instanceof {{#isArray}}List<?>{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}) {
7676
{{#isPrimitiveType}}
77-
JsonPrimitive primitive = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
77+
JsonPrimitive primitive = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonPrimitive();
7878
elementAdapter.write(out, primitive);
7979
return;
8080
{{/isPrimitiveType}}
8181
{{^isPrimitiveType}}
8282
{{#isArray}}
8383
List<?> list = (List<?>) value.getActualInstance();
8484
if (list.get(0) instanceof {{{items.dataType}}}) {
85-
JsonArray array = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
85+
JsonArray array = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}.toJsonTree(({{{dataType}}})value.getActualInstance()).getAsJsonArray();
8686
elementAdapter.write(out, array);
8787
return;
8888
}
8989
{{/isArray}}
9090
{{/isPrimitiveType}}
9191
{{^isArray}}
9292
{{^isPrimitiveType}}
93-
JsonElement element = adapter{{{dataType}}}.toJsonTree(({{{dataType}}})value.getActualInstance());
93+
JsonElement element = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}.toJsonTree(({{{dataType}}})value.getActualInstance());
9494
elementAdapter.write(out, element);
9595
return;
9696
{{/isPrimitiveType}}
@@ -122,20 +122,20 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
122122
if (!jsonElement.getAsJsonPrimitive().isNumber()) {
123123
throw new IllegalArgumentException(String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()));
124124
}
125-
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
125+
actualAdapter = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}};
126126
{{/isNumber}}
127127
{{^isNumber}}
128128
{{#isPrimitiveType}}
129129
if (!jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
130130
throw new IllegalArgumentException(String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString()));
131131
}
132-
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
132+
actualAdapter = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}};
133133
{{/isPrimitiveType}}
134134
{{/isNumber}}
135135
{{^isNumber}}
136136
{{^isPrimitiveType}}
137137
{{{dataType}}}.validateJsonElement(jsonElement);
138-
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
138+
actualAdapter = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}};
139139
{{/isPrimitiveType}}
140140
{{/isNumber}}
141141
{{/isArray}}
@@ -167,7 +167,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
167167
{{/isNumber}}
168168
{{/items}}
169169
}
170-
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
170+
actualAdapter = adapter{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}};
171171
{{/isArray}}
172172
{{classname}} ret = new {{classname}}();
173173
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
@@ -291,7 +291,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
291291
* @return The actual instance of `{{{dataType}}}`
292292
* @throws ClassCastException if the instance is not `{{{dataType}}}`
293293
*/
294-
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
294+
public {{{dataType}}} get{{#sanitizeDataType}}{{{dataType}}}{{/sanitizeDataType}}() throws ClassCastException {
295295
return ({{{dataType}}})super.getActualInstance();
296296
}
297297

0 commit comments

Comments
 (0)