Objective c generator converts map and array properties to proper format using generics

This commit is contained in:
Mateusz Mackowiak 2016-05-07 14:06:20 +02:00
parent 084f15fc2e
commit 07b466a291
15 changed files with 97 additions and 40 deletions

View File

@ -39,6 +39,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String[] specialWords = {"new", "copy"};
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected Set<String> advancedMapingTypes = new HashSet<String>();
public ObjcClientCodegen() {
super();
@ -65,6 +67,16 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
defaultIncludes.add("NSDictionary");
defaultIncludes.add("NSMutableArray");
defaultIncludes.add("NSMutableDictionary");
advancedMapingTypes.add("NSDictionary");
advancedMapingTypes.add("NSArray");
advancedMapingTypes.add("NSMutableArray");
advancedMapingTypes.add("NSMutableDictionary");
advancedMapingTypes.add("NSObject");
advancedMapingTypes.add("NSNumber");
advancedMapingTypes.add("NSURL");
advancedMapingTypes.add("NSString");
advancedMapingTypes.add("NSDate");
languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("NSNumber");
@ -284,15 +296,20 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
if (innerTypeDeclaration.endsWith("*")) {
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
}
// In this codition, type of property p is array of primitive,
// return container type with pointer, e.g. `NSArray* /* NSString */'
if (languageSpecificPrimitives.contains(innerType)) {
return getSwaggerType(p) + "*" + " /* " + innerTypeDeclaration + " */";
// return container type with pointer, e.g. `NSArray*<NSString*>*'
if (languageSpecificPrimitives.contains(innerTypeDeclaration)) {
return getSwaggerType(p) + "<" + innerTypeDeclaration + "*>*";
}
// In this codition, type of property p is array of model,
// return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
else {
for (String sd : advancedMapingTypes) {
if(innerTypeDeclaration.startsWith(sd)) {
return getSwaggerType(p) + "<" + innerTypeDeclaration + "*>*";
}
}
return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
}
} else if (p instanceof MapProperty) {
@ -300,11 +317,20 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
Property inner = mp.getAdditionalProperties();
String innerTypeDeclaration = getTypeDeclaration(inner);
if (innerTypeDeclaration.endsWith("*")) {
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
}
return getSwaggerType(p) + "* /* NSString, " + innerTypeDeclaration + " */";
if (languageSpecificPrimitives.contains(innerTypeDeclaration)) {
return getSwaggerType(p) + "<NSString*, " + innerTypeDeclaration + "*>*";
} else {
for (String s : advancedMapingTypes) {
if(innerTypeDeclaration.startsWith(s)) {
return getSwaggerType(p) + "<NSString*, " + innerTypeDeclaration + "*>*";
}
}
return getSwaggerType(p) + "<NSString*, " + innerTypeDeclaration + ">*";
}
} else {
String swaggerType = getSwaggerType(p);

View File

@ -28,7 +28,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
/**
* Log debug message macro
*/
#define {{classPrefix}}DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
#ifndef {{classPrefix}}DebugLog
#define {{classPrefix}}DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
#endif
@interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager

View File

@ -27,6 +27,31 @@ import java.util.Map;
@SuppressWarnings("static-method")
public class ObjcModelTest {
@Test(description = "convert a model with a advanced map property")
public void advancedMapPropertyTest() {
final Model model = new ModelImpl()
.description("a sample model")
.property("translations", new MapProperty()
.additionalProperties(new MapProperty().additionalProperties(new StringProperty())))
.required("id");
final DefaultCodegen codegen = new ObjcClientCodegen();
final CodegenModel cm = codegen.fromModel("sample", model);
Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "SWGSample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 1);
final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "translations");
Assert.assertEquals(property1.datatype, "NSDictionary<NSString*, NSDictionary<NSString*, NSString*>*>*");
Assert.assertEquals(property1.name, "translations");
Assert.assertEquals(property1.baseType, "NSDictionary");
Assert.assertEquals(property1.containerType, "map");
Assert.assertNull(property1.required);
Assert.assertTrue(property1.isContainer);
}
@Test(description = "convert a simple java model")
public void simpleModelTest() {
final Model model = new ModelImpl()
@ -108,7 +133,7 @@ public class ObjcModelTest {
final CodegenProperty property2 = cm.vars.get(1);
Assert.assertEquals(property2.baseName, "urls");
Assert.assertEquals(property2.datatype, "NSArray* /* NSString */");
Assert.assertEquals(property2.datatype, "NSArray<NSString*>*");
Assert.assertEquals(property2.name, "urls");
Assert.assertNull(property2.defaultValue);
Assert.assertEquals(property2.baseType, "NSArray");
@ -136,7 +161,7 @@ public class ObjcModelTest {
final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "translations");
Assert.assertEquals(property1.datatype, "NSDictionary* /* NSString, NSString */");
Assert.assertEquals(property1.datatype, "NSDictionary<NSString*, NSString*>*");
Assert.assertEquals(property1.name, "translations");
Assert.assertEquals(property1.baseType, "NSDictionary");
Assert.assertEquals(property1.containerType, "map");
@ -145,6 +170,7 @@ public class ObjcModelTest {
Assert.assertTrue(property1.isPrimitiveType);
}
@Test(description = "convert a model with complex property")
public void complexPropertyTest() {
final Model model = new ModelImpl()
@ -210,7 +236,7 @@ public class ObjcModelTest {
final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "children");
Assert.assertEquals(property1.complexType, "SWGChildren");
Assert.assertEquals(property1.datatype, "NSDictionary* /* NSString, SWGChildren */");
Assert.assertEquals(property1.datatype, "NSDictionary<NSString*, SWGChildren>*");
Assert.assertEquals(property1.name, "children");
Assert.assertEquals(property1.baseType, "NSDictionary");
Assert.assertEquals(property1.containerType, "map");

View File

@ -6,7 +6,7 @@ This ObjC package is automatically generated by the [Swagger Codegen](https://gi
- API version: 1.0.0
- Package version:
- Build date: 2016-05-06T12:20:47.112+02:00
- Build date: 2016-05-06T14:56:38.502+02:00
- Build package: class io.swagger.codegen.languages.ObjcClientCodegen
## Requirements

View File

@ -32,7 +32,9 @@ extern NSString *const SWGResponseObjectErrorKey;
/**
* Log debug message macro
*/
#define SWGDebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
#ifndef SWGDebugLog
#define SWGDebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
#endif
@interface SWGApiClient : AFHTTPRequestOperationManager

View File

@ -23,7 +23,7 @@
@property(nonatomic) NSString* name;
@property(nonatomic) NSArray* /* NSString */ photoUrls;
@property(nonatomic) NSArray<NSString*>* photoUrls;
@property(nonatomic) NSArray<SWGTag>* tags;
/* pet status in the store [optional]

View File

@ -56,7 +56,7 @@
///
///
/// @return NSArray<SWGPet>*
-(NSNumber*) findPetsByStatusWithStatus: (NSArray* /* NSString */) status
-(NSNumber*) findPetsByStatusWithStatus: (NSArray<NSString*>*) status
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler;
@ -69,7 +69,7 @@
///
///
/// @return NSArray<SWGPet>*
-(NSNumber*) findPetsByTagsWithTags: (NSArray* /* NSString */) tags
-(NSNumber*) findPetsByTagsWithTags: (NSArray<NSString*>*) tags
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler;

View File

@ -217,7 +217,7 @@ static SWGPetApi* singletonAPI = nil;
///
/// @returns NSArray<SWGPet>*
///
-(NSNumber*) findPetsByStatusWithStatus: (NSArray* /* NSString */) status
-(NSNumber*) findPetsByStatusWithStatus: (NSArray<NSString*>*) status
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler {
NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"/pet/findByStatus"];
@ -284,7 +284,7 @@ static SWGPetApi* singletonAPI = nil;
///
/// @returns NSArray<SWGPet>*
///
-(NSNumber*) findPetsByTagsWithTags: (NSArray* /* NSString */) tags
-(NSNumber*) findPetsByTagsWithTags: (NSArray<NSString*>*) tags
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler {
NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"/pet/findByTags"];

View File

@ -39,9 +39,9 @@
///
///
///
/// @return NSDictionary* /* NSString, NSNumber */
/// @return NSDictionary<NSString*, NSNumber*>*
-(NSNumber*) getInventoryWithCompletionHandler:
(void (^)(NSDictionary* /* NSString, NSNumber */ output, NSError* error)) handler;
(void (^)(NSDictionary<NSString*, NSNumber*>* output, NSError* error)) handler;
///

View File

@ -141,10 +141,10 @@ static SWGStoreApi* singletonAPI = nil;
///
/// Returns pet inventories by status
/// Returns a map of status codes to quantities
/// @returns NSDictionary* /* NSString, NSNumber */
/// @returns NSDictionary<NSString*, NSNumber*>*
///
-(NSNumber*) getInventoryWithCompletionHandler:
(void (^)(NSDictionary* /* NSString, NSNumber */ output, NSError* error)) handler {
(void (^)(NSDictionary<NSString*, NSNumber*>* output, NSError* error)) handler {
NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"/store/inventory"];
// remove format in URL if needed
@ -192,9 +192,9 @@ static SWGStoreApi* singletonAPI = nil;
authSettings: authSettings
requestContentType: requestContentType
responseContentType: responseContentType
responseType: @"NSDictionary* /* NSString, NSNumber */"
responseType: @"NSDictionary<NSString*, NSNumber*>*"
completionBlock: ^(id data, NSError *error) {
handler((NSDictionary* /* NSString, NSNumber */)data, error);
handler((NSDictionary<NSString*, NSNumber*>*)data, error);
}
];
}

View File

@ -27,16 +27,11 @@
SWGPetApi *api = [[SWGPetApi alloc] init];
NSURL *file = [NSURL fileURLWithPath:@"/Users/geekerzp/tmp/test.jpg"];
[api uploadFileWithPetId:@2 additionalMetadata:@2 file:file completionHandler:^(NSError *error) {
[api uploadFileWithPetId:@2 additionalMetadata:@"2" file:file completionHandler:^(NSError *error) {
NSLog(@"*** error: %@", error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (SWGPet*) createPet {
SWGPet * pet = [[SWGPet alloc] init];
pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]];

View File

@ -138,7 +138,13 @@
XCTAssertTrue([result isKindOfClass:[NSArray class]]);
XCTAssertTrue([[result firstObject] isKindOfClass:[SWGPet class]]);
XCTAssertEqualObjects([[result firstObject] _id], @119);
SWGPet*pet = [result firstObject];
XCTAssertEqualObjects([pet.photoUrls firstObject],@"string");
XCTAssertTrue([[pet.tags firstObject] isKindOfClass:[SWGTag class]]);
SWGTag* tag = [pet.tags firstObject];
XCTAssertEqualObjects(tag._id, @0);
XCTAssertEqualObjects(tag.name, @"string");
XCTAssertEqualObjects(pet._id, @119);
}
- (void)testDeserializeMapOfModels {

View File

@ -6,7 +6,7 @@ Name | Type | Description | Notes
**_id** | **NSNumber*** | | [optional]
**category** | [**SWGCategory***](SWGCategory.md) | | [optional]
**name** | **NSString*** | |
**photoUrls** | **NSArray* /* NSString */** | |
**photoUrls** | **NSArray&lt;NSString*&gt;*** | |
**tags** | [**NSArray&lt;SWGTag&gt;***](SWGTag.md) | | [optional]
**status** | **NSString*** | pet status in the store | [optional]

View File

@ -140,7 +140,7 @@ void (empty response body)
# **findPetsByStatus**
```objc
-(NSNumber*) findPetsByStatusWithStatus: (NSArray* /* NSString */) status
-(NSNumber*) findPetsByStatusWithStatus: (NSArray<NSString*>*) status
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler;
```
@ -156,7 +156,7 @@ SWGConfiguration *apiConfig = [SWGConfiguration sharedConfig];
[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"];
NSArray* /* NSString */ status = @[@"available"]; // Status values that need to be considered for filter (optional) (default to available)
NSArray<NSString*>* status = @[@"available"]; // Status values that need to be considered for filter (optional) (default to available)
@try
{
@ -184,7 +184,7 @@ NSArray* /* NSString */ status = @[@"available"]; // Status values that need to
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**status** | [**NSArray* /* NSString */**](NSString*.md)| Status values that need to be considered for filter | [optional] [default to available]
**status** | [**NSArray&lt;NSString*&gt;***](NSString*.md)| Status values that need to be considered for filter | [optional] [default to available]
### Return type
@ -203,7 +203,7 @@ Name | Type | Description | Notes
# **findPetsByTags**
```objc
-(NSNumber*) findPetsByTagsWithTags: (NSArray* /* NSString */) tags
-(NSNumber*) findPetsByTagsWithTags: (NSArray<NSString*>*) tags
completionHandler: (void (^)(NSArray<SWGPet>* output, NSError* error)) handler;
```
@ -219,7 +219,7 @@ SWGConfiguration *apiConfig = [SWGConfiguration sharedConfig];
[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"];
NSArray* /* NSString */ tags = @[@"tags_example"]; // Tags to filter by (optional)
NSArray<NSString*>* tags = @[@"tags_example"]; // Tags to filter by (optional)
@try
{
@ -247,7 +247,7 @@ NSArray* /* NSString */ tags = @[@"tags_example"]; // Tags to filter by (optiona
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**tags** | [**NSArray* /* NSString */**](NSString*.md)| Tags to filter by | [optional]
**tags** | [**NSArray&lt;NSString*&gt;***](NSString*.md)| Tags to filter by | [optional]
### Return type

View File

@ -68,7 +68,7 @@ No authorization required
# **getInventory**
```objc
-(NSNumber*) getInventoryWithCompletionHandler:
(void (^)(NSDictionary* /* NSString, NSNumber */ output, NSError* error)) handler;
(void (^)(NSDictionary<NSString*, NSNumber*>* output, NSError* error)) handler;
```
Returns pet inventories by status
@ -92,7 +92,7 @@ SWGConfiguration *apiConfig = [SWGConfiguration sharedConfig];
// Returns pet inventories by status
[apiInstance getInventoryWithCompletionHandler:
^(NSDictionary* /* NSString, NSNumber */ output, NSError* error) {
^(NSDictionary<NSString*, NSNumber*>* output, NSError* error) {
if (output) {
NSLog(@"%@", output);
}
@ -113,7 +113,7 @@ This endpoint does not need any parameter.
### Return type
[**NSDictionary* /* NSString, NSNumber */**](NSDictionary.md)
[**NSDictionary<NSString*, NSNumber*>***](NSDictionary.md)
### Authorization