forked from loafle/openapi-generator-original
add nullable support to swift4
This commit is contained in:
committed by
Daiki Matsudate
parent
213efd8e84
commit
0c8481b8c7
@@ -41,7 +41,6 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
public static final String PROJECT_NAME = "projectName";
|
||||
public static final String RESPONSE_AS = "responseAs";
|
||||
public static final String UNWRAP_REQUIRED = "unwrapRequired";
|
||||
public static final String OBJC_COMPATIBLE = "objcCompatible";
|
||||
public static final String POD_SOURCE = "podSource";
|
||||
public static final String POD_AUTHORS = "podAuthors";
|
||||
@@ -209,10 +208,6 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
"Optionally use libraries to manage response. Currently "
|
||||
+ StringUtils.join(RESPONSE_LIBRARIES, ", ")
|
||||
+ " are available."));
|
||||
cliOptions.add(new CliOption(UNWRAP_REQUIRED,
|
||||
"Treat 'required' properties in response as non-optional "
|
||||
+ "(which would crash the app if api returns null as opposed "
|
||||
+ "to required option specified in json schema"));
|
||||
cliOptions.add(new CliOption(OBJC_COMPATIBLE,
|
||||
"Add additional properties and methods for Objective-C "
|
||||
+ "compatibility (default: false)"));
|
||||
@@ -330,13 +325,6 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
sourceFolder = projectName + File.separator + sourceFolder;
|
||||
|
||||
// Setup unwrapRequired option, which makes all the
|
||||
// properties with "required" non-optional
|
||||
if (additionalProperties.containsKey(UNWRAP_REQUIRED)) {
|
||||
setUnwrapRequired(convertPropertyToBooleanAndWriteBack(UNWRAP_REQUIRED));
|
||||
}
|
||||
additionalProperties.put(UNWRAP_REQUIRED, unwrapRequired);
|
||||
|
||||
// Setup objcCompatible option, which adds additional properties
|
||||
// and methods for Objective-C compatibility
|
||||
if (additionalProperties.containsKey(OBJC_COMPATIBLE)) {
|
||||
@@ -712,10 +700,6 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
public void setUnwrapRequired(boolean unwrapRequired) {
|
||||
this.unwrapRequired = unwrapRequired;
|
||||
}
|
||||
|
||||
public void setObjcCompatible(boolean objcCompatible) {
|
||||
this.objcCompatible = objcCompatible;
|
||||
}
|
||||
@@ -875,10 +859,9 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
|
||||
//
|
||||
// We can drop the check for unwrapRequired in (unwrapRequired && !property.required)
|
||||
// due to short-circuit evaluation of the || operator.
|
||||
boolean isSwiftOptional = !unwrapRequired || !property.required;
|
||||
boolean isSwiftScalarType = property.isInteger || property.isLong || property.isFloat
|
||||
|| property.isDouble || property.isBoolean;
|
||||
if (isSwiftOptional && isSwiftScalarType) {
|
||||
if ((!property.required || property.isNullable) && isSwiftScalarType) {
|
||||
// Optional scalar types like Int?, Int64?, Float?, Double?, and Bool?
|
||||
// do not translate to Objective-C. So we want to flag those
|
||||
// properties in case we want to put special code in the templates
|
||||
|
||||
@@ -13,12 +13,16 @@ public struct {{classname}}: Codable {
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#description}}/** {{description}} */
|
||||
{{/description}}public var {{name}}: {{{datatype}}}{{#unwrapRequired}}?{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#objcCompatible}}{{#vendorExtensions.x-swift-optional-scalar}}
|
||||
{{/description}}public var {{name}}: {{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
{{#objcCompatible}}
|
||||
{{#vendorExtensions.x-swift-optional-scalar}}
|
||||
public var {{name}}Num: NSNumber? {
|
||||
get {
|
||||
return {{name}}.map({ return NSNumber(value: $0) })
|
||||
}
|
||||
}{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
|
||||
}
|
||||
{{/vendorExtensions.x-swift-optional-scalar}}
|
||||
{{/objcCompatible}}
|
||||
{{/isEnum}}
|
||||
{{/allVars}}
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ public class Swift4OptionsProvider implements OptionsProvider {
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
.put(Swift4Codegen.PROJECT_NAME, PROJECT_NAME_VALUE)
|
||||
.put(Swift4Codegen.RESPONSE_AS, RESPONSE_AS_VALUE)
|
||||
.put(Swift4Codegen.UNWRAP_REQUIRED, UNWRAP_REQUIRED_VALUE)
|
||||
.put(Swift4Codegen.OBJC_COMPATIBLE, OBJC_COMPATIBLE_VALUE)
|
||||
.put(Swift4Codegen.LENIENT_TYPE_CAST, LENIENT_TYPE_CAST_VALUE)
|
||||
.put(Swift4Codegen.POD_SOURCE, POD_SOURCE_VALUE)
|
||||
|
||||
@@ -48,8 +48,6 @@ public class Swift4OptionsTest extends AbstractOptionsTest {
|
||||
times = 1;
|
||||
clientCodegen.setResponseAs(Swift4OptionsProvider.RESPONSE_AS_VALUE.split(","));
|
||||
times = 1;
|
||||
clientCodegen.setUnwrapRequired(Boolean.valueOf(Swift4OptionsProvider.UNWRAP_REQUIRED_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setObjcCompatible(Boolean.valueOf(Swift4OptionsProvider.OBJC_COMPATIBLE_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setLenientTypeCast(Boolean.valueOf(Swift4OptionsProvider.LENIENT_TYPE_CAST_VALUE));
|
||||
|
||||
@@ -9,7 +9,7 @@ import Foundation
|
||||
|
||||
public struct Animal: Codable {
|
||||
|
||||
public var className: String?
|
||||
public var className: String
|
||||
public var color: String? = "red"
|
||||
|
||||
public init(className: String?, color: String?) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import Foundation
|
||||
|
||||
public struct Cat: Codable {
|
||||
|
||||
public var className: String?
|
||||
public var className: String
|
||||
public var color: String? = "red"
|
||||
public var declawed: Bool?
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import Foundation
|
||||
public struct Category: Codable {
|
||||
|
||||
public var id: Int64?
|
||||
public var name: String? = "default-name"
|
||||
public var name: String = "default-name"
|
||||
|
||||
public init(id: Int64?, name: String?) {
|
||||
self.id = id
|
||||
|
||||
@@ -9,7 +9,7 @@ import Foundation
|
||||
|
||||
public struct Dog: Codable {
|
||||
|
||||
public var className: String?
|
||||
public var className: String
|
||||
public var color: String? = "red"
|
||||
public var breed: String?
|
||||
|
||||
|
||||
@@ -12,16 +12,16 @@ public struct FormatTest: Codable {
|
||||
public var integer: Int?
|
||||
public var int32: Int?
|
||||
public var int64: Int64?
|
||||
public var number: Double?
|
||||
public var number: Double
|
||||
public var float: Float?
|
||||
public var double: Double?
|
||||
public var string: String?
|
||||
public var byte: Data?
|
||||
public var byte: Data
|
||||
public var binary: URL?
|
||||
public var date: Date?
|
||||
public var date: Date
|
||||
public var dateTime: Date?
|
||||
public var uuid: UUID?
|
||||
public var password: String?
|
||||
public var password: String
|
||||
|
||||
public init(integer: Int?, int32: Int?, int64: Int64?, number: Double?, float: Float?, double: Double?, string: String?, byte: Data?, binary: URL?, date: Date?, dateTime: Date?, uuid: UUID?, password: String?) {
|
||||
self.integer = integer
|
||||
|
||||
@@ -11,7 +11,7 @@ import Foundation
|
||||
|
||||
public struct Name: Codable {
|
||||
|
||||
public var name: Int?
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
@@ -16,8 +16,8 @@ public struct Pet: Codable {
|
||||
}
|
||||
public var id: Int64?
|
||||
public var category: Category?
|
||||
public var name: String?
|
||||
public var photoUrls: [String]?
|
||||
public var name: String
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
|
||||
@@ -9,11 +9,11 @@ import Foundation
|
||||
|
||||
public struct TypeHolderDefault: Codable {
|
||||
|
||||
public var stringItem: String? = "what"
|
||||
public var numberItem: Double?
|
||||
public var integerItem: Int?
|
||||
public var boolItem: Bool? = true
|
||||
public var arrayItem: [Int]?
|
||||
public var stringItem: String = "what"
|
||||
public var numberItem: Double
|
||||
public var integerItem: Int
|
||||
public var boolItem: Bool = true
|
||||
public var arrayItem: [Int]
|
||||
|
||||
public init(stringItem: String?, numberItem: Double?, integerItem: Int?, boolItem: Bool?, arrayItem: [Int]?) {
|
||||
self.stringItem = stringItem
|
||||
|
||||
@@ -9,11 +9,11 @@ import Foundation
|
||||
|
||||
public struct TypeHolderExample: Codable {
|
||||
|
||||
public var stringItem: String?
|
||||
public var numberItem: Double?
|
||||
public var integerItem: Int?
|
||||
public var boolItem: Bool?
|
||||
public var arrayItem: [Int]?
|
||||
public var stringItem: String
|
||||
public var numberItem: Double
|
||||
public var integerItem: Int
|
||||
public var boolItem: Bool
|
||||
public var arrayItem: [Int]
|
||||
|
||||
public init(stringItem: String?, numberItem: Double?, integerItem: Int?, boolItem: Bool?, arrayItem: [Int]?) {
|
||||
self.stringItem = stringItem
|
||||
|
||||
Reference in New Issue
Block a user