Skip to content

Commit b04617d

Browse files
committed
Fix test case
1 parent f853632 commit b04617d

6 files changed

Lines changed: 213 additions & 16 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ public ModelsMap postProcessModels(ModelsMap objs) {
343343
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
344344
objs = super.postProcessOperationsWithModels(objs, allModels);
345345
removeImport(objs, "java.util.List");
346+
if (QUARKUS_LIBRARY.equals(getLibrary())) {
347+
for (CodegenOperation op : objs.getOperations().getOperation()) {
348+
if (shouldAddAuthenticatedAnnotation(op)){
349+
op.vendorExtensions.put("x-quarkus-authenticated", true);
350+
}
351+
}
352+
}
346353
return objs;
347354
}
348355

@@ -367,4 +374,16 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
367374
}
368375
return result;
369376
}
377+
378+
protected boolean shouldAddAuthenticatedAnnotation(CodegenOperation op) {
379+
if (!op.hasAuthMethods) {
380+
return false;
381+
}
382+
return op.authMethods.stream().anyMatch(m ->
383+
(Boolean.TRUE.equals(m.isOAuth) && (m.scopes == null || m.scopes.isEmpty())) ||
384+
(Boolean.TRUE.equals(m.isOpenId) && (m.scopes == null || m.scopes.isEmpty())) ||
385+
Boolean.TRUE.equals(m.isBasicBasic) ||
386+
Boolean.TRUE.equals(m.isApiKey)
387+
);
388+
}
370389
}

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/libraries/quarkus/apiInterface.mustache

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@
5050
{{^vendorExtensions.x-java-is-response-void}}@org.eclipse.microprofile.openapi.annotations.media.Content(schema = @org.eclipse.microprofile.openapi.annotations.media.Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{/vendorExtensions.x-java-is-response-void}}
5151
}){{^-last}},{{/-last}}{{/responses}}
5252
}){{/hasProduces}}{{/useMicroProfileOpenAPIAnnotations}}
53-
{{#hasAuthMethods}}
54-
{{#authMethods}}
55-
{{#isOAuth}}
56-
{{^scopes}}
53+
{{#vendorExtensions.x-quarkus-authenticated}}
5754
@io.quarkus.security.Authenticated
58-
{{/scopes}}
59-
{{/isOAuth}}
60-
{{/authMethods}}
61-
{{/hasAuthMethods}}
55+
{{/vendorExtensions.x-quarkus-authenticated}}
6256
{{#supportAsync}}{{>returnAsyncTypeInterface}}{{/supportAsync}}{{^supportAsync}}{{#returnJBossResponse}}{{>returnResponseTypeInterface}}{{/returnJBossResponse}}{{^returnJBossResponse}}{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnTypeInterface}}{{/returnResponse}}{{/returnJBossResponse}}{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}});

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/libraries/quarkus/apiMethod.mustache

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@
4747
{{^vendorExtensions.x-java-is-response-void}}@org.eclipse.microprofile.openapi.annotations.media.Content(schema = @org.eclipse.microprofile.openapi.annotations.media.Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{/vendorExtensions.x-java-is-response-void}}
4848
}){{^-last}},{{/-last}}{{/responses}}
4949
}){{/hasProduces}}{{/useMicroProfileOpenAPIAnnotations}}
50-
{{#hasAuthMethods}}
51-
{{#authMethods}}
52-
{{#isOAuth}}
53-
{{^scopes}}
50+
{{#vendorExtensions.x-quarkus-authenticated}}
5451
@io.quarkus.security.Authenticated
55-
{{/scopes}}
56-
{{/isOAuth}}
57-
{{/authMethods}}
58-
{{/hasAuthMethods}}
52+
{{/vendorExtensions.x-quarkus-authenticated}}
5953
public {{#supportAsync}}{{#useMutiny}}Uni{{/useMutiny}}{{^useMutiny}}CompletionStage{{/useMutiny}}<{{/supportAsync}}{{#returnJBossResponse}}{{>returnResponseTypeInterface}}{{/returnJBossResponse}}{{^returnJBossResponse}}Response{{/returnJBossResponse}}{{#supportAsync}}>{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) {
6054
return {{#supportAsync}}{{#useMutiny}}Uni.createFrom().item({{/useMutiny}}{{^useMutiny}}CompletableFuture.supplyAsync(() -> {{/useMutiny}}{{/supportAsync}}Response.ok().entity("magic!").build(){{#supportAsync}}){{/supportAsync}};
6155
}

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,4 +1450,118 @@ public void generateQuarkusImplWithoutAuthenticatedForOAuth2WithScopes() throws
14501450
assertFileNotContains(defaultApi, "@io.quarkus.security.Authenticated");
14511451
}
14521452

1453+
@Test
1454+
public void generateQuarkusInterfaceWithAuthenticatedOnceForOAuth2MultipleFlowsNoScopes() throws Exception {
1455+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1456+
output.deleteOnExit();
1457+
1458+
final OpenAPI openAPI = new OpenAPIParser()
1459+
.readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-multi-flow-no-scopes.yaml", null, new ParseOptions()).getOpenAPI();
1460+
1461+
codegen.setOutputDir(output.getAbsolutePath());
1462+
codegen.setLibrary(QUARKUS_LIBRARY);
1463+
codegen.additionalProperties().put(INTERFACE_ONLY, true);
1464+
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1465+
1466+
final ClientOptInput input = new ClientOptInput()
1467+
.openAPI(openAPI)
1468+
.config(codegen);
1469+
1470+
final DefaultGenerator generator = new DefaultGenerator();
1471+
final List<File> files = generator.opts(input).generate();
1472+
1473+
validateJavaSourceFiles(files);
1474+
1475+
final Path defaultApi = output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java");
1476+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java");
1477+
String content = Files.readString(defaultApi);
1478+
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1);
1479+
}
1480+
1481+
@Test
1482+
public void generateQuarkusImplWithAuthenticatedOnceForOAuth2MultipleFlowsNoScopes() throws Exception {
1483+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1484+
output.deleteOnExit();
1485+
1486+
final OpenAPI openAPI = new OpenAPIParser()
1487+
.readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-multi-flow-no-scopes.yaml", null, new ParseOptions()).getOpenAPI();
1488+
1489+
codegen.setOutputDir(output.getAbsolutePath());
1490+
codegen.setLibrary(QUARKUS_LIBRARY);
1491+
codegen.additionalProperties().put(INTERFACE_ONLY, false);
1492+
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1493+
1494+
final ClientOptInput input = new ClientOptInput()
1495+
.openAPI(openAPI)
1496+
.config(codegen);
1497+
1498+
final DefaultGenerator generator = new DefaultGenerator();
1499+
final List<File> files = generator.opts(input).generate();
1500+
1501+
validateJavaSourceFiles(files);
1502+
1503+
final Path defaultApi = output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java");
1504+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java");
1505+
String content = Files.readString(defaultApi);
1506+
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1);
1507+
}
1508+
1509+
@Test
1510+
public void generateQuarkusInterfaceWithAuthenticatedWhenOrSchemesIncludeEmptyScopes() throws Exception {
1511+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1512+
output.deleteOnExit();
1513+
1514+
final OpenAPI openAPI = new OpenAPIParser()
1515+
.readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-or-empty-and-scoped.yaml", null, new ParseOptions()).getOpenAPI();
1516+
1517+
codegen.setOutputDir(output.getAbsolutePath());
1518+
codegen.setLibrary(QUARKUS_LIBRARY);
1519+
codegen.additionalProperties().put(INTERFACE_ONLY, true);
1520+
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1521+
1522+
final ClientOptInput input = new ClientOptInput()
1523+
.openAPI(openAPI)
1524+
.config(codegen);
1525+
1526+
final DefaultGenerator generator = new DefaultGenerator();
1527+
final List<File> files = generator.opts(input).generate();
1528+
1529+
validateJavaSourceFiles(files);
1530+
1531+
final Path defaultApi = output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java");
1532+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java");
1533+
// getItems (empty-scope OR scoped) → @Authenticated; createItem (scoped only) → none: count must be exactly 1
1534+
String content = Files.readString(defaultApi);
1535+
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1);
1536+
}
1537+
1538+
@Test
1539+
public void generateQuarkusImplWithAuthenticatedWhenOrSchemesIncludeEmptyScopes() throws Exception {
1540+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1541+
output.deleteOnExit();
1542+
1543+
final OpenAPI openAPI = new OpenAPIParser()
1544+
.readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-or-empty-and-scoped.yaml", null, new ParseOptions()).getOpenAPI();
1545+
1546+
codegen.setOutputDir(output.getAbsolutePath());
1547+
codegen.setLibrary(QUARKUS_LIBRARY);
1548+
codegen.additionalProperties().put(INTERFACE_ONLY, false);
1549+
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
1550+
1551+
final ClientOptInput input = new ClientOptInput()
1552+
.openAPI(openAPI)
1553+
.config(codegen);
1554+
1555+
final DefaultGenerator generator = new DefaultGenerator();
1556+
final List<File> files = generator.opts(input).generate();
1557+
1558+
validateJavaSourceFiles(files);
1559+
1560+
final Path defaultApi = output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java");
1561+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java");
1562+
// getItems (empty-scope OR scoped) → @Authenticated; createItem (scoped only) → none: count must be exactly 1
1563+
String content = Files.readString(defaultApi);
1564+
Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1);
1565+
}
1566+
14531567
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Quarkus OAuth2 multi-flow no scopes test
4+
version: '1.0'
5+
servers:
6+
- url: 'http://localhost:8080/'
7+
paths:
8+
/items:
9+
get:
10+
operationId: getItems
11+
summary: Get items
12+
security:
13+
- oauth2_scheme: []
14+
responses:
15+
'200':
16+
description: OK
17+
post:
18+
operationId: createItem
19+
summary: Create item
20+
responses:
21+
'201':
22+
description: Created
23+
components:
24+
securitySchemes:
25+
oauth2_scheme:
26+
type: oauth2
27+
flows:
28+
authorizationCode:
29+
authorizationUrl: https://example.com/oauth/authorize
30+
tokenUrl: https://example.com/oauth/token
31+
scopes: {}
32+
implicit:
33+
authorizationUrl: https://example.com/api/oauth/dialog
34+
scopes: {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Quarkus OAuth2 OR empty-and-scoped test
4+
version: '1.0'
5+
servers:
6+
- url: 'http://localhost:8080/'
7+
paths:
8+
/items:
9+
get:
10+
operationId: getItems
11+
summary: Get items — OR any-authenticated OR scoped
12+
security:
13+
- oauth2_no_scope: []
14+
- oauth2_with_scope:
15+
- admin
16+
responses:
17+
'200':
18+
description: OK
19+
post:
20+
operationId: createItem
21+
summary: Create item — scoped only
22+
security:
23+
- oauth2_with_scope:
24+
- admin
25+
responses:
26+
'201':
27+
description: Created
28+
components:
29+
securitySchemes:
30+
oauth2_no_scope:
31+
type: oauth2
32+
flows:
33+
clientCredentials:
34+
tokenUrl: https://example.com/oauth/token
35+
scopes: {}
36+
oauth2_with_scope:
37+
type: oauth2
38+
flows:
39+
clientCredentials:
40+
tokenUrl: https://example.com/oauth/token
41+
scopes:
42+
admin: Admin access

0 commit comments

Comments
 (0)