update codegen to support global consumes and produces

This commit is contained in:
wing328 2015-10-10 18:39:54 +08:00
parent a5a6ae72c3
commit 9311dcaccb
14 changed files with 52 additions and 60 deletions

View File

@ -67,6 +67,8 @@ public interface CodegenConfig {
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

View File

@ -861,8 +861,12 @@ public class DefaultCodegen {
}
return responses.get(code);
}
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
return fromOperation(path, httpMethod, operation, definitions, null);
}
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
Set<String> imports = new HashSet<String>();
op.vendorExtensions = operation.getVendorExtensions();
@ -899,14 +903,25 @@ public class DefaultCodegen {
op.notes = escapeText(operation.getDescription());
op.tags = operation.getTags();
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
List<String> consumes = new ArrayList<String>();
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
// use consumes defined in the operation
consumes = operation.getConsumes();
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
// use consumes defined globally
consumes = swagger.getConsumes();
LOGGER.debug("Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
}
// if "consumes" is defined (per operation or using global definition)
if (consumes != null && consumes.size() > 0) {
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
int count = 0;
for (String key : operation.getConsumes()) {
for (String key : consumes) {
Map<String, String> mediaType = new HashMap<String, String>();
mediaType.put("mediaType", key);
count += 1;
if (count < operation.getConsumes().size()) {
if (count < consumes.size()) {
mediaType.put("hasMore", "true");
} else {
mediaType.put("hasMore", null);
@ -917,14 +932,25 @@ public class DefaultCodegen {
op.hasConsumes = true;
}
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
List<String> produces = new ArrayList<String>();
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
// use produces defined in the operation
produces = operation.getProduces();
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
// use produces defined globally
produces = swagger.getProduces();
LOGGER.debug("Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
}
// if "produces" is defined (per operation or using global definition)
if (produces != null && produces.size() > 0) {
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
int count = 0;
for (String key : operation.getProduces()) {
for (String key : produces) {
Map<String, String> mediaType = new HashMap<String, String>();
mediaType.put("mediaType", key);
count += 1;
if (count < operation.getProduces().size()) {
if (count < produces.size()) {
mediaType.put("hasMore", "true");
} else {
mediaType.put("hasMore", null);

View File

@ -470,7 +470,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
for (String tag : tags) {
CodegenOperation co = null;
try {
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
co.tags = new ArrayList<String>();
co.tags.add(sanitizeTag(tag));
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);

View File

@ -1,9 +1,11 @@
package io.swagger.codegen.languages;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import io.swagger.codegen.*;
import io.swagger.models.Swagger;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.parameters.HeaderParameter;
@ -256,7 +258,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
}
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
path = normalizePath(path);
List<Parameter> parameters = operation.getParameters();
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
@ -266,7 +268,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
}
}));
operation.setParameters(parameters);
return super.fromOperation(path, httpMethod, operation, definitions);
return super.fromOperation(path, httpMethod, operation, definitions, swagger);
}
private static String normalizePath(String path) {

View File

@ -21,10 +21,10 @@ Pod::Spec.new do |s|
s.framework = 'SystemConfiguration'
s.homepage = ""
s.license = ""
s.source = { :git => ".git", :tag => "#{s.version}" }
s.author = { "" => "" }
s.homepage = "https://github.com/swagger-api/swagger-codegen"
s.license = "MIT"
s.source = { :git => "https://github.com/swagger-api/swagger-codegen.git", :tag => "#{s.version}" }
s.author = { "Swagger" => "apiteam@swagger.io" }
s.source_files = 'SwaggerClient/**/*'
s.public_header_files = 'SwaggerClient/**/*.h'

View File

@ -26,6 +26,6 @@
*/
@property(nonatomic) NSString* status;
@property(nonatomic) NSNumber* complete;
@property(nonatomic) NSString* count;
@end

View File

@ -8,7 +8,7 @@
*/
+ (JSONKeyMapper *)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"complete": @"complete" }];
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"count": @"count" }];
}
/**
@ -18,7 +18,7 @@
*/
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
NSArray *optionalProperties = @[@"_id", @"petId", @"quantity", @"shipDate", @"status", @"complete"];
NSArray *optionalProperties = @[@"_id", @"petId", @"quantity", @"shipDate", @"status", @"count"];
if ([optionalProperties containsObject:propertyName]) {
return YES;

View File

@ -7,8 +7,8 @@
* Do not edit the class manually.
*/
#import "SWGCategory.h"
#import "SWGTag.h"
#import "SWGCategory.h"
@protocol SWGPet

View File

@ -450,7 +450,7 @@ static SWGPetApi* singletonAPI = nil;
NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[]];
// Authentication setting
NSArray *authSettings = @[@"petstore_auth", @"api_key"];
NSArray *authSettings = @[@"api_key", @"petstore_auth"];
id bodyParam = nil;
NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init];

View File

@ -99,7 +99,7 @@
/// Get user by user name
///
///
/// @param username The name that needs to be fetched. Use user1 for testing.
/// @param username The name that needs to be fetched. Use user1 for testing.
///
///
/// @return SWGUser*

View File

@ -470,7 +470,7 @@ static SWGUserApi* singletonAPI = nil;
///
/// Get user by user name
///
/// @param username The name that needs to be fetched. Use user1 for testing.
/// @param username The name that needs to be fetched. Use user1 for testing.
///
/// @returns SWGUser*
///

View File

@ -135,9 +135,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -200,9 +197,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -264,9 +258,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -340,9 +331,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -424,16 +412,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
$apiKey = $this->apiClient->getApiKeyWithPrefix('api_key');
if (isset($apiKey)) {
$headerParams['api_key'] = $apiKey;
}
// make the API Call
try
{
@ -523,9 +501,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -599,9 +574,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{
@ -679,9 +651,6 @@ class PetApi
$httpBody = $formParams; // for HTTP post (form)
}
//TODO support oauth
// make the API Call
try
{

View File

@ -130,13 +130,6 @@ class StoreApi
$httpBody = $formParams; // for HTTP post (form)
}
$apiKey = $this->apiClient->getApiKeyWithPrefix('api_key');
if (isset($apiKey)) {
$headerParams['api_key'] = $apiKey;
}
// make the API Call
try
{

View File

@ -193,7 +193,7 @@ class ObjectSerializer
$deserialized = $values;
} elseif ($class === '\DateTime') {
$deserialized = new \DateTime($data);
} elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) {
} elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) {
settype($data, $class);
$deserialized = $data;
} elseif ($class === '\SplFileObject') {