diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenSecurity.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenSecurity.java index 8613e0a8d4..edd65cc060 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenSecurity.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenSecurity.java @@ -1,5 +1,7 @@ package io.swagger.codegen; +import java.util.Set; + public class CodegenSecurity { public String name; public String type; @@ -7,4 +9,7 @@ public class CodegenSecurity { // ApiKey specific public String keyParamName; public Boolean isKeyInQuery, isKeyInHeader; + // Oauth specific + public String flow, authorizationUrl, tokenUrl; + public Set scopes; } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 71b6295c9b..e4967cd8b2 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -14,6 +14,7 @@ import io.swagger.models.Swagger; import io.swagger.models.auth.ApiKeyAuthDefinition; import io.swagger.models.auth.BasicAuthDefinition; import io.swagger.models.auth.In; +import io.swagger.models.auth.OAuth2Definition; import io.swagger.models.auth.SecuritySchemeDefinition; import io.swagger.models.parameters.BodyParameter; import io.swagger.models.parameters.CookieParameter; @@ -1056,10 +1057,17 @@ public class DefaultCodegen { sec.keyParamName = apiKeyDefinition.getName(); sec.isKeyInHeader = apiKeyDefinition.getIn() == In.HEADER; sec.isKeyInQuery = !sec.isKeyInHeader; + } else if(schemeDefinition instanceof BasicAuthDefinition) { + sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isOAuth = false; + sec.isBasic = true; } else { - sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = false; - sec.isBasic = schemeDefinition instanceof BasicAuthDefinition; - sec.isOAuth = !sec.isBasic; + final OAuth2Definition oauth2Definition = (OAuth2Definition) schemeDefinition; + sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isBasic = false; + sec.isOAuth = true; + sec.flow = oauth2Definition.getFlow(); + sec.authorizationUrl = oauth2Definition.getAuthorizationUrl(); + sec.tokenUrl = oauth2Definition.getTokenUrl(); + sec.scopes = oauth2Definition.getScopes().keySet(); } sec.hasMore = it.hasNext(); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index a04e517bee..8ace77d564 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -9,6 +9,7 @@ import io.swagger.models.Model; import io.swagger.models.Operation; import io.swagger.models.Path; import io.swagger.models.Swagger; +import io.swagger.models.auth.OAuth2Definition; import io.swagger.models.auth.SecuritySchemeDefinition; import io.swagger.util.Json; import org.apache.commons.io.IOUtils; @@ -366,7 +367,22 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { String securityName = security.keySet().iterator().next(); SecuritySchemeDefinition securityDefinition = fromSecurity(securityName); if (securityDefinition != null) { - authMethods.put(securityName, securityDefinition); + if(securityDefinition instanceof OAuth2Definition) { + OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition; + OAuth2Definition oauth2Operation = new OAuth2Definition(); + oauth2Operation.setType(oauth2Definition.getType()); + oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl()); + oauth2Operation.setFlow(oauth2Definition.getFlow()); + oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl()); + for (String scope : security.values().iterator().next()) { + if (oauth2Definition.getScopes().containsKey(scope)) { + oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope)); + } + } + authMethods.put(securityName, oauth2Operation); + } else { + authMethods.put(securityName, securityDefinition); + } } } if (!authMethods.isEmpty()) {