Skip to content

Commit 25a7873

Browse files
committed
test(jaxrs-spec/quarkus): add missing ResponseStatus annotation test coverage
Add negative tests for returnJBossResponse=true and non-Quarkus libraries to verify @ResponseStatus is suppressed, and a positive test for interfaceOnly=false to cover the apiMethod.mustache code path. Also fixes the response filter to restrict to pure numeric 2xx codes only, removing 3xx support and wildcard code guard, and drops the redundant global additionalProperties flag.
1 parent df871e1 commit 25a7873

3 files changed

Lines changed: 75 additions & 35 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
346346
if (QUARKUS_LIBRARY.equals(library) && !returnResponse && !returnJbossResponse) {
347347
for (CodegenOperation op : objs.getOperations().getOperation()) {
348348
op.responses.stream()
349-
.filter(r -> r.is2xx || r.is3xx)
349+
.filter(r -> r.is2xx && r.code.matches("\\d+"))
350350
.findFirst()
351351
.ifPresent(r -> op.vendorExtensions.put("x-java-success-response-code", r.code));
352352
}
353353
if (objs.getOperations().getOperation().stream()
354354
.anyMatch(op -> op.vendorExtensions.containsKey("x-java-success-response-code"))) {
355355
objs.put("hasResponseStatusAnnotations", true);
356-
additionalProperties.put("hasResponseStatusAnnotations", true);
357356
}
358357
}
359358
return objs;

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

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,20 +1444,86 @@ public void generateQuarkusInterfaceDoesNotAddResponseStatusAnnotationWhenReturn
14441444
}
14451445

14461446
/**
1447-
* Verify that when using the quarkus library with interfaceOnly=true and a 3xx response,
1448-
* the generated interface method is annotated with {@code @ResponseStatus(<code>)}.
1447+
* Verify that the {@code @ResponseStatus} annotation is NOT emitted when returnJBossResponse=true,
1448+
* because the caller controls the status code via the {@code RestResponse} wrapper in that mode.
14491449
*/
14501450
@Test
1451-
public void generateQuarkusInterfaceAddsResponseStatusAnnotationFor3xxResponseCode() throws Exception {
1451+
public void generateQuarkusInterfaceDoesNotAddResponseStatusAnnotationWhenReturnJBossResponse() throws Exception {
14521452
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
14531453
output.deleteOnExit();
14541454

14551455
final OpenAPI openAPI = new OpenAPIParser()
1456-
.readLocation("src/test/resources/3_0/jaxrs-spec/petstore-quarkus.yaml", null, new ParseOptions()).getOpenAPI();
1456+
.readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI();
14571457

14581458
codegen.setOutputDir(output.getAbsolutePath());
14591459
codegen.setLibrary(QUARKUS_LIBRARY);
14601460
codegen.additionalProperties().put(INTERFACE_ONLY, true);
1461+
codegen.additionalProperties().put(USE_JAKARTA_EE, true); //Required by returnJBossResponse
1462+
codegen.additionalProperties().put(RETURN_JBOSS_RESPONSE, true); //Given returnJBossResponse is true
1463+
1464+
final ClientOptInput input = new ClientOptInput()
1465+
.openAPI(openAPI)
1466+
.config(codegen);
1467+
1468+
final DefaultGenerator generator = new DefaultGenerator();
1469+
final List<File> files = generator.opts(input).generate();
1470+
1471+
validateJavaSourceFiles(files);
1472+
1473+
//Then the annotation must NOT appear
1474+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PingApi.java");
1475+
assertFileNotContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PingApi.java"),
1476+
"@ResponseStatus",
1477+
"import org.jboss.resteasy.reactive.ResponseStatus");
1478+
}
1479+
1480+
/**
1481+
* Verify that {@code @ResponseStatus} is NOT emitted when using a non-Quarkus jaxrs-spec library,
1482+
* since {@code org.jboss.resteasy.reactive.ResponseStatus} is a RESTEasy Reactive / Quarkus-specific annotation.
1483+
*/
1484+
@Test
1485+
public void generateNonQuarkusInterfaceDoesNotAddResponseStatusAnnotation() throws Exception {
1486+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1487+
output.deleteOnExit();
1488+
1489+
final OpenAPI openAPI = new OpenAPIParser()
1490+
.readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI();
1491+
1492+
codegen.setOutputDir(output.getAbsolutePath());
1493+
// No setLibrary call — uses the default jaxrs-spec library
1494+
codegen.additionalProperties().put(INTERFACE_ONLY, true);
1495+
1496+
final ClientOptInput input = new ClientOptInput()
1497+
.openAPI(openAPI)
1498+
.config(codegen);
1499+
1500+
final DefaultGenerator generator = new DefaultGenerator();
1501+
final List<File> files = generator.opts(input).generate();
1502+
1503+
validateJavaSourceFiles(files);
1504+
1505+
//Then the annotation must NOT appear
1506+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PingApi.java");
1507+
assertFileNotContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PingApi.java"),
1508+
"@ResponseStatus",
1509+
"import org.jboss.resteasy.reactive.ResponseStatus");
1510+
}
1511+
1512+
/**
1513+
* Verify that when using the quarkus library with interfaceOnly=false (concrete stub class),
1514+
* the generated implementation method is also annotated with {@code @ResponseStatus(<code>)}.
1515+
*/
1516+
@Test
1517+
public void generateQuarkusConcreteClassAddsResponseStatusAnnotation() throws Exception {
1518+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1519+
output.deleteOnExit();
1520+
1521+
final OpenAPI openAPI = new OpenAPIParser()
1522+
.readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI();
1523+
1524+
codegen.setOutputDir(output.getAbsolutePath());
1525+
codegen.setLibrary(QUARKUS_LIBRARY);
1526+
codegen.additionalProperties().put(INTERFACE_ONLY, false); //Given the concrete class is generated
14611527

14621528
final ClientOptInput input = new ClientOptInput()
14631529
.openAPI(openAPI)
@@ -1468,10 +1534,10 @@ public void generateQuarkusInterfaceAddsResponseStatusAnnotationFor3xxResponseCo
14681534

14691535
validateJavaSourceFiles(files);
14701536

1471-
//Then the generated interface contains the ResponseStatus import and annotation with code 302
1472-
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/RedirectApi.java");
1473-
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/RedirectApi.java"),
1537+
//Then the generated class contains the ResponseStatus import and annotation with code 201
1538+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PingApi.java");
1539+
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PingApi.java"),
14741540
"import org.jboss.resteasy.reactive.ResponseStatus;",
1475-
"@ResponseStatus(302)");
1541+
"@ResponseStatus(201)");
14761542
}
14771543
}

modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/petstore-quarkus.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)