Skip to content

Commit 583aab3

Browse files
committed
fix (JAVA NATIVE CLIENT): using anyOf with generic type lead to compile error
1 parent 9f81af0 commit 583aab3

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

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
@@ -231,7 +231,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
231231
* @return The actual instance of `{{{.}}}`
232232
* @throws ClassCastException if the instance is not `{{{.}}}`
233233
*/
234-
public {{{.}}} get{{{.}}}() throws ClassCastException {
234+
public {{{.}}} get{{#sanitizeGeneric}}{{{.}}}{{/sanitizeGeneric}}() throws ClassCastException {
235235
return ({{{.}}})super.getActualInstance();
236236
}
237237

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
@@ -279,7 +279,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
279279
* @return The actual instance of `{{{.}}}`
280280
* @throws ClassCastException if the instance is not `{{{.}}}`
281281
*/
282-
public {{{.}}} get{{{.}}}() throws ClassCastException {
282+
public {{{.}}} get{{#sanitizeGeneric}}{{{.}}}{{/sanitizeGeneric}}() throws ClassCastException {
283283
return ({{{.}}})super.getActualInstance();
284284
}
285285

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
@@ -153,18 +153,21 @@ public static void validateJavaSourceFiles(List<File> files) {
153153
} catch (IOException ignored) {
154154

155155
}
156-
assertValidJavaSourceCode(fileContents);
156+
assertValidJavaSourceCode(fileContents, f.getAbsolutePath());
157157
}
158158
}
159159
);
160160
}
161161

162162
public static void assertValidJavaSourceCode(String javaSourceCode) {
163+
assertValidJavaSourceCode(javaSourceCode, null);
164+
}
165+
public static void assertValidJavaSourceCode(String javaSourceCode, String fileName) {
163166
ParserConfiguration config = new ParserConfiguration();
164167
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
165168
JavaParser parser = new JavaParser(config);
166169
ParseResult<CompilationUnit> parseResult = parser.parse(javaSourceCode);
167-
assertTrue(parseResult.isSuccessful(), parseResult.getProblems() + "\n in " + javaSourceCode);
170+
assertTrue(parseResult.isSuccessful(), (fileName == null ? "" : "File " + fileName + ": ") + parseResult.getProblems());
168171
}
169172

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

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4307,4 +4307,48 @@ public void testJspecify(String library, boolean useSpringBoot4, boolean hasJspe
43074307
.fileContains("@org.jspecify.annotations.NullMarked");
43084308

43094309
}
4310-
}
4310+
4311+
@DataProvider(name = "allJavaClients")
4312+
public Object[][] allJavaClients() {
4313+
return new Object[][] {
4314+
{ JavaClientCodegen.FEIGN },
4315+
{ JavaClientCodegen.GOOGLE_API_CLIENT },
4316+
{ JavaClientCodegen.JERSEY2 },
4317+
{ JavaClientCodegen.JERSEY3 },
4318+
{ JavaClientCodegen.NATIVE },
4319+
{ JavaClientCodegen.OKHTTP_GSON },
4320+
{ JavaClientCodegen.RESTEASY },
4321+
{ JavaClientCodegen.RESTTEMPLATE },
4322+
{ JavaClientCodegen.WEBCLIENT },
4323+
{ JavaClientCodegen.RESTCLIENT },
4324+
{ JavaClientCodegen.REST_ASSURED },
4325+
{ JavaClientCodegen.RETROFIT_2 },
4326+
{ JavaClientCodegen.VERTX },
4327+
{ JavaClientCodegen.MICROPROFILE },
4328+
{ JavaClientCodegen.APACHE }
4329+
};
4330+
}
4331+
4332+
@Test(dataProvider = "allJavaClients")
4333+
public void testClientWithAnyOfCausedCompileError(String client) {
4334+
if(JavaClientCodegen.MICROPROFILE.equals(client))
4335+
{
4336+
// MikroProfile currently does not support anyOf
4337+
return;
4338+
}
4339+
4340+
final Path output = newTempFolder();
4341+
final OpenAPI openAPI = new OpenAPIParser()
4342+
.readLocation("src/test/resources/3_1/java/petstore.yaml", null, new ParseOptions())
4343+
.getOpenAPI();
4344+
final JavaClientCodegen codegen = new JavaClientCodegen();
4345+
codegen.setOutputDir(output.toString());
4346+
codegen.setLibrary(client);
4347+
4348+
final ClientOptInput input = new ClientOptInput().openAPI(openAPI).config(codegen);
4349+
4350+
List<File> files = new DefaultGenerator().opts(input).generate();
4351+
4352+
validateJavaSourceFiles(files);
4353+
}
4354+
}

0 commit comments

Comments
 (0)