Skip to content

Commit 29ec2eb

Browse files
committed
Added additional required properties to OpenAPI parameter type.
1 parent c283fee commit 29ec2eb

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift

+59
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,65 @@ extension OpenAPIPathItem.PathProperties.Operation: Encodable {
321321
}
322322
}
323323

324+
extension OpenAPIPathItem.PathProperties.Parameter: Encodable {
325+
private enum CodingKeys: String, CodingKey {
326+
case name
327+
case parameterLocation = "in"
328+
case description
329+
case required
330+
case deprecated
331+
332+
// the following are alternatives
333+
case content
334+
case schema
335+
}
336+
337+
public func encode(to encoder: Encoder) throws {
338+
var container = encoder.container(keyedBy: CodingKeys.self)
339+
340+
try container.encode(name, forKey: .name)
341+
342+
let required: Bool?
343+
let location: String
344+
switch parameterLocation {
345+
case .query(required: let req):
346+
required = req
347+
location = "query"
348+
case .header(required: let req):
349+
required = req
350+
location = "header"
351+
case .path:
352+
required = true
353+
location = "path"
354+
case .cookie(required: let req):
355+
required = req
356+
location = "cookie"
357+
}
358+
try container.encode(location, forKey: .parameterLocation)
359+
360+
try container.encode(required, forKey: .required)
361+
362+
switch schemaOrContent {
363+
case .a(let schema):
364+
try container.encode(schema, forKey: .schema)
365+
case .b(let contentMap):
366+
// Hack to work around Dictionary encoding
367+
// itself as an array in this case:
368+
let stringKeyedDict = Dictionary(
369+
contentMap.map { ($0.key.rawValue, $0.value) },
370+
uniquingKeysWith: { $1 }
371+
)
372+
try container.encode(stringKeyedDict, forKey: .content)
373+
}
374+
375+
if description != nil {
376+
try container.encode(description, forKey: .description)
377+
}
378+
379+
try container.encode(deprecated, forKey: .deprecated)
380+
}
381+
}
382+
324383
extension OpenAPIPathItem.PathProperties: Encodable {
325384
private enum CodingKeys: String, CodingKey {
326385
case summary

Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -720,37 +720,37 @@ public enum OpenAPIPathItem: Equatable {
720720

721721
public typealias ParameterArray = [Either<Parameter, JSONReference<OpenAPIComponents, Parameter>>]
722722

723-
public struct Parameter: Equatable, Encodable {
724-
private enum CodingKeys: String, CodingKey {
725-
case name
726-
// case parameterLocation = "in"
727-
case description
728-
case deprecated
729-
}
730-
723+
public struct Parameter: Equatable {
731724
public let name: String
732-
// public let parameterLocation: Location
725+
public let parameterLocation: Location
733726
public let description: String?
734727
public let deprecated: Bool // default is false
728+
public let schemaOrContent: Either<SchemaProperty, Operation.ContentMap>
735729
// TODO: serialization rules
736730
/*
737731
Serialization Rules
738732
*/
739733

734+
public typealias SchemaProperty = Either<JSONNode, JSONReference<OpenAPIComponents, JSONNode>>
735+
740736
public init(name: String,
737+
parameterLocation: Location,
738+
schemaOrContent: Either<SchemaProperty, Operation.ContentMap>,
741739
description: String? = nil,
742740
deprecated: Bool = false) {
743741
self.name = name
742+
self.parameterLocation = parameterLocation
743+
self.schemaOrContent = schemaOrContent
744744
self.description = description
745745
self.deprecated = deprecated
746746
}
747747

748-
// public enum Location: Encodable {
749-
// case query(required: Bool?)
750-
// case header(required: Bool?)
751-
// case path
752-
// case cookie(required: Bool?)
753-
// }
748+
public enum Location: Equatable {
749+
case query(required: Bool?)
750+
case header(required: Bool?)
751+
case path
752+
case cookie(required: Bool?)
753+
}
754754
}
755755

756756
public struct Operation: Equatable {

0 commit comments

Comments
 (0)