forked from loafle/openapi-generator-original
[swift5] Add property x-null-encodable extension for full control over encoding value/nil or nothing (#11141)
* feat(ISSUE-11033): add null encodable type and mustache changes * feat(ISSUE-11033): regen all swift5 samples * feat(ISSUE-11033): add swift5 examples of null encodable * feat(ISSUE-11033): fix hashable and compliation issues * fix(ISSUE-11033): fix tests by using new enum encodeValue * fix(ISSUE-11033): fix tests by using new enum encodeValue * fix(ISSUE-11033): revert status back in pet api test * fix(ISSUE-11033): fix issue with objc compat generator * feat(ISSUE-11033): restore default values to null encodables * chore(ISSUE-11033): rename default value for null encodable * chore(ISSUE-11033): add test cases with different nullable defaults
This commit is contained in:
+10
@@ -1110,6 +1110,16 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
prop.vendorExtensions.put("x-codegen-escaped-property-name", true);
|
||||
modelHasPropertyWithEscapedName = true;
|
||||
}
|
||||
|
||||
if (prop.vendorExtensions.containsKey("x-null-encodable")) {
|
||||
if (prop.vendorExtensions.get("x-null-encodable").toString().equals("true")) {
|
||||
if (prop.defaultValue == null || prop.defaultValue.equals("null")) {
|
||||
prop.vendorExtensions.put("x-null-encodable-default-value", ".encodeNull");
|
||||
} else {
|
||||
prop.vendorExtensions.put("x-null-encodable-default-value", ".encodeValue(" + prop.defaultValue + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (modelHasPropertyWithEscapedName) {
|
||||
cm.vendorExtensions.put("x-codegen-has-escaped-property-names", true);
|
||||
|
||||
@@ -37,6 +37,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
@@ -11,17 +11,26 @@
|
||||
{{#isEnum}}
|
||||
{{#description}}/** {{{.}}} */
|
||||
{{/description}}{{#deprecated}}@available(*, deprecated, message: "This property is deprecated.")
|
||||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
|
||||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatypeWithEnum}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}}
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#description}}/** {{{.}}} */
|
||||
{{/description}}{{#deprecated}}@available(*, deprecated, message: "This property is deprecated.")
|
||||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
|
||||
{{/deprecated}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatype}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}}
|
||||
{{#objcCompatible}}
|
||||
{{#vendorExtensions.x-swift-optional-scalar}}
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{{name}}}Num: NSNumber? {
|
||||
get {
|
||||
{{^vendorExtensions.x-null-encodable}}
|
||||
return {{{name}}} as NSNumber?
|
||||
{{/vendorExtensions.x-null-encodable}}
|
||||
{{#vendorExtensions.x-null-encodable}}
|
||||
if case .encodeValue(let value) = {{name}} {
|
||||
return value as NSNumber?
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
{{/vendorExtensions.x-null-encodable}}
|
||||
}
|
||||
}
|
||||
{{/vendorExtensions.x-swift-optional-scalar}}
|
||||
@@ -30,7 +39,7 @@
|
||||
{{/allVars}}
|
||||
{{#hasVars}}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init({{#allVars}}{{{name}}}: {{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}}{{^required}} = nil{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allVars}}) {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init({{#allVars}}{{{name}}}: {{#vendorExtensions.x-null-encodable}}NullEncodable<{{{datatypeWithEnum}}}>{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}{{#defaultValue}} = {{#vendorExtensions.x-null-encodable}}{{{vendorExtensions.x-null-encodable-default-value}}}{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}{{/defaultValue}}{{^defaultValue}}{{^required}} = {{#vendorExtensions.x-null-encodable}}.encodeNull{{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}nil{{/vendorExtensions.x-null-encodable}}{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allVars}}) {
|
||||
{{#allVars}}
|
||||
self.{{{name}}} = {{{name}}}
|
||||
{{/allVars}}
|
||||
@@ -63,7 +72,15 @@
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
{{#allVars}}
|
||||
{{#vendorExtensions.x-null-encodable}}
|
||||
switch {{{name}}} {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode({{{name}}}, forKey: .{{{name}}})
|
||||
}
|
||||
{{/vendorExtensions.x-null-encodable}}
|
||||
{{^vendorExtensions.x-null-encodable}}
|
||||
try container.encode{{^required}}IfPresent{{/required}}({{{name}}}, forKey: .{{{name}}})
|
||||
{{/vendorExtensions.x-null-encodable}}
|
||||
{{/allVars}}
|
||||
{{#generateModelAdditionalProperties}}
|
||||
{{#additionalPropertiesType}}
|
||||
@@ -98,7 +115,7 @@
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func hash(into hasher: inout Hasher) {
|
||||
{{#allVars}}
|
||||
hasher.combine({{{name}}}{{^required}}?{{/required}}.hashValue)
|
||||
hasher.combine({{{name}}}{{^vendorExtensions.x-null-encodable}}{{^required}}?{{/required}}{{/vendorExtensions.x-null-encodable}}.hashValue)
|
||||
{{/allVars}}
|
||||
{{#generateModelAdditionalProperties}}{{#additionalPropertiesType}}hasher.combine(additionalProperties.hashValue){{/additionalPropertiesType}}{{/generateModelAdditionalProperties}}
|
||||
}{{/vendorExtensions.x-swift-hashable}}{{/useClasses}}{{/objcCompatible}}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
{{#vars}}**{{{name}}}** | {{#isPrimitiveType}}**{{{dataType}}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isContainer}}[**{{dataType}}**]({{complexType}}.md){{/isContainer}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{/isPrimitiveType}} | {{description}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{{.}}}]{{/defaultValue}}
|
||||
{{#vars}}**{{{name}}}** | {{#isPrimitiveType}}**{{{dataType}}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isContainer}}[**{{dataType}}**]({{complexType}}.md){{/isContainer}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{/isPrimitiveType}} | {{description}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{#vendorExtensions.x-null-encodable}}.encodeValue({{{.}}}){{/vendorExtensions.x-null-encodable}}{{^vendorExtensions.x-null-encodable}}{{{.}}}{{/vendorExtensions.x-null-encodable}}]{{/defaultValue}}
|
||||
{{/vars}}
|
||||
|
||||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||
|
||||
+4
@@ -1169,6 +1169,8 @@ definitions:
|
||||
status:
|
||||
type: string
|
||||
description: pet status in the store
|
||||
x-null-encodable: true
|
||||
default: null
|
||||
enum:
|
||||
- available
|
||||
- pending
|
||||
@@ -1212,6 +1214,8 @@ definitions:
|
||||
readOnly: true
|
||||
type: integer
|
||||
format: int32
|
||||
x-null-encodable: true
|
||||
default: 11033
|
||||
property:
|
||||
type: string
|
||||
123Number:
|
||||
|
||||
+30
@@ -37,6 +37,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ class PetAPITests: XCTestCase {
|
||||
let expectation = self.expectation(description: "testCreatePet")
|
||||
let category = Category(id: 1234, name: "eyeColor")
|
||||
let tags = [Tag(id: 1234, name: "New York"), Tag(id: 124321, name: "Jose")]
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .available)
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .encodeValue(.available))
|
||||
|
||||
PetAPI.addPet(body: newPet) { (_, error) in
|
||||
guard error == nil else {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ class PetAPITests: XCTestCase {
|
||||
let expectation = self.expectation(description: "testCreatePet")
|
||||
let category = Category(id: 1234, name: "eyeColor")
|
||||
let tags = [Tag(id: 1234, name: "New York"), Tag(id: 124321, name: "Jose")]
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .available)
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .encodeValue(.available))
|
||||
|
||||
PetAPI.addPet(body: newPet).sink(receiveCompletion: { (completion) in
|
||||
switch completion {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -24,9 +24,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -53,7 +53,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
internal struct Name: Codable, Hashable {
|
||||
|
||||
internal var name: Int
|
||||
internal var snakeCase: Int?
|
||||
internal var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
internal var property: String?
|
||||
internal var _123number: Int?
|
||||
|
||||
internal init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
internal init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ internal struct Name: Codable, Hashable {
|
||||
internal func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ internal struct Pet: Codable, Hashable {
|
||||
internal var photoUrls: [String]
|
||||
internal var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
internal var status: Status?
|
||||
internal var status: NullEncodable<Status>
|
||||
|
||||
internal init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
internal init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ internal struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+11
-4
@@ -14,10 +14,14 @@ import AnyCodable
|
||||
@objc public class Name: NSObject, Codable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var snakeCaseNum: NSNumber? {
|
||||
get {
|
||||
return snakeCase as NSNumber?
|
||||
if case .encodeValue(let value) = snakeCase {
|
||||
return value as NSNumber?
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
public var property: String?
|
||||
@@ -28,7 +32,7 @@ import AnyCodable
|
||||
}
|
||||
}
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -47,7 +51,10 @@ import AnyCodable
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -28,9 +28,9 @@ import AnyCodable
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(_id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(_id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self._id = _id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -57,7 +57,10 @@ import AnyCodable
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ class PetAPITests: XCTestCase {
|
||||
let expectation = self.expectation(description: "testCreatePet")
|
||||
let category = Category(id: 1234, name: "eyeColor")
|
||||
let tags = [Tag(id: 1234, name: "New York"), Tag(id: 124321, name: "Jose")]
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .available)
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .encodeValue(.available))
|
||||
|
||||
PetAPI.addPet(body: newPet).done {
|
||||
expectation.fulfill()
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public private(set) var name: Int
|
||||
public private(set) var snakeCase: Int?
|
||||
public private(set) var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public private(set) var property: String?
|
||||
public private(set) var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public private(set) var photoUrls: [String]
|
||||
public private(set) var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public private(set) var status: Status?
|
||||
public private(set) var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ class PetAPITests: XCTestCase {
|
||||
let expectation = self.expectation(description: "testCreatePet")
|
||||
let category = Category(id: 1234, name: "eyeColor")
|
||||
let tags = [Tag(id: 1234, name: "New York"), Tag(id: 124321, name: "Jose")]
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .available)
|
||||
let newPet = Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .encodeValue(.available))
|
||||
|
||||
PetAPI.addPet(body: newPet).subscribe(onNext: {
|
||||
expectation.fulfill()
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+7
-4
@@ -19,11 +19,11 @@ extension PetstoreClientAPI {
|
||||
public final class Name: Codable, Hashable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -42,7 +42,10 @@ public final class Name: Codable, Hashable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
@@ -57,7 +60,7 @@ public final class Name: Codable, Hashable {
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(name.hashValue)
|
||||
hasher.combine(snakeCase?.hashValue)
|
||||
hasher.combine(snakeCase.hashValue)
|
||||
hasher.combine(property?.hashValue)
|
||||
hasher.combine(_123number?.hashValue)
|
||||
|
||||
|
||||
+7
-4
@@ -28,9 +28,9 @@ public final class Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -57,7 +57,10 @@ public final class Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
|
||||
public static func == (lhs: Pet, rhs: Pet) -> Bool {
|
||||
@@ -76,7 +79,7 @@ public final class Pet: Codable, Hashable {
|
||||
hasher.combine(name.hashValue)
|
||||
hasher.combine(photoUrls.hashValue)
|
||||
hasher.combine(tags?.hashValue)
|
||||
hasher.combine(status?.hashValue)
|
||||
hasher.combine(status.hashValue)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ class PetAPITests: XCTestCase {
|
||||
let expectation = self.expectation(description: "testCreatePet")
|
||||
let category = PetstoreClientAPI.Category(id: 1234, name: "eyeColor")
|
||||
let tags = [PetstoreClientAPI.Tag(id: 1234, name: "New York"), PetstoreClientAPI.Tag(id: 124321, name: "Jose")]
|
||||
let newPet = PetstoreClientAPI.Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .available)
|
||||
let newPet = PetstoreClientAPI.Pet(id: 1000, category: category, name: "Fluffy", photoUrls: ["https://petstore.com/sample/photo1.jpg", "https://petstore.com/sample/photo2.jpg"], tags: tags, status: .encodeValue(.available))
|
||||
|
||||
PetstoreClientAPI.PetAPI.addPet(body: newPet) { (_, error) in
|
||||
guard error == nil else {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
+30
@@ -36,6 +36,36 @@ extension CaseIterableDefaultsLast {
|
||||
}
|
||||
}
|
||||
|
||||
/// A flexible type that can be encoded (`.encodeNull` or `.encodeValue`)
|
||||
/// or not encoded (`.encodeNothing`). Intended for request payloads.
|
||||
public enum NullEncodable<Wrapped: Hashable>: Hashable {
|
||||
case encodeNothing
|
||||
case encodeNull
|
||||
case encodeValue(Wrapped)
|
||||
}
|
||||
|
||||
extension NullEncodable: Codable where Wrapped: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(Wrapped.self) {
|
||||
self = .encodeValue(value)
|
||||
} else if container.decodeNil() {
|
||||
self = .encodeNull
|
||||
} else {
|
||||
self = .encodeNothing
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .encodeNothing: return
|
||||
case .encodeNull: try container.encodeNil()
|
||||
case .encodeValue(let wrapped): try container.encode(wrapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
+6
-3
@@ -14,11 +14,11 @@ import AnyCodable
|
||||
public struct Name: Codable {
|
||||
|
||||
public var name: Int
|
||||
public var snakeCase: Int?
|
||||
public var snakeCase: NullEncodable<Int> = .encodeValue(11033)
|
||||
public var property: String?
|
||||
public var _123number: Int?
|
||||
|
||||
public init(name: Int, snakeCase: Int? = nil, property: String? = nil, _123number: Int? = nil) {
|
||||
public init(name: Int, snakeCase: NullEncodable<Int> = .encodeValue(11033), property: String? = nil, _123number: Int? = nil) {
|
||||
self.name = name
|
||||
self.snakeCase = snakeCase
|
||||
self.property = property
|
||||
@@ -37,7 +37,10 @@ public struct Name: Codable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(snakeCase, forKey: .snakeCase)
|
||||
switch snakeCase {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(snakeCase, forKey: .snakeCase)
|
||||
}
|
||||
try container.encodeIfPresent(property, forKey: .property)
|
||||
try container.encodeIfPresent(_123number, forKey: ._123number)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,9 +23,9 @@ public struct Pet: Codable, Hashable {
|
||||
public var photoUrls: [String]
|
||||
public var tags: [Tag]?
|
||||
/** pet status in the store */
|
||||
public var status: Status?
|
||||
public var status: NullEncodable<Status>
|
||||
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: Status? = nil) {
|
||||
public init(id: Int64? = nil, category: Category? = nil, name: String, photoUrls: [String], tags: [Tag]? = nil, status: NullEncodable<Status> = .encodeNull) {
|
||||
self.id = id
|
||||
self.category = category
|
||||
self.name = name
|
||||
@@ -52,7 +52,10 @@ public struct Pet: Codable, Hashable {
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(photoUrls, forKey: .photoUrls)
|
||||
try container.encodeIfPresent(tags, forKey: .tags)
|
||||
try container.encodeIfPresent(status, forKey: .status)
|
||||
switch status {
|
||||
case .encodeNothing: break
|
||||
case .encodeNull, .encodeValue: try container.encode(status, forKey: .status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**name** | **Int** | |
|
||||
**snakeCase** | **Int** | | [optional] [readonly]
|
||||
**snakeCase** | **Int** | | [optional] [readonly] [default to .encodeValue(11033)]
|
||||
**property** | **String** | | [optional]
|
||||
**_123number** | **Int** | | [optional] [readonly]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user