Skip to content

Commit

Permalink
Merge pull request #1392 from nbruno/add-auth-annotations-jaxrs
Browse files Browse the repository at this point in the history
Add authorization scope data to CodegenSecurity
  • Loading branch information
wing328 committed Oct 19, 2015
2 parents eb1e0b5 + b83db8e commit 4ea797d
Show file tree
Hide file tree
Showing 39 changed files with 235 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class CodegenOperation {
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
public Boolean hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
public Boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
returnSimpleType, subresourceOperation, isMapContainer, isListContainer,
hasMore = Boolean.TRUE, isMultipart, isResponseBinary = Boolean.FALSE;
public String path, operationId, returnType, httpMethod, returnBaseType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.swagger.codegen;

import java.util.Set;
import java.util.List;
import java.util.Map;

public class CodegenSecurity {
public String name;
Expand All @@ -11,5 +12,5 @@ public class CodegenSecurity {
public Boolean isKeyInQuery, isKeyInHeader;
// Oauth specific
public String flow, authorizationUrl, tokenUrl;
public Set<String> scopes;
public List<Map<String, Object>> scopes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,23 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
sec.tokenUrl = oauth2Definition.getTokenUrl();
if (oauth2Definition.getScopes() != null) {
sec.scopes = oauth2Definition.getScopes().keySet();
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
int count = 0, numScopes = oauth2Definition.getScopes().size();
for(Map.Entry<String, String> scopeEntry : oauth2Definition.getScopes().entrySet()) {
Map<String, Object> scope = new HashMap<String, Object>();
scope.put("scope", scopeEntry.getKey());
scope.put("description", scopeEntry.getValue());

count += 1;
if (count < numScopes) {
scope.put("hasMore", "true");
} else {
scope.put("hasMore", null);
}

scopes.add(scope);
}
sec.scopes = scopes;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
}
if (!authMethods.isEmpty()) {
co.authMethods = config.fromSecurity(authMethods);
co.hasAuthMethods = true;
}
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ApiClient {
if (authName == "{{name}}") { {{#isBasic}}
auth = new HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{^-first}}, {{/-first}}{{this}}{{/scopes}}");{{/isOAuth}}
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{#hasMore}}, {{/hasMore}}{{/scopes}}");{{/isOAuth}}
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public class {{classname}} {
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
@io.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
@io.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
{{#authMethods}}@io.swagger.annotations.Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
{{#scopes}}@io.swagger.annotations.AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
{{/hasMore}}{{/scopes}}
}{{/isOAuth}}){{#hasMore}},
{{/hasMore}}{{/authMethods}}
}{{/hasAuthMethods}})
@io.swagger.annotations.ApiResponses(value = { {{#responses}}
@io.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}},
{{/hasMore}}{{/responses}} })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.AuthorizationScope;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -35,7 +37,13 @@ import static org.springframework.http.MediaType.*;
public class {{classname}} {
{{#operation}}

@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
{{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
{{/hasMore}}{{/scopes}}
}{{/isOAuth}}){{#hasMore}},
{{/hasMore}}{{/authMethods}}
}{{/hasAuthMethods}})
@ApiResponses(value = { {{#responses}}
@ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},{{/hasMore}}{{/responses}} })
@RequestMapping(value = "{{path}}",
Expand Down
8 changes: 3 additions & 5 deletions samples/server/petstore/jaxrs/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Swagger generated server

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

This example uses the [scalatra](http://scalatra.org/) framework. To see how to make this your own, look here:

[README](https://github.com/swagger-api/swagger-codegen/tree/master/samples/server-generator/scalatra)
This example uses the [JAX-RS](https://jax-rs-spec.java.net/) framework.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.swagger.api;

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-08-23T23:29:16.812-07:00")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-10-16T21:16:23.957-04:00")
public class ApiException extends Exception{
private int code;
public ApiException (int code, String msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-08-23T23:29:16.812-07:00")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-10-16T21:16:23.957-04:00")
public class ApiOriginFilter implements javax.servlet.Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.xml.bind.annotation.XmlTransient;

@javax.xml.bind.annotation.XmlRootElement
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-08-23T23:29:16.812-07:00")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2015-10-16T21:16:23.957-04:00")
public class ApiResponseMessage {
public static final int ERROR = 1;
public static final int WARNING = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.swagger.api;

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

0 comments on commit 4ea797d

Please sign in to comment.