Skip to content

Commit 4ea797d

Browse files
committed
Merge pull request #1392 from nbruno/add-auth-annotations-jaxrs
Add authorization scope data to CodegenSecurity
2 parents eb1e0b5 + b83db8e commit 4ea797d

39 files changed

+235
-113
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
@@ -1346,7 +1346,23 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
13461346
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
13471347
sec.tokenUrl = oauth2Definition.getTokenUrl();
13481348
if (oauth2Definition.getScopes() != null) {
1349-
sec.scopes = oauth2Definition.getScopes().keySet();
1349+
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
1350+
int count = 0, numScopes = oauth2Definition.getScopes().size();
1351+
for(Map.Entry<String, String> scopeEntry : oauth2Definition.getScopes().entrySet()) {
1352+
Map<String, Object> scope = new HashMap<String, Object>();
1353+
scope.put("scope", scopeEntry.getKey());
1354+
scope.put("description", scopeEntry.getValue());
1355+
1356+
count += 1;
1357+
if (count < numScopes) {
1358+
scope.put("hasMore", "true");
1359+
} else {
1360+
scope.put("hasMore", null);
1361+
}
1362+
1363+
scopes.add(scope);
1364+
}
1365+
sec.scopes = scopes;
13501366
}
13511367
}
13521368

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
@@ -519,6 +519,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
519519
}
520520
if (!authMethods.isEmpty()) {
521521
co.authMethods = config.fromSecurity(authMethods);
522+
co.hasAuthMethods = true;
522523
}
523524
}
524525
catch (Exception ex) {

modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ApiClient {
5454
if (authName == "{{name}}") { {{#isBasic}}
5555
auth = new HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
5656
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
57-
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{^-first}}, {{/-first}}{{this}}{{/scopes}}");{{/isOAuth}}
57+
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{#hasMore}}, {{/hasMore}}{{/scopes}}");{{/isOAuth}}
5858
} else {
5959
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
6060
}

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}} })

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import io.swagger.annotations.ApiOperation;
1010
import io.swagger.annotations.ApiParam;
1111
import io.swagger.annotations.ApiResponse;
1212
import io.swagger.annotations.ApiResponses;
13+
import io.swagger.annotations.Authorization;
14+
import io.swagger.annotations.AuthorizationScope;
1315

1416
import org.springframework.http.HttpStatus;
1517
import org.springframework.http.ResponseEntity;
@@ -35,7 +37,13 @@ import static org.springframework.http.MediaType.*;
3537
public class {{classname}} {
3638
{{#operation}}
3739

38-
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
40+
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
41+
{{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
42+
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
43+
{{/hasMore}}{{/scopes}}
44+
}{{/isOAuth}}){{#hasMore}},
45+
{{/hasMore}}{{/authMethods}}
46+
}{{/hasAuthMethods}})
3947
@ApiResponses(value = { {{#responses}}
4048
@ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},{{/hasMore}}{{/responses}} })
4149
@RequestMapping(value = "{{path}}",
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Swagger generated server
22

33
## Overview
4-
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
4+
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
55
[swagger-spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
6-
is an example of building a swagger-enabled scalatra server.
6+
is an example of building a swagger-enabled JAX-RS server.
77

8-
This example uses the [scalatra](http://scalatra.org/) framework. To see how to make this your own, look here:
9-
10-
[README](https://github.com/swagger-api/swagger-codegen/tree/master/samples/server-generator/scalatra)
8+
This example uses the [JAX-RS](https://jax-rs-spec.java.net/) framework.

samples/server/petstore/jaxrs/src/gen/java/io/swagger/api/ApiException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.swagger.api;
22

3-
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-08-23T23:29:16.812-07:00")
3+
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-10-16T21:16:23.957-04:00")
44
public class ApiException extends Exception{
55
private int code;
66
public ApiException (int code, String msg) {

samples/server/petstore/jaxrs/src/gen/java/io/swagger/api/ApiOriginFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import javax.servlet.*;
66
import javax.servlet.http.HttpServletResponse;
77

8-
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-08-23T23:29:16.812-07:00")
8+
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-10-16T21:16:23.957-04:00")
99
public class ApiOriginFilter implements javax.servlet.Filter {
1010
public void doFilter(ServletRequest request, ServletResponse response,
1111
FilterChain chain) throws IOException, ServletException {

0 commit comments

Comments
 (0)