Skip to content

Commit bade71c

Browse files
committed
merged
2 parents 9adb077 + 6b7059d commit bade71c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+329
-66
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface CodegenConfig {
6767

6868
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
6969

70+
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
71+
7072
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
7173

7274
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

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: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,12 @@ private Response findMethodResponse(Map<String, Response> responses) {
867867
}
868868
return responses.get(code);
869869
}
870-
870+
871871
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
872+
return fromOperation(path, httpMethod, operation, definitions, null);
873+
}
874+
875+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
872876
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
873877
Set<String> imports = new HashSet<String>();
874878
op.vendorExtensions = operation.getVendorExtensions();
@@ -904,15 +908,32 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
904908
op.summary = escapeText(operation.getSummary());
905909
op.notes = escapeText(operation.getDescription());
906910
op.tags = operation.getTags();
907-
908-
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
911+
op.hasConsumes = false;
912+
op.hasProduces = false;
913+
914+
List<String> consumes = new ArrayList<String>();
915+
if (operation.getConsumes() != null) {
916+
if (operation.getConsumes().size() > 0) {
917+
// use consumes defined in the operation
918+
consumes = operation.getConsumes();
919+
} else {
920+
// empty list, do nothing to override global setting
921+
}
922+
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
923+
// use consumes defined globally
924+
consumes = swagger.getConsumes();
925+
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
926+
}
927+
928+
// if "consumes" is defined (per operation or using global definition)
929+
if (consumes != null && consumes.size() > 0) {
909930
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
910931
int count = 0;
911-
for (String key : operation.getConsumes()) {
932+
for (String key : consumes) {
912933
Map<String, String> mediaType = new HashMap<String, String>();
913934
mediaType.put("mediaType", key);
914935
count += 1;
915-
if (count < operation.getConsumes().size()) {
936+
if (count < consumes.size()) {
916937
mediaType.put("hasMore", "true");
917938
} else {
918939
mediaType.put("hasMore", null);
@@ -923,14 +944,29 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
923944
op.hasConsumes = true;
924945
}
925946

926-
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
947+
List<String> produces = new ArrayList<String>();
948+
if (operation.getProduces() != null) {
949+
if (operation.getProduces().size() > 0) {
950+
// use produces defined in the operation
951+
produces = operation.getProduces();
952+
} else {
953+
// empty list, do nothing to override global setting
954+
}
955+
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
956+
// use produces defined globally
957+
produces = swagger.getProduces();
958+
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
959+
}
960+
961+
// if "produces" is defined (per operation or using global definition)
962+
if (produces != null && produces.size() > 0) {
927963
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
928964
int count = 0;
929-
for (String key : operation.getProduces()) {
965+
for (String key : produces) {
930966
Map<String, String> mediaType = new HashMap<String, String>();
931967
mediaType.put("mediaType", key);
932968
count += 1;
933-
if (count < operation.getProduces().size()) {
969+
if (count < produces.size()) {
934970
mediaType.put("hasMore", "true");
935971
} else {
936972
mediaType.put("hasMore", null);
@@ -1310,7 +1346,23 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
13101346
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
13111347
sec.tokenUrl = oauth2Definition.getTokenUrl();
13121348
if (oauth2Definition.getScopes() != null) {
1313-
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;
13141366
}
13151367
}
13161368

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
475475
for (String tag : tags) {
476476
CodegenOperation co = null;
477477
try {
478-
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
478+
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
479479
co.tags = new ArrayList<String>();
480480
co.tags.add(sanitizeTag(tag));
481481
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
@@ -523,6 +523,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
523523
}
524524
if (!authMethods.isEmpty()) {
525525
co.authMethods = config.fromSecurity(authMethods);
526+
co.hasAuthMethods = true;
526527
}
527528
}
528529
catch (Exception ex) {

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwiftCodegen.java

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

33
import com.google.common.base.Predicate;
4+
45
import com.google.common.collect.Iterators;
56
import com.google.common.collect.Lists;
67
import io.swagger.codegen.*;
8+
import io.swagger.models.Swagger;
79
import io.swagger.models.Model;
810
import io.swagger.models.Operation;
911
import io.swagger.models.parameters.HeaderParameter;
@@ -256,7 +258,7 @@ public String toApiName(String name) {
256258
}
257259

258260
@Override
259-
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
261+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
260262
path = normalizePath(path);
261263
List<Parameter> parameters = operation.getParameters();
262264
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
@@ -266,7 +268,7 @@ public boolean apply(@Nullable Parameter parameter) {
266268
}
267269
}));
268270
operation.setParameters(parameters);
269-
return super.fromOperation(path, httpMethod, operation, definitions);
271+
return super.fromOperation(path, httpMethod, operation, definitions, swagger);
270272
}
271273

272274
private static String normalizePath(String path) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ public class ApiClient {
4949
for(String authName : authNames) {
5050
if (apiAuthorizations.containsKey(authName)) {
5151
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
52-
}
53-
Interceptor auth;{{#authMethods}}
52+
}{{#authMethods}}
53+
Interceptor auth;
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}}
58-
} else {{/authMethods}}{
57+
auth = new OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{#hasMore}}, {{/hasMore}}{{/scopes}}");{{/isOAuth}}
58+
} else {
5959
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
6060
}
61-
apiAuthorizations.put(authName, auth);
61+
apiAuthorizations.put(authName, auth);{{/authMethods}}
6262
}
6363
addAuthsToOkClient(okClient);
6464
}

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}}",

modules/swagger-codegen/src/main/resources/php/ApiClient.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ class ApiClient
239239
$data = $http_body;
240240
}
241241
} else {
242+
$data = json_decode($http_body);
243+
if (json_last_error() > 0) { // if response is a string
244+
$data = $http_body;
245+
}
246+
242247
throw new ApiException(
243248
"[".$response_info['http_code']."] Error connecting to the API ($url)",
244249
$response_info['http_code'], $http_header, $http_body

0 commit comments

Comments
 (0)