Skip to content

Commit 9311dca

Browse files
committed
update codegen to support global consumes and produces
1 parent a5a6ae7 commit 9311dca

File tree

14 files changed

+52
-60
lines changed

14 files changed

+52
-60
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/DefaultCodegen.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,12 @@ private Response findMethodResponse(Map<String, Response> responses) {
861861
}
862862
return responses.get(code);
863863
}
864-
864+
865865
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
866+
return fromOperation(path, httpMethod, operation, definitions, null);
867+
}
868+
869+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
866870
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
867871
Set<String> imports = new HashSet<String>();
868872
op.vendorExtensions = operation.getVendorExtensions();
@@ -899,14 +903,25 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
899903
op.notes = escapeText(operation.getDescription());
900904
op.tags = operation.getTags();
901905

902-
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
906+
List<String> consumes = new ArrayList<String>();
907+
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
908+
// use consumes defined in the operation
909+
consumes = operation.getConsumes();
910+
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
911+
// use consumes defined globally
912+
consumes = swagger.getConsumes();
913+
LOGGER.debug("Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
914+
}
915+
916+
// if "consumes" is defined (per operation or using global definition)
917+
if (consumes != null && consumes.size() > 0) {
903918
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
904919
int count = 0;
905-
for (String key : operation.getConsumes()) {
920+
for (String key : consumes) {
906921
Map<String, String> mediaType = new HashMap<String, String>();
907922
mediaType.put("mediaType", key);
908923
count += 1;
909-
if (count < operation.getConsumes().size()) {
924+
if (count < consumes.size()) {
910925
mediaType.put("hasMore", "true");
911926
} else {
912927
mediaType.put("hasMore", null);
@@ -917,14 +932,25 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
917932
op.hasConsumes = true;
918933
}
919934

920-
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
935+
List<String> produces = new ArrayList<String>();
936+
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
937+
// use produces defined in the operation
938+
produces = operation.getProduces();
939+
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
940+
// use produces defined globally
941+
produces = swagger.getProduces();
942+
LOGGER.debug("Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
943+
}
944+
945+
// if "produces" is defined (per operation or using global definition)
946+
if (produces != null && produces.size() > 0) {
921947
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
922948
int count = 0;
923-
for (String key : operation.getProduces()) {
949+
for (String key : produces) {
924950
Map<String, String> mediaType = new HashMap<String, String>();
925951
mediaType.put("mediaType", key);
926952
count += 1;
927-
if (count < operation.getProduces().size()) {
953+
if (count < produces.size()) {
928954
mediaType.put("hasMore", "true");
929955
} else {
930956
mediaType.put("hasMore", null);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
470470
for (String tag : tags) {
471471
CodegenOperation co = null;
472472
try {
473-
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
473+
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
474474
co.tags = new ArrayList<String>();
475475
co.tags.add(sanitizeTag(tag));
476476
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);

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

samples/client/petstore/objc/SwaggerClient.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Pod::Spec.new do |s|
2121

2222
s.framework = 'SystemConfiguration'
2323

24-
s.homepage = ""
25-
s.license = ""
26-
s.source = { :git => ".git", :tag => "#{s.version}" }
27-
s.author = { "" => "" }
24+
s.homepage = "https://github.com/swagger-api/swagger-codegen"
25+
s.license = "MIT"
26+
s.source = { :git => "https://github.com/swagger-api/swagger-codegen.git", :tag => "#{s.version}" }
27+
s.author = { "Swagger" => "[email protected]" }
2828

2929
s.source_files = 'SwaggerClient/**/*'
3030
s.public_header_files = 'SwaggerClient/**/*.h'

samples/client/petstore/objc/SwaggerClient/SWGOrder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
*/
2727
@property(nonatomic) NSString* status;
2828

29-
@property(nonatomic) NSNumber* complete;
29+
@property(nonatomic) NSString* count;
3030

3131
@end

samples/client/petstore/objc/SwaggerClient/SWGOrder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ @implementation SWGOrder
88
*/
99
+ (JSONKeyMapper *)keyMapper
1010
{
11-
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"complete": @"complete" }];
11+
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"count": @"count" }];
1212
}
1313

1414
/**
@@ -18,7 +18,7 @@ + (JSONKeyMapper *)keyMapper
1818
*/
1919
+ (BOOL)propertyIsOptional:(NSString *)propertyName
2020
{
21-
NSArray *optionalProperties = @[@"_id", @"petId", @"quantity", @"shipDate", @"status", @"complete"];
21+
NSArray *optionalProperties = @[@"_id", @"petId", @"quantity", @"shipDate", @"status", @"count"];
2222

2323
if ([optionalProperties containsObject:propertyName]) {
2424
return YES;

samples/client/petstore/objc/SwaggerClient/SWGPet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* Do not edit the class manually.
88
*/
99

10-
#import "SWGCategory.h"
1110
#import "SWGTag.h"
11+
#import "SWGCategory.h"
1212

1313

1414
@protocol SWGPet

samples/client/petstore/objc/SwaggerClient/SWGPetApi.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ -(NSNumber*) getPetByIdWithCompletionBlock: (NSNumber*) petId
450450
NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[]];
451451

452452
// Authentication setting
453-
NSArray *authSettings = @[@"petstore_auth", @"api_key"];
453+
NSArray *authSettings = @[@"api_key", @"petstore_auth"];
454454

455455
id bodyParam = nil;
456456
NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init];

samples/client/petstore/objc/SwaggerClient/SWGUserApi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
/// Get user by user name
100100
///
101101
///
102-
/// @param username The name that needs to be fetched. Use user1 for testing.
102+
/// @param username The name that needs to be fetched. Use user1 for testing.
103103
///
104104
///
105105
/// @return SWGUser*

0 commit comments

Comments
 (0)