Skip to content

Commit 3500d83

Browse files
committed
fix (JAVA NATIVE CLIENT): using anyOf with generic type lead to compile error
1 parent f950ac9 commit 3500d83

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
191191
* @return The actual instance of `{{{.}}}`
192192
* @throws ClassCastException if the instance is not `{{{.}}}`
193193
*/
194-
public {{{.}}} get{{{.}}}() throws ClassCastException {
194+
public {{{.}}} get{{#sanitizeGeneric}}{{{.}}}{{/sanitizeGeneric}}() throws ClassCastException {
195195
return ({{{.}}})super.getActualInstance();
196196
}
197197

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
224224
* @return The actual instance of `{{{.}}}`
225225
* @throws ClassCastException if the instance is not `{{{.}}}`
226226
*/
227-
public {{{.}}} get{{{.}}}() throws ClassCastException {
227+
public {{{.}}} get{{#sanitizeGeneric}}{{{.}}}{{/sanitizeGeneric}}() throws ClassCastException {
228228
return ({{{.}}})super.getActualInstance();
229229
}
230230

modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,21 @@ public static void validateJavaSourceFiles(List<File> files) {
151151
} catch (IOException ignored) {
152152

153153
}
154-
assertValidJavaSourceCode(fileContents);
154+
assertValidJavaSourceCode(fileContents, f.getAbsolutePath());
155155
}
156156
}
157157
);
158158
}
159159

160160
public static void assertValidJavaSourceCode(String javaSourceCode) {
161+
assertValidJavaSourceCode(javaSourceCode, null);
162+
}
163+
public static void assertValidJavaSourceCode(String javaSourceCode, String fileName) {
161164
ParserConfiguration config = new ParserConfiguration();
162165
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
163166
JavaParser parser = new JavaParser(config);
164167
ParseResult<CompilationUnit> parseResult = parser.parse(javaSourceCode);
165-
assertTrue(parseResult.isSuccessful(), String.valueOf(parseResult.getProblems()));
168+
assertTrue(parseResult.isSuccessful(), (fileName == null ? "" : "File " + fileName + ": ") + parseResult.getProblems());
166169
}
167170

168171
public static void assertFileContains(Path path, String... lines) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,4 +3592,48 @@ public void testClassesAreValidJavaOkHttpGson() {
35923592
"public some.pkg.B getsomepkgB() throws ClassCastException {"
35933593
);
35943594
}
3595+
3596+
@DataProvider(name = "allJavaClients")
3597+
public Object[][] allJavaClients() {
3598+
return new Object[][] {
3599+
{ JavaClientCodegen.FEIGN },
3600+
{ JavaClientCodegen.GOOGLE_API_CLIENT },
3601+
{ JavaClientCodegen.JERSEY2 },
3602+
{ JavaClientCodegen.JERSEY3 },
3603+
{ JavaClientCodegen.NATIVE },
3604+
{ JavaClientCodegen.OKHTTP_GSON },
3605+
{ JavaClientCodegen.RESTEASY },
3606+
{ JavaClientCodegen.RESTTEMPLATE },
3607+
{ JavaClientCodegen.WEBCLIENT },
3608+
{ JavaClientCodegen.RESTCLIENT },
3609+
{ JavaClientCodegen.REST_ASSURED },
3610+
{ JavaClientCodegen.RETROFIT_2 },
3611+
{ JavaClientCodegen.VERTX },
3612+
{ JavaClientCodegen.MICROPROFILE },
3613+
{ JavaClientCodegen.APACHE }
3614+
};
3615+
}
3616+
3617+
@Test(dataProvider = "allJavaClients")
3618+
public void testClientWithAnyOfCausedCompileError(String client) {
3619+
if(JavaClientCodegen.MICROPROFILE.equals(client))
3620+
{
3621+
// MikroProfile currently does not support anyOf
3622+
return;
3623+
}
3624+
3625+
final Path output = newTempFolder();
3626+
final OpenAPI openAPI = new OpenAPIParser()
3627+
.readLocation("src/test/resources/3_1/java/petstore.yaml", null, new ParseOptions())
3628+
.getOpenAPI();
3629+
final JavaClientCodegen codegen = new JavaClientCodegen();
3630+
codegen.setOutputDir(output.toString());
3631+
codegen.setLibrary(client);
3632+
3633+
final ClientOptInput input = new ClientOptInput().openAPI(openAPI).config(codegen);
3634+
3635+
List<File> files = new DefaultGenerator().opts(input).generate();
3636+
3637+
validateJavaSourceFiles(files);
3638+
}
35953639
}

0 commit comments

Comments
 (0)