Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/objc-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/objc -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l objc -o samples/client/petstore/objc"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/objc -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l objc -o samples/client/petstore/objc --additional-properties coreData=true"

java -DappName=PetstoreClient $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package io.swagger.codegen.languages;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.*;
import io.swagger.models.Model;
import io.swagger.models.properties.*;

import java.io.File;
Expand All @@ -28,9 +21,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String AUTHOR_EMAIL = "authorEmail";
public static final String LICENSE = "license";
public static final String GIT_REPO_URL = "gitRepoURL";

public static final String DEFAULT_LICENSE = "Apache License, Version 2.0";

public static final String CORE_DATA = "coreData";
public static final String BinaryDataType = "ObjcClientCodegenBinaryData";

protected Set<String> foundationClasses = new HashSet<String>();
Expand All @@ -47,6 +39,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String modelFilesPath = "Model/";
protected String coreFilesPath = "Core/";
protected String apiFilesPath = "Api/";

protected boolean generateCoreData = false;

protected Set<String> advancedMapingTypes = new HashSet<String>();

Expand Down Expand Up @@ -75,6 +69,7 @@ public ObjcClientCodegen() {
defaultIncludes.add("NSDictionary");
defaultIncludes.add("NSMutableArray");
defaultIncludes.add("NSMutableDictionary");
defaultIncludes.add("NSManagedObject");

defaultIncludes.add(BinaryDataType);

Expand Down Expand Up @@ -156,6 +151,7 @@ public ObjcClientCodegen() {
instantiationTypes.put("map", "NSMutableDictionary");

cliOptions.clear();
cliOptions.add(new CliOption(CORE_DATA, "Should generate core data models").defaultValue("false"));
cliOptions.add(new CliOption(CLASS_PREFIX, "prefix for generated classes (convention: Abbreviation of pod name e.g. `HN` for `HackerNews`).`")
.defaultValue("SWG"));
cliOptions.add(new CliOption(POD_NAME, "cocoapods package name (convention: CameCase).")
Expand Down Expand Up @@ -195,6 +191,12 @@ public void processOpts() {
setPodVersion((String) additionalProperties.get(CodegenConstants.POD_VERSION));
}

if (additionalProperties.containsKey(CORE_DATA)) {
Object coreData = additionalProperties.get(CORE_DATA);
if(((String)coreData).equalsIgnoreCase("true")) {
generateCoreData = true;
}
}
if (additionalProperties.containsKey(CLASS_PREFIX)) {
setClassPrefix((String) additionalProperties.get(CLASS_PREFIX));
}
Expand All @@ -211,6 +213,13 @@ public void processOpts() {
setGitRepoURL((String) additionalProperties.get(GIT_REPO_URL));
}

if(generateCoreData) {
modelTemplateFiles.put("NSManagedObject-header.mustache", "ManagedObject.h");
modelTemplateFiles.put("NSManagedObject-body.mustache", "ManagedObject.m");
modelTemplateFiles.put("NSManagedObjectBuilder-header.mustache", "ManagedObjectBuilder.h");
modelTemplateFiles.put("NSManagedObjectBuilder-body.mustache", "ManagedObjectBuilder.m");
}

additionalProperties.put(POD_NAME, podName);
additionalProperties.put(CodegenConstants.POD_VERSION, podVersion);
additionalProperties.put(CLASS_PREFIX, classPrefix);
Expand All @@ -222,6 +231,7 @@ public void processOpts() {
// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
additionalProperties.put("useCoreData", generateCoreData);

modelPackage = podName;
apiPackage = podName;
Expand Down Expand Up @@ -251,6 +261,11 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));

if(generateCoreData) {
supportingFiles.add(new SupportingFile("xccurrentversion.mustache", (modelPackage() + "/" + modelFilesPath + "/").replace("/", File.separator) + classPrefix + "Model.xcdatamodeld", ".xccurrentversion"));
supportingFiles.add(new SupportingFile("Model.xcdatamodel.mustache",(modelPackage() + "/" + modelFilesPath + "/").replace("/", File.separator) + classPrefix + "Model.xcdatamodeld" + File.separator + classPrefix + "Model.xcdatamodel", "contents"));
}
}

@Override
Expand Down Expand Up @@ -288,6 +303,15 @@ public String getSwaggerType(Property p) {
return toModelNameWithoutReservedWordCheck(type);
}

public CodegenProperty coreDatafromProperty(String name, Property p) {
CodegenProperty property = fromProperty(name, p);
if(!generateCoreData) {
return property;
}
property.baseType = getTypeCoreDataDeclaration(p);
return property;
}

@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
Expand Down Expand Up @@ -359,6 +383,77 @@ else if (languageSpecificPrimitives.contains(swaggerType)) {
}
}


public String getTypeCoreDataDeclaration(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
String innerType = getSwaggerType(inner);

String innerTypeDeclaration = getTypeDeclaration(inner);
if (innerTypeDeclaration.endsWith("*")) {
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
}

if(innerTypeDeclaration.equalsIgnoreCase(BinaryDataType)) {
return "NSData*";
}
// In this codition, type of property p is array of primitive,
// 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) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();

String innerTypeDeclaration = getTypeDeclaration(inner);

if (innerTypeDeclaration.endsWith("*")) {
innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
}
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);

