Skip to content

Commit

Permalink
Swift5 models improvements (#9205)
Browse files Browse the repository at this point in the history
* [swift5] Add useClasses to use `final class` instead of `struct`

* [swift5] Always include CodingKeys enum

* [swift5] Implement model equals and hash functions

* [swift5] Encode `null` values

* [swift5] Test `useClasses` in urlsessionLibrary

* [swift5] Add a required nullable prop test case to 2_0/swift/petstore*.yaml

* [swift5] Update samples and docs
  • Loading branch information
aymanbagabas authored Apr 24, 2021
1 parent 22950fa commit 0f5e7d1
Show file tree
Hide file tree
Showing 414 changed files with 6,684 additions and 342 deletions.
1 change: 1 addition & 0 deletions bin/configs/swift5-urlsessionLibrary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ additionalProperties:
projectName: PetstoreClient
podHomepage: https://github.com/openapitools/openapi-generator
useSPMFileStructure: true
useClasses: true
2 changes: 1 addition & 1 deletion docs/generators/swift5.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
|lenientTypeCast|Accept and cast values for simple types (string-&gt;bool, string-&gt;int, int-&gt;string)| |false|
|library|Library template (sub-template) to use|<dl><dt>**urlsession**</dt><dd>[DEFAULT] HTTP client: URLSession</dd><dt>**alamofire**</dt><dd>HTTP client: Alamofire</dd><dt>**vapor**</dt><dd>HTTP client: Vapor</dd></dl>|urlsession|
|library|Library template (sub-template) to use|<dl><dt>**urlsession**</dt><dd>[DEFAULT] HTTP client: URLSession</dd><dt>**alamofire**</dt><dd>HTTP client: Alamofire</dd></dl>|urlsession|
|nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.(default: false)| |null|
|objcCompatible|Add additional properties and methods for Objective-C compatibility (default: false)| |null|
|podAuthors|Authors used for Podspec| |null|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct {{classname}}: Codable, Hashable {
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{classname}}: Codable, Hashable {
{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable {
{{/objcCompatible}}

Expand Down Expand Up @@ -36,6 +36,11 @@
{{/allVars}}
}
{{/hasVars}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum CodingKeys: {{#hasVars}}String, {{/hasVars}}CodingKey, CaseIterable {
{{#allVars}}
case {{{name}}}{{#vendorExtensions.x-codegen-escaped-property-name}} = "{{{baseName}}}"{{/vendorExtensions.x-codegen-escaped-property-name}}
{{/allVars}}
}
{{#additionalPropertiesType}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var additionalProperties: [String: {{{additionalPropertiesType}}}] = [:]

Expand All @@ -51,19 +56,22 @@
additionalProperties[key] = newValue
}
}
{{/additionalPropertiesType}}

// Encodable protocol methods

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: String.self)
var container = encoder.container(keyedBy: CodingKeys.self)
{{#allVars}}
try container.encode{{#required}}{{#isNullable}}IfPresent{{/isNullable}}{{/required}}{{^required}}IfPresent{{/required}}({{{name}}}, forKey: "{{{baseName}}}")
try container.encode{{^required}}IfPresent{{/required}}({{{name}}}, forKey: .{{{name}}})
{{/allVars}}
try container.encodeMap(additionalProperties)
{{#additionalPropertiesType}}
var additionalPropertiesContainer = encoder.container(keyedBy: String.self)
try additionalPropertiesContainer.encodeMap(additionalProperties)
{{/additionalPropertiesType}}
}

{{#additionalPropertiesType}}
// Decodable protocol methods

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}}{{#objcCompatible}} required{{/objcCompatible}} init(from decoder: Decoder) throws {
Expand All @@ -79,11 +87,20 @@
additionalProperties = try container.decodeMap({{{additionalPropertiesType}}}.self, excludedKeys: nonAdditionalPropertyKeys)
}
{{/additionalPropertiesType}}
{{^additionalPropertiesType}}{{#vendorExtensions.x-codegen-has-escaped-property-names}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum CodingKeys: String, CodingKey, CaseIterable {

{{^objcCompatible}}{{#useClasses}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func == (lhs: {{classname}}, rhs: {{classname}}) -> Bool {
{{#allVars}}
case {{{name}}}{{#vendorExtensions.x-codegen-escaped-property-name}} = "{{{baseName}}}"{{/vendorExtensions.x-codegen-escaped-property-name}}
lhs.{{{name}}} == rhs.{{{name}}}{{^-last}} &&{{/-last}}
{{/allVars}}
{{#additionalPropertiesType}}{{#hasVars}}&& {{/hasVars}}lhs.additionalProperties == rhs.additionalProperties{{/additionalPropertiesType}}
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func hash(into hasher: inout Hasher) {
{{#allVars}}
hasher.combine({{{name}}}{{^required}}?{{/required}}.hashValue)
{{/allVars}}
{{#additionalPropertiesType}}hasher.combine(additionalProperties.hashValue){{/additionalPropertiesType}}
}
{{/vendorExtensions.x-codegen-has-escaped-property-names}}{{/additionalPropertiesType}}
{{/useClasses}}{{/objcCompatible}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ definitions:
name:
type: string
default: default-name
x-nullable: true
xml:
name: Category
User:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ public struct AdditionalPropertiesClass: Codable, Hashable {
self.mapString = mapString
self.mapMapString = mapMapString
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case mapString = "map_string"
case mapMapString = "map_map_string"
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(mapString, forKey: .mapString)
try container.encodeIfPresent(mapMapString, forKey: .mapMapString)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,19 @@ public struct Animal: Codable, Hashable {
self.className = className
self.color = color
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case className
case color
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(className, forKey: .className)
try container.encodeIfPresent(color, forKey: .color)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,21 @@ public struct ApiResponse: Codable, Hashable {
self.type = type
self.message = message
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case code
case type
case message
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(code, forKey: .code)
try container.encodeIfPresent(type, forKey: .type)
try container.encodeIfPresent(message, forKey: .message)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ public struct ArrayOfArrayOfNumberOnly: Codable, Hashable {
public init(arrayArrayNumber: [[Double]]? = nil) {
self.arrayArrayNumber = arrayArrayNumber
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case arrayArrayNumber = "ArrayArrayNumber"
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(arrayArrayNumber, forKey: .arrayArrayNumber)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ public struct ArrayOfNumberOnly: Codable, Hashable {
public init(arrayNumber: [Double]? = nil) {
self.arrayNumber = arrayNumber
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case arrayNumber = "ArrayNumber"
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(arrayNumber, forKey: .arrayNumber)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ public struct ArrayTest: Codable, Hashable {
self.arrayArrayOfInteger = arrayArrayOfInteger
self.arrayArrayOfModel = arrayArrayOfModel
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case arrayOfString = "array_of_string"
case arrayArrayOfInteger = "array_array_of_integer"
case arrayArrayOfModel = "array_array_of_model"
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(arrayOfString, forKey: .arrayOfString)
try container.encodeIfPresent(arrayArrayOfInteger, forKey: .arrayArrayOfInteger)
try container.encodeIfPresent(arrayArrayOfModel, forKey: .arrayArrayOfModel)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public struct Capitalization: Codable, Hashable {
self.sCAETHFlowPoints = sCAETHFlowPoints
self.ATT_NAME = ATT_NAME
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case smallCamel
case capitalCamel = "CapitalCamel"
Expand All @@ -35,4 +34,18 @@ public struct Capitalization: Codable, Hashable {
case ATT_NAME
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(smallCamel, forKey: .smallCamel)
try container.encodeIfPresent(capitalCamel, forKey: .capitalCamel)
try container.encodeIfPresent(smallSnake, forKey: .smallSnake)
try container.encodeIfPresent(capitalSnake, forKey: .capitalSnake)
try container.encodeIfPresent(sCAETHFlowPoints, forKey: .sCAETHFlowPoints)
try container.encodeIfPresent(ATT_NAME, forKey: .ATT_NAME)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,21 @@ public struct Cat: Codable, Hashable {
self.color = color
self.declawed = declawed
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case className
case color
case declawed
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(className, forKey: .className)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(declawed, forKey: .declawed)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ public struct CatAllOf: Codable, Hashable {
public init(declawed: Bool? = nil) {
self.declawed = declawed
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case declawed
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(declawed, forKey: .declawed)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ import Foundation
public struct Category: Codable, Hashable {

public var id: Int64?
public var name: String = "default-name"
public var name: String? = "default-name"

public init(id: Int64? = nil, name: String = "default-name") {
public init(id: Int64? = nil, name: String? = "default-name") {
self.id = id
self.name = name
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case id
case name
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(id, forKey: .id)
try container.encode(name, forKey: .name)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ public struct ClassModel: Codable, Hashable {
public init(_class: String? = nil) {
self._class = _class
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case _class
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(_class, forKey: ._class)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ public struct Client: Codable, Hashable {
public init(client: String? = nil) {
self.client = client
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case client
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(client, forKey: .client)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,21 @@ public struct Dog: Codable, Hashable {
self.color = color
self.breed = breed
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case className
case color
case breed
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(className, forKey: .className)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(breed, forKey: .breed)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ public struct DogAllOf: Codable, Hashable {
public init(breed: String? = nil) {
self.breed = breed
}
public enum CodingKeys: String, CodingKey, CaseIterable {
case breed
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(breed, forKey: .breed)
}



}
Loading

0 comments on commit 0f5e7d1

Please sign in to comment.