Skip to content

Commit 44ded67

Browse files
committed
split more stuff up into separate files, add more test stub files
1 parent df86045 commit 44ded67

File tree

5 files changed

+295
-253
lines changed

5 files changed

+295
-253
lines changed

Sources/OpenAPIKit/Path Item/Parameter.swift

Lines changed: 0 additions & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -92,196 +92,6 @@ extension OpenAPI.PathItem {
9292
}
9393
}
9494

95-
extension OpenAPI.PathItem.Parameter {
96-
public enum Location: Equatable {
97-
case query(required: Bool, allowEmptyValue: Bool)
98-
case header(required: Bool)
99-
case path
100-
case cookie(required: Bool)
101-
102-
public static func query(required: Bool) -> Location { return .query(required: required, allowEmptyValue: false) }
103-
104-
public static func query(allowEmptyValue: Bool) -> Location { return .query(required: false, allowEmptyValue: allowEmptyValue) }
105-
106-
public static var query: Location { return .query(required: false, allowEmptyValue: false) }
107-
108-
public static var header: Location { return .header(required: false) }
109-
110-
public static var cookie: Location { return .cookie(required: false) }
111-
112-
public var isQuery: Bool {
113-
guard case .query = self else {
114-
return false
115-
}
116-
return true
117-
}
118-
119-
public var isHeader: Bool {
120-
guard case .header = self else {
121-
return false
122-
}
123-
return true
124-
}
125-
126-
public var isPath: Bool { return self == .path }
127-
128-
public var isCookie: Bool {
129-
guard case .cookie = self else {
130-
return false
131-
}
132-
return true
133-
}
134-
}
135-
}
136-
137-
extension OpenAPI.PathItem.Parameter {
138-
public struct Schema: Equatable {
139-
public let style: Style
140-
public let explode: Bool
141-
public let allowReserved: Bool //defaults to false
142-
public let schema: Either<JSONReference<OpenAPI.Components, JSONSchema>, JSONSchema>
143-
144-
public let example: AnyCodable?
145-
public let examples: OpenAPI.Example.Map?
146-
147-
public init(_ schema: JSONSchema,
148-
style: Style,
149-
explode: Bool,
150-
allowReserved: Bool = false,
151-
example: AnyCodable? = nil) {
152-
self.style = style
153-
self.explode = explode
154-
self.allowReserved = allowReserved
155-
self.schema = .init(schema)
156-
self.example = example
157-
self.examples = nil
158-
}
159-
160-
public init(_ schema: JSONSchema,
161-
style: Style,
162-
allowReserved: Bool = false,
163-
example: AnyCodable? = nil) {
164-
self.style = style
165-
self.allowReserved = allowReserved
166-
self.schema = .init(schema)
167-
self.example = example
168-
self.examples = nil
169-
170-
self.explode = style.defaultExplode
171-
}
172-
173-
public init(schemaReference: JSONReference<OpenAPI.Components, JSONSchema>,
174-
style: Style,
175-
explode: Bool,
176-
allowReserved: Bool = false,
177-
example: AnyCodable? = nil) {
178-
self.style = style
179-
self.explode = explode
180-
self.allowReserved = allowReserved
181-
self.schema = .init(schemaReference)
182-
self.example = example
183-
self.examples = nil
184-
}
185-
186-
public init(schemaReference: JSONReference<OpenAPI.Components, JSONSchema>,
187-
style: Style,
188-
allowReserved: Bool = false,
189-
example: AnyCodable? = nil) {
190-
self.style = style
191-
self.allowReserved = allowReserved
192-
self.schema = .init(schemaReference)
193-
self.example = example
194-
self.examples = nil
195-
196-
self.explode = style.defaultExplode
197-
}
198-
199-
public init(_ schema: JSONSchema,
200-
style: Style,
201-
explode: Bool,
202-
allowReserved: Bool = false,
203-
examples: OpenAPI.Example.Map?) {
204-
self.style = style
205-
self.explode = explode
206-
self.allowReserved = allowReserved
207-
self.schema = .init(schema)
208-
self.examples = examples
209-
self.example = examples.flatMap(OpenAPI.Content.firstExample(from:))
210-
}
211-
212-
public init(_ schema: JSONSchema,
213-
style: Style,
214-
allowReserved: Bool = false,
215-
examples: OpenAPI.Example.Map?) {
216-
self.style = style
217-
self.allowReserved = allowReserved
218-
self.schema = .init(schema)
219-
self.examples = examples
220-
self.example = examples.flatMap(OpenAPI.Content.firstExample(from:))
221-
222-
self.explode = style.defaultExplode
223-
}
224-
225-
public init(schemaReference: JSONReference<OpenAPI.Components, JSONSchema>,
226-
style: Style,
227-
explode: Bool,
228-
allowReserved: Bool = false,
229-
examples: OpenAPI.Example.Map?) {
230-
self.style = style
231-
self.explode = explode
232-
self.allowReserved = allowReserved
233-
self.schema = .init(schemaReference)
234-
self.examples = examples
235-
self.example = examples.flatMap(OpenAPI.Content.firstExample(from:))
236-
}
237-
238-
public init(schemaReference: JSONReference<OpenAPI.Components, JSONSchema>,
239-
style: Style,
240-
allowReserved: Bool = false,
241-
examples: OpenAPI.Example.Map?) {
242-
self.style = style
243-
self.allowReserved = allowReserved
244-
self.schema = .init(schemaReference)
245-
self.examples = examples
246-
self.example = examples.flatMap(OpenAPI.Content.firstExample(from:))
247-
248-
self.explode = style.defaultExplode
249-
}
250-
251-
public enum Style: String, CaseIterable, Codable {
252-
case form
253-
case simple
254-
case matrix
255-
case label
256-
case spaceDelimited
257-
case pipeDelimited
258-
case deepObject
259-
260-
public static func `default`(for location: OpenAPI.PathItem.Parameter.Location) -> Self {
261-
switch location {
262-
case .query:
263-
return .form
264-
case .cookie:
265-
return .form
266-
case .path:
267-
return .simple
268-
case .header:
269-
return .simple
270-
}
271-
}
272-
273-
internal var defaultExplode: Bool {
274-
switch self {
275-
case .form:
276-
return true
277-
default:
278-
return false
279-
}
280-
}
281-
}
282-
}
283-
}
284-
28595
// MARK: `Either` convenience methods
28696
// OpenAPI.PathItem.Array.Element =>
28797
extension Either where A == OpenAPI.PathItem.Parameter, B == JSONReference<OpenAPI.Components, OpenAPI.PathItem.Parameter> {
@@ -435,66 +245,3 @@ extension OpenAPI.PathItem.Parameter: Decodable {
435245
deprecated = try container.decodeIfPresent(Bool.self, forKey: .deprecated) ?? false
436246
}
437247
}
438-
439-
extension OpenAPI.PathItem.Parameter.Schema {
440-
private enum CodingKeys: String, CodingKey {
441-
case style
442-
case explode
443-
case allowReserved
444-
case schema
445-
446-
// the following two are alternatives
447-
case example
448-
case examples
449-
}
450-
}
451-
452-
extension OpenAPI.PathItem.Parameter.Schema {
453-
public func encode(to encoder: Encoder, for location: OpenAPI.PathItem.Parameter.Location) throws {
454-
var container = encoder.container(keyedBy: CodingKeys.self)
455-
456-
if style != Style.default(for: location) {
457-
try container.encode(style, forKey: .style)
458-
}
459-
460-
if explode != style.defaultExplode {
461-
try container.encode(explode, forKey: .explode)
462-
}
463-
464-
if allowReserved != false {
465-
try container.encode(allowReserved, forKey: .allowReserved)
466-
}
467-
468-
try container.encode(schema, forKey: .schema)
469-
470-
if examples != nil {
471-
try container.encode(examples, forKey: .examples)
472-
} else if example != nil {
473-
try container.encode(example, forKey: .example)
474-
}
475-
}
476-
}
477-
478-
extension OpenAPI.PathItem.Parameter.Schema {
479-
public init(from decoder: Decoder, for location: OpenAPI.PathItem.Parameter.Location) throws {
480-
let container = try decoder.container(keyedBy: CodingKeys.self)
481-
482-
schema = try container.decode(Either<JSONReference<OpenAPI.Components, JSONSchema>, JSONSchema>.self, forKey: .schema)
483-
484-
let style = try container.decodeIfPresent(Style.self, forKey: .style) ?? Style.default(for: location)
485-
self.style = style
486-
487-
explode = try container.decodeIfPresent(Bool.self, forKey: .explode) ?? style.defaultExplode
488-
489-
allowReserved = try container.decodeIfPresent(Bool.self, forKey: .allowReserved) ?? false
490-
491-
if container.contains(.example) {
492-
example = try container.decode(AnyCodable.self, forKey: .example)
493-
examples = nil
494-
} else {
495-
let examplesMap = try container.decodeIfPresent(OpenAPI.Example.Map.self, forKey: .examples)
496-
examples = examplesMap
497-
example = examplesMap.flatMap(OpenAPI.Content.firstExample(from:))
498-
}
499-
}
500-
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// ParameterLocation.swift
3+
//
4+
//
5+
// Created by Mathew Polzin on 12/29/19.
6+
//
7+
8+
extension OpenAPI.PathItem.Parameter {
9+
public enum Location: Equatable {
10+
case query(required: Bool, allowEmptyValue: Bool)
11+
case header(required: Bool)
12+
case path
13+
case cookie(required: Bool)
14+
15+
public static func query(required: Bool) -> Location { return .query(required: required, allowEmptyValue: false) }
16+
17+
public static func query(allowEmptyValue: Bool) -> Location { return .query(required: false, allowEmptyValue: allowEmptyValue) }
18+
19+
public static var query: Location { return .query(required: false, allowEmptyValue: false) }
20+
21+
public static var header: Location { return .header(required: false) }
22+
23+
public static var cookie: Location { return .cookie(required: false) }
24+
25+
public var isQuery: Bool {
26+
guard case .query = self else {
27+
return false
28+
}
29+
return true
30+
}
31+
32+
public var isHeader: Bool {
33+
guard case .header = self else {
34+
return false
35+
}
36+
return true
37+
}
38+
39+
public var isPath: Bool { return self == .path }
40+
41+
public var isCookie: Bool {
42+
guard case .cookie = self else {
43+
return false
44+
}
45+
return true
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)