escape objc special word with var

This commit is contained in:
wing328 2015-12-01 17:44:07 +08:00
parent a78f33581a
commit c64f352ba6
5 changed files with 28 additions and 3 deletions

View File

@ -34,6 +34,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String authorEmail = "apiteam@swagger.io";
protected String license = "MIT";
protected String gitRepoURL = "https://github.com/swagger-api/swagger-codegen";
protected String[] specialWords = {"new", "copy"};
public ObjcClientCodegen() {
super();
@ -86,6 +87,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("object", "NSObject");
typeMapping.put("file", "NSURL");
// ref: http://www.tutorialspoint.com/objective_c/objective_c_basic_syntax.htm
reservedWords = new HashSet<String>(
Arrays.asList(
@ -375,6 +377,12 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
return name;
}
// if name starting with special word, escape with '_'
for(int i =0; i < specialWords.length; i++) {
if (name.matches("(?i:^" + specialWords[i] + ".*)"))
name = escapeSpecialWord(name);
}
// camelize (lower first character) the variable name
// e.g. `pet_id` to `petId`
name = camelize(name, true);
@ -384,6 +392,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
name = escapeReservedWord(name);
}
return name;
}
@ -397,6 +406,10 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
return "_" + name;
}
public String escapeSpecialWord(String name) {
return "var_" + name;
}
@Override
public String toOperationId(String operationId) {
// throw exception if method name is empty

View File

@ -944,6 +944,14 @@
"type": "integer",
"format": "int64"
},
"CopyPassword": {
"type": "integer",
"format": "int64"
},
"newPassword": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"

View File

@ -17,6 +17,10 @@
@property(nonatomic) NSNumber* _id;
@property(nonatomic) NSNumber* varCopyPassword;
@property(nonatomic) NSNumber* varNewPassword;
@property(nonatomic) NSNumber* petId;
@property(nonatomic) NSNumber* quantity;

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", @"CopyPassword": @"varCopyPassword", @"newPassword": @"varNewPassword", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"complete": @"complete" }];
}
/**
@ -18,7 +18,7 @@
*/
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
NSArray *optionalProperties = @[@"_id", @"petId", @"quantity", @"shipDate", @"status", @"complete"];
NSArray *optionalProperties = @[@"_id", @"varCopyPassword", @"varNewPassword", @"petId", @"quantity", @"shipDate", @"status", @"complete"];
if ([optionalProperties containsObject:propertyName]) {
return YES;

View File

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