From f6bc1f52624dc4f91f58798aa9d386836072f779 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 1 Apr 2016 19:12:14 +0800 Subject: [PATCH] tested and fixed sample code for objc --- .../codegen/languages/ObjcClientCodegen.java | 29 ++++---- .../src/main/resources/objc/README.mustache | 69 +++++++++++------- samples/client/petstore/objc/README.md | 72 +++++++++++-------- 3 files changed, 102 insertions(+), 68 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java index d6c6af659097..8b39f90de7b0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java @@ -399,12 +399,12 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String apiDocFileFolder() { - return (outputFolder + File.separatorChar + apiDocPath); + return (outputFolder + "/" + apiDocPath).replace("/", File.separator); } @Override public String modelDocFileFolder() { - return (outputFolder + File.separatorChar + modelDocPath); + return (outputFolder + "/" + modelDocPath).replace("/", File.separator); } @Override @@ -606,15 +606,16 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { type = p.dataType; } - if ("NSString".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { + if ("NSString*".equalsIgnoreCase(type)) { if (example == null) { example = p.paramName + "_example"; } - example = "'" + escapeText(example) + "'"; - } else if ("NSNumber".equals(type)) { + example = "@\"" + escapeText(example) + "\""; + } else if ("NSNumber*".equals(type)) { if (example == null) { example = "56"; } + example = "@" + example; /* OBJC uses NSNumber to represent both int, long, double and float } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { if (example == null) { @@ -628,20 +629,22 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { if (example == null) { example = "/path/to/file"; } - example = "'" + escapeText(example) + "'"; - } else if ("NSDate".equalsIgnoreCase(type)) { + example = "@\"" + escapeText(example) + "\""; + /*} else if ("NSDate".equalsIgnoreCase(type)) { if (example == null) { example = "2013-10-20"; } - example = "'" + escapeText(example) + "'"; - } else if ("DateTime".equalsIgnoreCase(type)) { + example = "'" + escapeText(example) + "'";*/ + } else if ("NSDate*".equalsIgnoreCase(type)) { if (example == null) { example = "2013-10-20T19:20:30+01:00"; } - example = "'" + escapeText(example) + "'"; + example = "@\"" + escapeText(example) + "\""; } else if (!languageSpecificPrimitives.contains(type)) { // type is a model class, e.g. User - example = this.podName+ "." + type + "()"; + type = type.replace("*", ""); + // e.g. [[SWGPet alloc] init + example = "[[" + type + " alloc] init]"; } else { LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); } @@ -649,9 +652,9 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { if (example == null) { example = "NULL"; } else if (Boolean.TRUE.equals(p.isListContainer)) { - example = "[" + example + "]"; + example = "@[" + example + "]"; } else if (Boolean.TRUE.equals(p.isMapContainer)) { - example = "{'key': " + example + "}"; + example = "@{@\"key\" : " + example + "}"; } p.example = example; diff --git a/modules/swagger-codegen/src/main/resources/objc/README.mustache b/modules/swagger-codegen/src/main/resources/objc/README.mustache index 2e6556d41921..735d598b018f 100644 --- a/modules/swagger-codegen/src/main/resources/objc/README.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/README.mustache @@ -18,7 +18,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) The SDK requires [**ARC (Automatic Reference Counting)**](http://stackoverflow.com/questions/7778356/how-to-enable-disable-automatic-reference-counting) to be enabled in the Xcode project. -## Installation +## Installation & Usage ### Install from Github using [CocoaPods](https://cocoapods.org/) Add the following to the Podfile: @@ -33,23 +33,22 @@ To specify a particular commit, append `, :commit => '11aa22'` ### Install from local path using [CocoaPods](https://cocoapods.org/) -Put the SDK under your project folder (e.g. ./vendor/{{podName}}) and then add the following to the Podfile: +Put the SDK under your project folder (e.g. /path/to/objc_project/Vendor/{{podName}}) and then add the following to the Podfile: ```ruby -pod '{{podName}}', :path => './vendor/{{podName}}' +pod '{{podName}}', :path => 'Vendor/{{podName}}' ``` -## Usage +### Usage Import the following: ```objc #import <{{podName}}/{{{classPrefix}}}ApiClient.h> #import <{{podName}}/{{{classPrefix}}}Configuration.h> // load models -{{#models}}{{#model}}#import <{{podName}}/{{{classname}}}> -{{/model}}{{/models}} -// load API classes for accessing endpoints -{{#apiInfo}}{{#apis}}#import <{{podName}}/{{{classname}}}> +{{#models}}{{#model}}#import <{{podName}}/{{{classname}}}.h> +{{/model}}{{/models}}// load API classes for accessing endpoints +{{#apiInfo}}{{#apis}}#import <{{podName}}/{{{classname}}}.h> {{/apis}}{{/apiInfo}} ``` @@ -62,28 +61,48 @@ It's recommended to create an instance of ApiClient per thread in a multi-thread Please follow the [installation procedure](#installation--usage) and then run the following: ```objc - -{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} -// Configure HTTP basic authorization: {{{name}}} -{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setUsername('YOUR_USERNAME'); -{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setPassword('YOUR_PASSWORD');{{/isBasic}}{{#isApiKey}} -// Configure API key authorization: {{{name}}} -{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY'); +{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}} +{{#hasAuthMethods}} +{{classPrefix}}Configuration *apiConfig = [{{classPrefix}}Configuration sharedConfig]; +{{#authMethods}}{{#isBasic}}// Configure HTTP basic authorization (authentication scheme: {{{name}}}) +[apiConfig setUsername:@"YOUR_USERNAME"]; +[apiConfig setPassword:@"YOUR_PASSWORD"]; +{{/isBasic}}{{#isApiKey}} +// Configure API key authorization: (authentication scheme: {{{name}}}) +[apiConfig setApiKey:@"YOUR_API_KEY" forApiKeyIdentifier:@"{{{keyParamName}}}"]; // Uncomment below to setup prefix (e.g. BEARER) for API key, if needed -// {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'BEARER');{{/isApiKey}}{{#isOAuth}} -// Configure OAuth2 access token for authorization: {{{name}}} -{{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');{{/isOAuth}}{{/authMethods}} +//[apiConfig setApiKeyPrefix:@"BEARER" forApiKeyIdentifier:@"{{{keyParamName}}}"]; +{{/isApiKey}}{{#isOAuth}} +// Configure OAuth2 access token for authorization: (authentication scheme: {{{name}}}) +[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"]; +{{/isOAuth}}{{/authMethods}} {{/hasAuthMethods}} -$api_instance = new {{invokerPackage}}\Api\{{classname}}(); -{{#allParams}}${{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}} +{{#allParams}}{{{dataType}}} *{{paramName}} = {{{example}}}; // {{{description}}}{{#optional}} (optional){{/optional}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}} {{/allParams}} -try { - {{#returnType}}$result = {{/returnType}}$api_instance->{{{operationId}}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}} - print_r($result);{{/returnType}} -} catch (Exception $e) { - echo 'Exception when calling {{classname}}->{{operationId}}: ', $e->getMessage(), "\n"; +@try +{ + {{classname}} *apiInstance = [[{{classname}} alloc] init]; + +{{#summary}} // {{{.}}} +{{/summary}} [apiInstance {{#vendorExtensions.x-objc-operationId}}{{vendorExtensions.x-objc-operationId}}{{/vendorExtensions.x-objc-operationId}}{{^vendorExtensions.x-objc-operationId}}{{nickname}}{{#hasParams}}With{{vendorExtensions.firstParamAltName}}{{/hasParams}}{{^hasParams}}WithCompletionHandler: {{/hasParams}}{{/vendorExtensions.x-objc-operationId}}{{#allParams}}{{#secondaryParam}} + {{paramName}}{{/secondaryParam}}:{{paramName}}{{/allParams}} + {{#hasParams}}completionHandler: {{/hasParams}}^({{#returnBaseType}}{{{returnType}}} output, {{/returnBaseType}}NSError* error)) { +{{#returnType}} + if (output) { + NSLog(@"%@", output); + } +{{/returnType}} + if (error) { + NSLog(@"Error: %@", error); + } + }]; +} +@catch (NSException *exception) +{ + NSLog(@"Exception when calling {{classname}}->{{operationId}}: %@ ", exception.name); + NSLog(@"Reason: %@ ", exception.reason); } {{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}} ``` diff --git a/samples/client/petstore/objc/README.md b/samples/client/petstore/objc/README.md index ecc7b026ef97..2c2676aba917 100644 --- a/samples/client/petstore/objc/README.md +++ b/samples/client/petstore/objc/README.md @@ -6,14 +6,14 @@ This ObjC package is automatically generated by the [Swagger Codegen](https://gi - API version: 1.0.0 - Package version: -- Build date: 2016-04-01T12:02:50.753+08:00 +- Build date: 2016-04-01T18:59:36.262+08:00 - Build package: class io.swagger.codegen.languages.ObjcClientCodegen ## Requirements The SDK requires [**ARC (Automatic Reference Counting)**](http://stackoverflow.com/questions/7778356/how-to-enable-disable-automatic-reference-counting) to be enabled in the Xcode project. -## Installation +## Installation & Usage ### Install from Github using [CocoaPods](https://cocoapods.org/) Add the following to the Podfile: @@ -28,37 +28,36 @@ To specify a particular commit, append `, :commit => '11aa22'` ### Install from local path using [CocoaPods](https://cocoapods.org/) -Put the SDK under your project folder (e.g. ./vendor/SwaggerClient) and then add the following to the Podfile: +Put the SDK under your project folder (e.g. /path/to/objc_project/Vendor/SwaggerClient) and then add the following to the Podfile: ```ruby -pod 'SwaggerClient', :path => './vendor/SwaggerClient' +pod 'SwaggerClient', :path => 'Vendor/SwaggerClient' ``` -## Usage +### Usage Import the following: ```objc #import #import // load models -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import - +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import // load API classes for accessing endpoints -#import -#import -#import +#import +#import +#import ``` @@ -72,17 +71,30 @@ Please follow the [installation procedure](#installation--usage) and then run th ```objc +SWGConfiguration *apiConfig = [SWGConfiguration sharedConfig]; -// Configure OAuth2 access token for authorization: petstore_auth -\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN'); +// Configure OAuth2 access token for authorization: (authentication scheme: petstore_auth) +[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"]; -$api_instance = new \Api\SWGPetApi(); -$body = SwaggerClient.SWGPet*(); // SWGPet* | Pet object that needs to be added to the store -try { - $api_instance->addPet($body); -} catch (Exception $e) { - echo 'Exception when calling SWGPetApi->addPet: ', $e->getMessage(), "\n"; +SWGPet* *body = [[SWGPet alloc] init]; // Pet object that needs to be added to the store + +@try +{ + SWGPetApi *apiInstance = [[SWGPetApi alloc] init]; + + // Add a new pet to the store + [apiInstance addPetWithBody:body + completionHandler: ^(NSError* error)) { + if (error) { + NSLog(@"Error: %@", error); + } + }]; +} +@catch (NSException *exception) +{ + NSLog(@"Exception when calling SWGPetApi->addPet: %@ ", exception.name); + NSLog(@"Reason: %@ ", exception.reason); } ```