// In this codition, type of p is objective-c primitive type, e.g. `NSSNumber',
// return type of p with pointer, e.g. `NSNumber*'
if (languageSpecificPrimitives.contains(swaggerType) &&
foundationClasses.contains(swaggerType)) {
return swaggerType + "*";
}
// In this codition, type of p is c primitive type, e.g. `bool',
// return type of p, e.g. `bool'
else if (languageSpecificPrimitives.contains(swaggerType)) {
return swaggerType;
}
// In this codition, type of p is objective-c object type, e.g. `SWGPet',
// return type of p with pointer, e.g. `SWGPet*'
else {
return swaggerType + "*";
}
}
}

@Override
public String toModelName(String type) {
// model name cannot use reserved keyword
Expand Down Expand Up @@ -579,6 +674,12 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
return objs;
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property){
super.postProcessModelProperty(model,property);
property.vendorExtensions.put("x-uppercaseName", camelize(property.name));
}

/**
* Return the default value of the property
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#import "{{classPrefix}}JSONResponseSerializer.h"

static BOOL JSONParseError(NSError *error) {
if ([error.domain isEqualToString:NSCocoaErrorDomain] && error.code == 3840) {
return YES;
}

return NO;
}

@implementation {{classPrefix}}JSONResponseSerializer

///
Expand All @@ -17,7 +9,7 @@ static BOOL JSONParseError(NSError *error) {
///
/// @param response The response to be processed.
/// @param data The response data to be decoded.
/// @param error The error that occurred while attempting to decode the respnse data.
/// @param error The error that occurred while attempting to decode the response data.
///
/// @return The object decoded from the specified response data.
///
Expand All @@ -27,7 +19,7 @@ static BOOL JSONParseError(NSError *error) {
NSDictionary *responseJson = [super responseObjectForResponse:response data:data error:error];

// if response data is not a valid json, return string of data.
if (JSONParseError(*error)) {
if ([self isParseError:*error]) {
*error = nil;
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return responseString;
Expand All @@ -36,4 +28,12 @@ static BOOL JSONParseError(NSError *error) {
return responseJson;
}

-(BOOL)isParseError:(NSError *)error {
return [error.domain isEqualToString:NSCocoaErrorDomain] && error.code == 3840;
}

+ (instancetype)serializer {
return [self serializerWithReadingOptions:NSJSONReadingAllowFragments];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="9525" systemVersion="15D21" minimumToolsVersion="Xcode 7.0">

{{#models}}{{#model}}<entity name="{{{classname}}}ManagedObject" representedClassName="{{{classname}}}ManagedObject" syncable="YES">
{{#vars}}{{#complexType}} <relationship name="{{name}}" {{^required}}optional="YES"{{/required}} {{#isListContainer}}toMany="YES"{{/isListContainer}}{{^isListContainer}}maxCount="1"{{/isListContainer}} deletionRule="Cascade" destinationEntity="{{{complexType}}}ManagedObject" syncable="YES"/>{{/complexType}}{{^complexType}} <attribute name="{{name}}" {{^required}}optional="YES"{{/required}} attributeType="{{#isString}}String{{/isString}}{{#isInteger}}Integer 16{{/isInteger}}{{#isLong}}Double{{/isLong}}{{#isDouble}}Double{{/isDouble}}{{#isFloat}}Float{{/isFloat}}{{#isByteArray}}Binary{{/isByteArray}}{{#isBinary}}Binary{{/isBinary}}{{#isListContainer}}Transformable{{/isListContainer}}{{#isMapContainer}}Transformable{{/isMapContainer}}{{#isBoolean}}Boolean{{/isBoolean}}{{#isDate}}Date{{/isDate}}{{#isDateTime}}Date{{/isDateTime}}" syncable="YES"/>{{/complexType}}
{{/vars}}
</entity>
{{/model}}{{/models}}
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{#models}}
{{#model}}
#import "{{classname}}ManagedObject.h"

/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/
@implementation {{classname}}ManagedObject

{{#vars}}
@dynamic {{name}};
{{/vars}}

{{/model}}
@end
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/

{{#imports}}#import "{{import}}ManagedObject.h"
{{/imports}}
{{newline}}
{{#models}}
{{#model}}

NS_ASSUME_NONNULL_BEGIN

@interface {{classname}}ManagedObject : {{#parent}}{{{parent}}}{{/parent}}{{^parent}}NSManagedObject{{/parent}}

{{#vars}}
{{#description}}/* {{{description}}} {{^required}}[optional]{{/required}}
*/{{/description}}
@property (nullable, nonatomic, retain) {{^complexType}}{{{ datatype }}}{{/complexType}}{{#complexType}}{{#isListContainer}}NSSet<{{{complexType}}}ManagedObject*>*{{/isListContainer}}{{^isListContainer}}{{{complexType}}}ManagedObject*{{/isListContainer}}{{/complexType}} {{name}};
{{/vars}}

@end

@interface {{classname}}ManagedObject (GeneratedAccessors)

{{#vars}}{{#isListContainer}}{{#complexType}}- (void)add{{vendorExtensions.x-uppercaseName}}Object:({{complexType}}ManagedObject *)value;
- (void)remove{{vendorExtensions.x-uppercaseName}}Object:({{complexType}}ManagedObject *)value;
- (void)add{{vendorExtensions.x-uppercaseName}}:(NSSet<{{{complexType}}}ManagedObject*> *)values;
- (void)remove{{vendorExtensions.x-uppercaseName}}:(NSSet<{{{complexType}}}ManagedObject*> *)values;
{{/complexType}}{{/isListContainer}}{{/vars}}
@end
{{/model}}
{{/models}}

NS_ASSUME_NONNULL_END
Loading