Skip to content

Commit

Permalink
2.16.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Jun 8, 2023
1 parent f46945b commit 9c5cf01
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/SwiftOpenAPI.git", from: "2.16.6")
.package(url: "https://github.com/dankinsoid/SwiftOpenAPI.git", from: "2.16.7")
],
targets: [
.target(name: "SomeProject", dependencies: ["SwiftOpenAPI"])
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftOpenAPI/Encoders/HeadersEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct HeadersEncoder {
}

private func parse(
value: CodableContainerValue,
value: TypeInfo,
type: Any.Type,
into schemas: inout [String: ReferenceOr<SchemaObject>]
) throws -> [String: HeaderObject] {
Expand All @@ -39,7 +39,7 @@ struct HeadersEncoder {
throw InvalidType()

default:
switch value {
switch value.container {
case .single, .unkeyed, .recursive:
throw InvalidType()

Expand All @@ -50,7 +50,7 @@ struct HeadersEncoder {
try HeaderObject(
required: !$0.isOptional,
schema: SchemeEncoder(dateFormat: dateFormat, keyEncodingStrategy: keyEncodingStrategy)
.parse(value: $0.container, type: $0.type, into: &schemas),
.parse(value: $0, type: $0.type, into: &schemas),
example: $0.container.anyValue
)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftOpenAPI/Encoders/ParametersEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct ParametersEncoder {
}

private func parse(
value: CodableContainerValue,
value: TypeInfo,
type: Any.Type,
into schemas: inout [String: ReferenceOr<SchemaObject>]
) throws -> [ParameterObject] {
Expand All @@ -40,7 +40,7 @@ struct ParametersEncoder {
throw InvalidType()

default:
switch value {
switch value.container {
case .single, .unkeyed, .recursive:
throw InvalidType()

Expand All @@ -51,7 +51,7 @@ struct ParametersEncoder {
in: location,
required: !$0.value.isOptional,
schema: SchemeEncoder(dateFormat: dateFormat, keyEncodingStrategy: keyEncodingStrategy)
.parse(value: $0.value.container, type: $0.value.type, into: &schemas),
.parse(value: $0.value, type: $0.value.type, into: &schemas),
example: $0.value.container.anyValue
)
}
Expand Down
23 changes: 15 additions & 8 deletions Sources/SwiftOpenAPI/Encoders/SchemeEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ struct SchemeEncoder {
}

func parse(
value: @autoclosure () throws -> CodableContainerValue,
value: @autoclosure () throws -> TypeInfo,
type: Any.Type,
into schemas: inout [String: ReferenceOr<SchemaObject>]
) throws -> ReferenceOr<SchemaObject> {
let name = String.typeName(type)
var result: ReferenceOr<SchemaObject>
let typeInfo = try value()

switch type {
case is Date.Type:
Expand All @@ -47,15 +48,17 @@ struct SchemeEncoder {
result = .value(openAPI.openAPISchema)

default:
switch try value() {
switch typeInfo.container {
case let .single(codableValues):
let (dataType, format) = try parse(value: codableValues)
let schema: SchemaObject
if let iterable = type as? any CaseIterable.Type {
let allCases = iterable.allCases as any Collection
result = .value(.enum(of: dataType, cases: allCases.map { "\($0)" }))
schema = .enum(of: dataType, cases: allCases.map { "\($0)" })
} else {
result = .value(.primitive(dataType, format: format))
schema = .primitive(dataType, format: format)
}
result = .value(schema)

case let .keyed(keyedInfo):
switch keyedInfo.isFixed {
Expand All @@ -64,7 +67,7 @@ struct SchemeEncoder {
properties: keyedInfo.fields.mapKeys {
keyEncodingStrategy.encode($0)
}.mapValues {
try parse(value: $0.container, type: $0.type, into: &schemas)
try parse(value: $0, type: $0.type, into: &schemas)
},
required: Set(keyedInfo.fields.filter { !$0.value.isOptional }.keys)
)
Expand All @@ -73,15 +76,15 @@ struct SchemeEncoder {
case false:
let schema = try SchemaObject.dictionary(
of: (keyedInfo.fields.first?.value).map {
try parse(value: $0.container, type: $0.type, into: &schemas)
try parse(value: $0, type: $0.type, into: &schemas)
} ?? .any
)
result = .value(schema)
}

case let .unkeyed(typeInfo):
case let .unkeyed(itemInfo):
let schema = try SchemaObject.array(
of: parse(value: typeInfo.container, type: typeInfo.type, into: &schemas)
of: parse(value: itemInfo, type: itemInfo.type, into: &schemas)
)
result = .value(schema)

Expand All @@ -95,9 +98,13 @@ struct SchemeEncoder {
}

if extractReferences, result.isReferenceable {
result.object?.nullable = nil
schemas[name] = result
return .ref(components: \.schemas, name)
} else {
if typeInfo.isOptional {
result.object?.nullable = true
}
return result
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ extension CodableContainerValue {
case let .float(value): return value.map { .double(Double($0)) }
case let .bool(value): return value.map { .bool($0) }
case let .string(value): return value.map { .string($0) }
case .null: return nil
case .null: return .null
}

case let .keyed(keyedInfo):
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftOpenAPI/Encoders/TypeRevision/TypeRevision.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ struct TypeRevision {
self.init { _, _ in nil }
}

func describeType(of value: Encodable) throws -> CodableContainerValue {
func describeType(of value: Encodable) throws -> TypeInfo {
let encoder = TypeRevisionEncoder(context: self)
try encoder.encode(value, type: type(of: value))
return encoder.result.container
return encoder.result
}

func describe(type: Decodable.Type) throws -> CodableContainerValue {
func describe(type: Decodable.Type) throws -> TypeInfo {
let decoder = TypeRevisionDecoder(context: self)
try decoder.decode(type)
return decoder.result.container
return decoder.result
}
}
64 changes: 63 additions & 1 deletion Sources/SwiftOpenAPI/Objects/AnyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum AnyValue: Codable, Equatable {
case double(Double)
case object([String: AnyValue])
case array([AnyValue])
case null

public init(from decoder: Decoder) throws {
do {
Expand All @@ -26,7 +27,18 @@ public enum AnyValue: Codable, Equatable {
do {
self = try .object([String: AnyValue](from: decoder))
} catch {
self = try .array([AnyValue](from: decoder))
do {
self = try .array([AnyValue](from: decoder))
} catch {
let container = try decoder.singleValueContainer()
if container.decodeNil() {
self = .null
} else {
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unknown type")
)
}
}
}
}
}
Expand All @@ -42,6 +54,9 @@ public enum AnyValue: Codable, Equatable {
case let .double(value): try value.encode(to: encoder)
case let .object(value): try value.encode(to: encoder)
case let .array(value): try value.encode(to: encoder)
case .null:
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}

Expand Down Expand Up @@ -85,6 +100,7 @@ public enum AnyValue: Codable, Equatable {
case .double: return .number
case .object: return .object
case .array: return .array
case .null: return .null
}
}
}
Expand Down Expand Up @@ -142,6 +158,52 @@ extension AnyValue: ExpressibleByFloatLiteral {
}
}

extension AnyValue: ExpressibleByNilLiteral {

public init(nilLiteral: ()) {
self = .null
}
}

extension AnyValue: LosslessStringConvertible {

public var description: String {
switch self {
case .string(let string):
return string.description
case .bool(let bool):
return bool.description
case .int(let int):
return int.description
case .double(let double):
return double.description
case .object(let dictionary):
return dictionary.description
case .array(let array):
return array.description
case .null:
return "nil"
}
}

public init(_ description: String) {
switch description {
case "nil":
self = .null
default:
if let int = Int(description) {
self = .int(int)
} else if let bool = Bool(description) {
self = .bool(bool)
} else if let double = Double(description) {
self = .double(double)
} else {
self = .string(description)
}
}
}
}

public extension AnyValue {

static func encode(_ value: Encodable, dateFormat: DateEncodingFormat = .default) throws -> AnyValue {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftOpenAPI/Objects/ComponentsObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public struct ComponentsObject: Codable, Equatable, SpecificationExtendable {
securitySchemes: [String: ReferenceOr<SecuritySchemeObject>]? = nil,
links: [String: ReferenceOr<LinkObject>]? = nil,
callbacks: [String: ReferenceOr<CallbackObject>]? = nil,
pathItems: [String: ReferenceOr<PathItemObject>]? = nil
// specificationExtensions: SpecificationExtensions = [:]
pathItems: [String: ReferenceOr<PathItemObject>]? = nil,
specificationExtensions: SpecificationExtensions = [:]
) {
self.schemas = schemas
self.responses = responses
Expand All @@ -58,6 +58,6 @@ public struct ComponentsObject: Codable, Equatable, SpecificationExtendable {
self.links = links
self.callbacks = callbacks
self.pathItems = pathItems
// self.specificationExtensions = specificationExtensions
self.specificationExtensions = specificationExtensions
}
}
3 changes: 2 additions & 1 deletion Sources/SwiftOpenAPI/Objects/DataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ public enum DataType: String, Codable {
case boolean
case array
case object
case null

public var asPrimitive: PrimitiveDataType? {
switch self {
case .string: return .string
case .number: return .number
case .integer: return .integer
case .boolean: return .boolean
case .array, .object: return nil
case .array, .object, .null: return nil
}
}
}
Expand Down

0 comments on commit 9c5cf01

Please sign in to comment.