Merge pull request #1644 from wing328/objc_special_word

[ObjC] escape objc special words with "var"
This commit is contained in:
wing328 2015-12-02 10:50:34 +08:00
commit 5bb444ba6d
2 changed files with 14 additions and 1 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

@ -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];