Skip to content

Commit 6cc17d8

Browse files
committed
Add authorization scope data to CodegenSecurity, demo use in JAXRS
1 parent 638bbfa commit 6cc17d8

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class CodegenOperation {
1212
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
13-
public Boolean hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
13+
public Boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
1414
returnSimpleType, subresourceOperation, isMapContainer, isListContainer,
1515
hasMore = Boolean.TRUE, isMultipart, isResponseBinary = Boolean.FALSE;
1616
public String path, operationId, returnType, httpMethod, returnBaseType,

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenSecurity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.swagger.codegen;
22

3-
import java.util.Set;
3+
import java.util.List;
4+
import java.util.Map;
45

56
public class CodegenSecurity {
67
public String name;
@@ -11,5 +12,5 @@ public class CodegenSecurity {
1112
public Boolean isKeyInQuery, isKeyInHeader;
1213
// Oauth specific
1314
public String flow, authorizationUrl, tokenUrl;
14-
public Set<String> scopes;
15+
public List<Map<String, Object>> scopes;
1516
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,23 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
13071307
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
13081308
sec.tokenUrl = oauth2Definition.getTokenUrl();
13091309
if (oauth2Definition.getScopes() != null) {
1310-
sec.scopes = oauth2Definition.getScopes().keySet();
1310+
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
1311+
int count = 0, numScopes = oauth2Definition.getScopes().size();
1312+
for(Map.Entry<String, String> scopeEntry : oauth2Definition.getScopes().entrySet()) {
1313+
Map<String, Object> scope = new HashMap<String, Object>();
1314+
scope.put("scope", scopeEntry.getKey());
1315+
scope.put("description", scopeEntry.getValue());
1316+
1317+
count += 1;
1318+
if (count < numScopes) {
1319+
scope.put("hasMore", "true");
1320+
} else {
1321+
scope.put("hasMore", null);
1322+
}
1323+
1324+
scopes.add(scope);
1325+
}
1326+
sec.scopes = scopes;
13111327
}
13121328
}
13131329

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
509509
}
510510
if (!authMethods.isEmpty()) {
511511
co.authMethods = config.fromSecurity(authMethods);
512+
co.hasAuthMethods = true;
512513
}
513514
}
514515
catch (Exception ex) {

modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ public class {{classname}} {
3737
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
3838
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
3939
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
40-
@io.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
40+
@io.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
41+
{{#authMethods}}@io.swagger.annotations.Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
42+
{{#scopes}}@io.swagger.annotations.AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
43+
{{/hasMore}}{{/scopes}}
44+
}{{/isOAuth}}){{#hasMore}},
45+
{{/hasMore}}{{/authMethods}}
46+
}{{/hasAuthMethods}})
4147
@io.swagger.annotations.ApiResponses(value = { {{#responses}}
4248
@io.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}},
4349
{{/hasMore}}{{/responses}} })

0 commit comments

Comments
 (0)