Skip to content

Commit 382668c

Browse files
authored
Merge pull request #63 from mattpolzin/refactor-context-names
1.0.0 Release
2 parents 58304ce + ed41448 commit 382668c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1381
-550
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2018 Mathew Polzin
1+
Copyright 2020 Mathew Polzin
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// swift-tools-version:5.1
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
32

43
import PackageDescription
54

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ A library containing Swift types that encode to- and decode from [OpenAPI](https
3131
- [Components Object (`OpenAPI.Components`)](#components-object-openapicomponents)
3232
- [Paths Object (`OpenAPI.PathItem.Map`)](#paths-object-openapipathitemmap)
3333
- [Path Item Object (`OpenAPI.PathItem`)](#path-item-object-openapipathitem)
34-
- [Operation Object (`OpenAPI.PathItem.Operation`)](#operation-object-openapipathitemoperation)
34+
- [Operation Object (`OpenAPI.Operation`)](#operation-object-openapipathitemoperation)
3535
- [External Document Object (`OpenAPI.ExternalDoc`)](#external-document-object-openapiexternaldoc)
36-
- [Parameter Object (`OpenAPI.PathItem.Parameter`)](#parameter-object-openapipathitemparameter)
36+
- [Parameter Object (`OpenAPI.Parameter`)](#parameter-object-openapipathitemparameter)
3737
- [Request Body Object (`OpenAPI.Request`)](#request-body-object-openapirequest)
3838
- [Media Type Object (`OpenAPI.Content`)](#media-type-object-openapicontent)
3939
- [Encoding Object (`OpenAPI.Content.Encoding`)](#encoding-object-openapicontentencoding)
@@ -101,10 +101,10 @@ The types used by this library largely mirror the object definitions found in th
101101
At the root there is an `OpenAPI.Document`. In addition to some information that applies to the entire API, the document contains `OpenAPI.Components` (essentially a dictionary of reusable components that can be referenced with `JSONReferences`) and an `OpenAPI.PathItem.Map` (a dictionary of routes your API defines).
102102

103103
#### Routes
104-
Each route is an entry in the document's `OpenAPI.PathItem.Map`. The keys of this dictionary are the paths for each route (i.e. `/widgets`). The values of this dictionary are `OpenAPI.PathItems` which define any combination of endpoints (i.e. `GET`, `POST`, `PATCH`, etc.) that the given route supports.
104+
Each route is an entry in the document's `OpenAPI.PathItem.Map`. The keys of this dictionary are the paths for each route (i.e. `/widgets`). The values of this dictionary are `OpenAPI.PathItems` which define any combination of endpoints (i.e. `GET`, `POST`, `PATCH`, etc.) that the given route supports. In addition to accessing endpoints on a path item under the name of the method (`.get`, `.post`, etc.), you can get an array of pairs matching endpoint methods to operations with the `.endpoints` method on `PathItem`.
105105

106106
#### Endpoints
107-
Each endpoint on a route is defined by an `OpenAPI.PathItem.Operation`. Among other things, this operation can specify the parameters (path, query, header, etc.), request body, and response bodies/codes supported by the given endpoint.
107+
Each endpoint on a route is defined by an `OpenAPI.Operation`. Among other things, this operation can specify the parameters (path, query, header, etc.), request body, and response bodies/codes supported by the given endpoint.
108108

109109
#### Request/Response Bodies
110110
Request and response bodies can be defined in great detail using OpenAPI's derivative of the JSON Schema specification. This library uses the `JSONSchema` type for such schema definitions.
@@ -183,7 +183,7 @@ For example,
183183
let apiDoc: OpenAPI.Document = ...
184184
let addBooksPath = apiDoc.paths["/cloudloading/addBook"]
185185

186-
let addBooksParameters: [OpenAPI.PathItem.Parameter]? = addBooksPath?.parameters.compactMap(apiDoc.components.dereference)
186+
let addBooksParameters: [OpenAPI.Parameter]? = addBooksPath?.parameters.compactMap(apiDoc.components.dereference)
187187
```
188188

189189
## Notes
@@ -275,7 +275,7 @@ See [**A note on dictionary ordering**](#a-note-on-dictionary-ordering) before d
275275
- [x] trace
276276
- [x] specification extensions (`vendorExtensions`)
277277

278-
### Operation Object (`OpenAPI.PathItem.Operation`)
278+
### Operation Object (`OpenAPI.Operation`)
279279
- [x] tags
280280
- [x] summary
281281
- [x] description
@@ -295,7 +295,7 @@ See [**A note on dictionary ordering**](#a-note-on-dictionary-ordering) before d
295295
- [x] url
296296
- [ ] specification extensions
297297

298-
### Parameter Object (`OpenAPI.PathItem.Parameter`)
298+
### Parameter Object (`OpenAPI.Parameter`)
299299
- [x] name
300300
- [x] in (`context`)
301301
- [x] description
@@ -387,7 +387,7 @@ See [**A note on dictionary ordering**](#a-note-on-dictionary-ordering) before d
387387
- [x] local (same file) reference (`internal` case)
388388
- [x] encode
389389
- [x] decode
390-
- [ ] dereference
390+
- [x] dereference
391391
- [x] remote (different file) reference (`external` case)
392392
- [x] encode
393393
- [x] decode
@@ -396,7 +396,7 @@ See [**A note on dictionary ordering**](#a-note-on-dictionary-ordering) before d
396396
### Schema Object (`JSONSchema`)
397397
- [x] Mostly complete support for JSON Schema inherited keywords
398398
- [x] nullable
399-
- [ ] discriminator
399+
- [x] discriminator
400400
- [x] readOnly (`permissions` `.readOnly` case)
401401
- [x] writeOnly (`permissions` `.writeOnly` case)
402402
- [ ] xml

Sources/OpenAPIKit/Component Object/Components+JSONReference.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ extension OpenAPI.Components {
6565
}
6666
}
6767

68+
/// Pass a reference to a component.
69+
/// `dereference()` will return the component value if it is found
70+
/// in the Components Object.
71+
///
72+
/// - Important: Dereferencing an external reference (i.e. one that points to another file)
73+
/// is not currently supported by OpenAPIKit and will therefore always result in `nil`.
74+
public func dereference<ReferenceType: ComponentDictionaryLocatable>(_ reference: JSONReference<ReferenceType>) -> ReferenceType? {
75+
return self[reference]
76+
}
77+
6878
/// Create a `JSONReference`.
6979
///
7080
/// - throws: If the given name does not refer to an existing component of the given type.

Sources/OpenAPIKit/Component Object/Components+Locatable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension OpenAPI.Response: ComponentDictionaryLocatable {
2828
public static var openAPIComponentsKeyPath: KeyPath<OpenAPI.Components, OpenAPI.ComponentDictionary<Self>> { \.responses }
2929
}
3030

31-
extension OpenAPI.PathItem.Parameter: ComponentDictionaryLocatable {
31+
extension OpenAPI.Parameter: ComponentDictionaryLocatable {
3232
public static var openAPIComponentsKey: String { "parameters" }
3333
public static var openAPIComponentsKeyPath: KeyPath<OpenAPI.Components, OpenAPI.ComponentDictionary<Self>> { \.parameters }
3434
}

Sources/OpenAPIKit/Component Object/Components.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension OpenAPI {
1818

1919
public var schemas: ComponentDictionary<JSONSchema>
2020
public var responses: ComponentDictionary<Response>
21-
public var parameters: ComponentDictionary<PathItem.Parameter>
21+
public var parameters: ComponentDictionary<Parameter>
2222
public var examples: ComponentDictionary<Example>
2323
public var requestBodies: ComponentDictionary<Request>
2424
public var headers: ComponentDictionary<Header>
@@ -35,7 +35,7 @@ extension OpenAPI {
3535

3636
public init(schemas: ComponentDictionary<JSONSchema> = [:],
3737
responses: ComponentDictionary<Response> = [:],
38-
parameters: ComponentDictionary<PathItem.Parameter> = [:],
38+
parameters: ComponentDictionary<Parameter> = [:],
3939
examples: ComponentDictionary<Example> = [:],
4040
requestBodies: ComponentDictionary<Request> = [:],
4141
headers: ComponentDictionary<Header> = [:],
@@ -163,7 +163,7 @@ extension OpenAPI.Components: Decodable {
163163
responses = try container.decodeIfPresent(OpenAPI.ComponentDictionary<OpenAPI.Response>.self, forKey: .responses)
164164
?? [:]
165165

166-
parameters = try container.decodeIfPresent(OpenAPI.ComponentDictionary<OpenAPI.PathItem.Parameter>.self, forKey: .parameters)
166+
parameters = try container.decodeIfPresent(OpenAPI.ComponentDictionary<OpenAPI.Parameter>.self, forKey: .parameters)
167167
?? [:]
168168

169169
examples = try container.decodeIfPresent(OpenAPI.ComponentDictionary<OpenAPI.Example>.self, forKey: .examples)
File renamed without changes.

Sources/OpenAPIKit/ContentEncoding.swift renamed to Sources/OpenAPIKit/Content/ContentEncoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension OpenAPI.Content {
1010
///
1111
/// See [OpenAPI Encoding Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#encoding-object).
1212
public struct Encoding: Equatable {
13-
public typealias Style = OpenAPI.PathItem.Parameter.Schema.Style
13+
public typealias Style = OpenAPI.Parameter.SchemaContext.Style
1414

1515
public let contentType: OpenAPI.ContentType?
1616
public let headers: OpenAPI.Header.Map?

Sources/OpenAPIKit/Document.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,20 @@ internal func decodeSecurityRequirements<CodingKeys: CodingKey>(from container:
286286

287287
internal func validateSecurityRequirements(in paths: OpenAPI.PathItem.Map, against components: OpenAPI.Components) throws {
288288
for (path, pathItem) in paths {
289-
for (verb, operation) in pathItem.endpoints {
290-
if let securityRequirements = operation.security {
289+
for endpoint in pathItem.endpoints {
290+
if let securityRequirements = endpoint.operation.security {
291291
try validate(
292292
securityRequirements: securityRequirements,
293293
at: path,
294-
for: verb,
294+
for: endpoint.method,
295295
against: components
296296
)
297297
}
298298
}
299299
}
300300
}
301301

302-
internal func validate(securityRequirements: [OpenAPI.SecurityRequirement], at path: OpenAPI.Path, for verb: OpenAPI.HttpVerb, against components: OpenAPI.Components) throws {
302+
internal func validate(securityRequirements: [OpenAPI.SecurityRequirement], at path: OpenAPI.Path, for verb: OpenAPI.HttpMethod, against components: OpenAPI.Components) throws {
303303
let securitySchemes = securityRequirements.flatMap { $0.keys }
304304

305305
for securityScheme in securitySchemes {

0 commit comments

Comments
 (0)