Skip to content

Commit 55c47a9

Browse files
Ignacio-Vidalclaude
andcommitted
[jaxrs-spec][quarkus] Add useQuarkusSecurityAnnotations CLI flag to opt into security annotation emission
Introduces a new `useQuarkusSecurityAnnotations` boolean generator option (default false, quarkus library only) that gates emission of security annotations (@authenticated and, in future PRs, @RolesAllowed and @permitAll). Without the flag the generator behaviour is unchanged; users who manage auth via application.properties can leave it unset. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a76ad13 commit 55c47a9

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
5353
public static final String USE_MUTINY = "useMutiny";
5454
public static final String OPEN_API_SPEC_FILE_LOCATION = "openApiSpecFileLocation";
5555
public static final String GENERATE_JSON_CREATOR = "generateJsonCreator";
56+
public static final String USE_QUARKUS_SECURITY_ANNOTATIONS = "useQuarkusSecurityAnnotations";
5657

5758
public static final String QUARKUS_LIBRARY = "quarkus";
5859
public static final String THORNTAIL_LIBRARY = "thorntail";
@@ -68,6 +69,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
6869
private boolean useSwaggerV3Annotations = false;
6970
private boolean useMicroProfileOpenAPIAnnotations = false;
7071
private boolean useMutiny = false;
72+
private boolean useQuarkusSecurityAnnotations = false;
7173

7274
@Getter @Setter
7375
protected boolean generateJsonCreator = true;
@@ -147,6 +149,7 @@ public JavaJAXRSSpecServerCodegen() {
147149
cliOptions.add(CliOption.newString(OPEN_API_SPEC_FILE_LOCATION, "Location where the file containing the spec will be generated in the output folder. No file generated when set to null or empty string."));
148150
cliOptions.add(CliOption.newBoolean(SUPPORT_ASYNC, "Wrap responses in CompletionStage type, allowing asynchronous computation (requires JAX-RS 2.1).", supportAsync));
149151
cliOptions.add(CliOption.newBoolean(USE_MUTINY, "Whether to use Smallrye Mutiny instead of CompletionStage for asynchronous computation. Only valid when library is set to quarkus.", useMutiny));
152+
cliOptions.add(CliOption.newBoolean(USE_QUARKUS_SECURITY_ANNOTATIONS, "Whether to generate Quarkus security annotations (@Authenticated, @RolesAllowed, @PermitAll). Only valid when library is set to quarkus.", useQuarkusSecurityAnnotations));
150153
cliOptions.add(CliOption.newBoolean(GENERATE_JSON_CREATOR, "Whether to generate @JsonCreator constructor for required properties.", generateJsonCreator));
151154
}
152155

@@ -189,6 +192,10 @@ public void processOpts() {
189192
convertPropertyToBooleanAndWriteBack(USE_MUTINY, value -> useMutiny = value);
190193
}
191194

195+
if (QUARKUS_LIBRARY.equals(library)) {
196+
convertPropertyToBooleanAndWriteBack(USE_QUARKUS_SECURITY_ANNOTATIONS, value -> useQuarkusSecurityAnnotations = value);
197+
}
198+
192199
convertPropertyToBooleanAndWriteBack(GENERATE_JSON_CREATOR, this::setGenerateJsonCreator);
193200

194201
if (additionalProperties.containsKey(OPEN_API_SPEC_FILE_LOCATION)) {
@@ -360,9 +367,9 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
360367
additionalProperties.put("hasResponseStatusAnnotations", true);
361368
}
362369
}
363-
if (QUARKUS_LIBRARY.equals(getLibrary())) {
370+
if (useQuarkusSecurityAnnotations && QUARKUS_LIBRARY.equals(getLibrary())) {
364371
for (CodegenOperation op : objs.getOperations().getOperation()) {
365-
if (shouldAddAuthenticatedAnnotation(op)){
372+
if (shouldAddAuthenticatedAnnotation(op)) {
366373
op.vendorExtensions.put("x-quarkus-authenticated", true);
367374
}
368375
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ public void quarkusEmitsAuthenticatedAnnotationForOAuth2(String specPath, boolea
15811581
codegen.setLibrary(QUARKUS_LIBRARY);
15821582
codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly);
15831583
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1584+
codegen.additionalProperties().put(USE_QUARKUS_SECURITY_ANNOTATIONS, true);
15841585

15851586
final ClientOptInput input = new ClientOptInput()
15861587
.openAPI(openAPI)
@@ -1620,6 +1621,7 @@ public void quarkusEmitsAuthenticatedAnnotationForHttpBasic(boolean interfaceOnl
16201621
codegen.setLibrary(QUARKUS_LIBRARY);
16211622
codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly);
16221623
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1624+
codegen.additionalProperties().put(USE_QUARKUS_SECURITY_ANNOTATIONS, true);
16231625

16241626
final DefaultGenerator generator = new DefaultGenerator();
16251627
final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate();
@@ -1655,6 +1657,7 @@ public void quarkusEmitsAuthenticatedAnnotationForHttpBearer(boolean interfaceOn
16551657
codegen.setLibrary(QUARKUS_LIBRARY);
16561658
codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly);
16571659
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1660+
codegen.additionalProperties().put(USE_QUARKUS_SECURITY_ANNOTATIONS, true);
16581661

16591662
final DefaultGenerator generator = new DefaultGenerator();
16601663
final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate();
@@ -1690,6 +1693,7 @@ public void quarkusEmitsAuthenticatedAnnotationForApiKey(boolean interfaceOnly)
16901693
codegen.setLibrary(QUARKUS_LIBRARY);
16911694
codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly);
16921695
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1696+
codegen.additionalProperties().put(USE_QUARKUS_SECURITY_ANNOTATIONS, true);
16931697

16941698
final DefaultGenerator generator = new DefaultGenerator();
16951699
final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate();
@@ -1730,6 +1734,7 @@ public void quarkusEmitsAuthenticatedAnnotationForOpenIdConnect(String specPath,
17301734
codegen.setLibrary(QUARKUS_LIBRARY);
17311735
codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly);
17321736
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1737+
codegen.additionalProperties().put(USE_QUARKUS_SECURITY_ANNOTATIONS, true);
17331738

17341739
final DefaultGenerator generator = new DefaultGenerator();
17351740
final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate();
@@ -1740,4 +1745,26 @@ public void quarkusEmitsAuthenticatedAnnotationForOpenIdConnect(String specPath,
17401745
final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java"));
17411746
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), expectedCount);
17421747
}
1748+
1749+
@Test
1750+
public void quarkusDoesNotEmitAuthenticatedWhenSecurityAnnotationsDisabled() throws Exception {
1751+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1752+
output.deleteOnExit();
1753+
1754+
final OpenAPI openAPI = new OpenAPIParser()
1755+
.readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-no-scopes.yaml",
1756+
null, new ParseOptions()).getOpenAPI();
1757+
1758+
codegen.setOutputDir(output.getAbsolutePath());
1759+
codegen.setLibrary(QUARKUS_LIBRARY);
1760+
codegen.additionalProperties().put(INTERFACE_ONLY, true);
1761+
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1762+
// useQuarkusSecurityAnnotations intentionally NOT set (defaults to false)
1763+
1764+
final DefaultGenerator generator = new DefaultGenerator();
1765+
generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate();
1766+
1767+
final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java"));
1768+
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 0);
1769+
}
17431770
}

0 commit comments

Comments
 (0)