Skip to content

Commit

Permalink
go from ExternalLoaderContext to ExternalLoader in OAS 3.0 port. upda…
Browse files Browse the repository at this point in the history
…te parameter name in document function to be less awkward with Loader rather than Context.
  • Loading branch information
mattpolzin committed Apr 24, 2024
1 parent 2f622ce commit e4a2b52
Show file tree
Hide file tree
Showing 28 changed files with 421 additions and 75 deletions.
24 changes: 12 additions & 12 deletions Sources/OpenAPIKit/Components Object/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ extension OpenAPI.Components {
}

extension OpenAPI.Components {
internal mutating func externallyDereference<Context: ExternalLoader>(in context: Context.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
internal mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
if case let .iterations(number) = depth,
number <= 0 {
return
Expand All @@ -336,15 +336,15 @@ extension OpenAPI.Components {
let oldCallbacks = callbacks
let oldPathItems = pathItems

async let (newSchemas, c1) = oldSchemas.externallyDereferenced(with: context)
async let (newResponses, c2) = oldResponses.externallyDereferenced(with: context)
async let (newParameters, c3) = oldParameters.externallyDereferenced(with: context)
async let (newExamples, c4) = oldExamples.externallyDereferenced(with: context)
async let (newRequestBodies, c5) = oldRequestBodies.externallyDereferenced(with: context)
async let (newHeaders, c6) = oldHeaders.externallyDereferenced(with: context)
async let (newSecuritySchemes, c7) = oldSecuritySchemes.externallyDereferenced(with: context)
async let (newCallbacks, c8) = oldCallbacks.externallyDereferenced(with: context)
async let (newPathItems, c9) = oldPathItems.externallyDereferenced(with: context)
async let (newSchemas, c1) = oldSchemas.externallyDereferenced(with: loader)
async let (newResponses, c2) = oldResponses.externallyDereferenced(with: loader)
async let (newParameters, c3) = oldParameters.externallyDereferenced(with: loader)
async let (newExamples, c4) = oldExamples.externallyDereferenced(with: loader)
async let (newRequestBodies, c5) = oldRequestBodies.externallyDereferenced(with: loader)
async let (newHeaders, c6) = oldHeaders.externallyDereferenced(with: loader)
async let (newSecuritySchemes, c7) = oldSecuritySchemes.externallyDereferenced(with: loader)
async let (newCallbacks, c8) = oldCallbacks.externallyDereferenced(with: loader)
async let (newPathItems, c9) = oldPathItems.externallyDereferenced(with: loader)

schemas = try await newSchemas
responses = try await newResponses
Expand Down Expand Up @@ -391,9 +391,9 @@ extension OpenAPI.Components {

switch depth {
case .iterations(let number):
try await externallyDereference(in: context, depth: .iterations(number - 1))
try await externallyDereference(with: loader, depth: .iterations(number - 1))
case .full:
try await externallyDereference(in: context, depth: .full)
try await externallyDereference(with: loader, depth: .full)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/OpenAPIKit/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ extension OpenAPI.Document {
return try DereferencedDocument(self)
}

public mutating func externallyDereference<Context: ExternalLoader>(in context: Context.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
public mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
if case let .iterations(number) = depth,
number <= 0 {
return
Expand All @@ -371,15 +371,15 @@ extension OpenAPI.Document {
let oldPaths = paths
let oldWebhooks = webhooks

async let (newPaths, c1) = oldPaths.externallyDereferenced(with: context)
async let (newWebhooks, c2) = oldWebhooks.externallyDereferenced(with: context)
async let (newPaths, c1) = oldPaths.externallyDereferenced(with: loader)
async let (newWebhooks, c2) = oldWebhooks.externallyDereferenced(with: loader)

paths = try await newPaths
webhooks = try await newWebhooks
try await components.merge(c1)
try await components.merge(c2)

try await components.externallyDereference(in: context, depth: depth)
try await components.externallyDereference(with: loader, depth: depth)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/ExternalLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public protocol ExternalLoader {
}

public protocol ExternallyDereferenceable {
func externallyDereferenced<Context: ExternalLoader>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components)
func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components)
}
90 changes: 59 additions & 31 deletions Sources/OpenAPIKit30/Components Object/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,33 +283,31 @@ extension OpenAPI.Components {
}

extension OpenAPI.Components {
internal mutating func externallyDereference<Context: ExternalLoaderContext>(in context: Context.Type) async throws {
internal mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
if case let .iterations(number) = depth,
number <= 0 {
return
}

let oldSchemas = schemas
let oldResponses = responses
let oldParameters = parameters
let oldExamples = examples
let oldRequestBodies = requestBodies
let oldHeaders = headers
let oldSecuritySchemes = securitySchemes

let oldCallbacks = callbacks

async let (newSchemas, c1) = oldSchemas.externallyDereferenced(with: context)
async let (newResponses, c2) = oldResponses.externallyDereferenced(with: context)
async let (newParameters, c3) = oldParameters.externallyDereferenced(with: context)
async let (newExamples, c4) = oldExamples.externallyDereferenced(with: context)
async let (newRequestBodies, c5) = oldRequestBodies.externallyDereferenced(with: context)
async let (newHeaders, c6) = oldHeaders.externallyDereferenced(with: context)
async let (newSecuritySchemes, c7) = oldSecuritySchemes.externallyDereferenced(with: context)

// async let (newCallbacks, c8) = oldCallbacks.externallyDereferenced(with: context)
var c8 = OpenAPI.Components()
var newCallbacks = oldCallbacks
for (key, callback) in oldCallbacks {
let (newCallback, components) = try await callback.externallyDereferenced(with: context)
newCallbacks[key] = newCallback
try c8.merge(components)
}
let oldPathItems = pathItems

async let (newSchemas, c1) = oldSchemas.externallyDereferenced(with: loader)
async let (newResponses, c2) = oldResponses.externallyDereferenced(with: loader)
async let (newParameters, c3) = oldParameters.externallyDereferenced(with: loader)
async let (newExamples, c4) = oldExamples.externallyDereferenced(with: loader)
async let (newRequestBodies, c5) = oldRequestBodies.externallyDereferenced(with: loader)
async let (newHeaders, c6) = oldHeaders.externallyDereferenced(with: loader)
async let (newSecuritySchemes, c7) = oldSecuritySchemes.externallyDereferenced(with: loader)
async let (newCallbacks, c8) = oldCallbacks.externallyDereferenced(with: loader)
async let (newPathItems, c9) = oldPathItems.externallyDereferenced(with: loader)

schemas = try await newSchemas
responses = try await newResponses
Expand All @@ -318,18 +316,48 @@ extension OpenAPI.Components {
requestBodies = try await newRequestBodies
headers = try await newHeaders
securitySchemes = try await newSecuritySchemes

callbacks = newCallbacks

try await merge(c1)
try await merge(c2)
try await merge(c3)
try await merge(c4)
try await merge(c5)
try await merge(c6)
try await merge(c7)

try merge(c8)
callbacks = try await newCallbacks
pathItems = try await newPathItems

let c1Resolved = try await c1
let c2Resolved = try await c2
let c3Resolved = try await c3
let c4Resolved = try await c4
let c5Resolved = try await c5
let c6Resolved = try await c6
let c7Resolved = try await c7
let c8Resolved = try await c8
let c9Resolved = try await c9

let noNewComponents =
c1Resolved.isEmpty
&& c2Resolved.isEmpty
&& c3Resolved.isEmpty
&& c4Resolved.isEmpty
&& c5Resolved.isEmpty
&& c6Resolved.isEmpty
&& c7Resolved.isEmpty
&& c8Resolved.isEmpty
&& c9Resolved.isEmpty

if noNewComponents { return }

try merge(c1Resolved)
try merge(c2Resolved)
try merge(c3Resolved)
try merge(c4Resolved)
try merge(c5Resolved)
try merge(c6Resolved)
try merge(c7Resolved)
try merge(c8Resolved)
try merge(c9Resolved)

switch depth {
case .iterations(let number):
try await externallyDereference(with: loader, depth: .iterations(number - 1))
case .full:
try await externallyDereference(with: loader, depth: .full)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Content/DereferencedContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension OpenAPI.Content: LocallyDereferenceable {
}

extension OpenAPI.Content: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let oldSchema = schema

async let (newSchema, c1) = oldSchema.externallyDereferenced(with: loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension OpenAPI.Content.Encoding: LocallyDereferenceable {
}

extension OpenAPI.Content.Encoding: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let newHeaders: OpenAPI.Header.Map?
let newComponents: OpenAPI.Components

Expand Down
24 changes: 20 additions & 4 deletions Sources/OpenAPIKit30/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ extension OpenAPI.Document {
}
}

public enum ExternalDereferenceDepth {
case iterations(Int)
case full
}

extension ExternalDereferenceDepth: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self = .iterations(value)
}
}

extension OpenAPI.Document {
/// Create a locally-dereferenced OpenAPI
/// Document.
Expand Down Expand Up @@ -335,15 +346,20 @@ extension OpenAPI.Document {
return try DereferencedDocument(self)
}

public mutating func externallyDereference<Context: ExternalLoaderContext>(in context: Context.Type) async throws {
let oldPaths = paths
public mutating func externallyDereference<Loader: ExternalLoader>(with loader: Loader.Type, depth: ExternalDereferenceDepth = .iterations(1)) async throws {
if case let .iterations(number) = depth,
number <= 0 {
return
}

try await components.externallyDereference(in: context)
let oldPaths = paths

async let (newPaths, c1) = oldPaths.externallyDereferenced(with: context)
async let (newPaths, c1) = oldPaths.externallyDereferenced(with: loader)

paths = try await newPaths
try await components.merge(c1)

try await components.externallyDereference(with: loader, depth: depth)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import OpenAPIKitCore
// MARK: - ExternallyDereferenceable
extension Either: ExternallyDereferenceable where A: ExternallyDereferenceable, B: ExternallyDereferenceable {

public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
switch self {
case .a(let a):
let (newA, components) = try await a.externallyDereferenced(with: loader)
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extension OpenAPI.Example: LocallyDereferenceable {
}

extension OpenAPI.Example: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
return (self, .init())
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAPIKit30/ExternalLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import OpenAPIKitCore
import Foundation

/// An `ExternalLoaderContext` enables `OpenAPIKit` to load external references
/// An `ExternalLoader` enables `OpenAPIKit` to load external references
/// without knowing the details of what decoder is being used or how new internal
/// references should be named.
public protocol ExternalLoaderContext {
public protocol ExternalLoader {
/// Load the given URL and decode it as Type `T`. All Types `T` are `Decodable`, so
/// the only real responsibility of a `load` function is to locate and load the given
/// `URL` and pass its `Data` or `String` (depending on the decoder) to an appropriate
Expand All @@ -30,5 +30,5 @@ public protocol ExternalLoaderContext {
}

public protocol ExternallyDereferenceable {
func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components)
func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components)
}
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Header/DereferencedHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extension OpenAPI.Header: LocallyDereferenceable {
}

extension OpenAPI.Header: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {

// if not for a Swift bug, this whole next bit would just be the
// next line:
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/JSONReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ extension JSONReference: LocallyDereferenceable where ReferenceType: LocallyDere

// MARK: - ExternallyDereferenceable
extension JSONReference: ExternallyDereferenceable where ReferenceType: ExternallyDereferenceable & Decodable & Equatable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
switch self {
case .internal(let ref):
return (.internal(ref), .init())
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Link.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ extension OpenAPI.Link: LocallyDereferenceable {
}

extension OpenAPI.Link: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let (newServer, newComponents) = try await server.externallyDereferenced(with: loader)

var newLink = self
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Operation/DereferencedOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension OpenAPI.Operation: LocallyDereferenceable {
}

extension OpenAPI.Operation: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let oldParameters = parameters
let oldRequestBody = requestBody
let oldResponses = responses
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Parameter/DereferencedParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extension OpenAPI.Parameter: LocallyDereferenceable {
}

extension OpenAPI.Parameter: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {

// if not for a Swift bug, this whole function would just be the
// next line:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension OpenAPI.Parameter.SchemaContext: LocallyDereferenceable {
}

extension OpenAPI.Parameter.SchemaContext: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let oldSchema = schema

async let (newSchema, c1) = oldSchema.externallyDereferenced(with: loader)
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Path Item/DereferencedPathItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension OpenAPI.PathItem: LocallyDereferenceable {
}

extension OpenAPI.PathItem: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let oldParameters = parameters
let oldServers = servers
let oldGet = get
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Request/DereferencedRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension OpenAPI.Request: LocallyDereferenceable {
}

extension OpenAPI.Request: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
var newRequest = self

let (newContent, components) = try await content.externallyDereferenced(with: loader)
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Response/DereferencedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension OpenAPI.Response: LocallyDereferenceable {
}

extension OpenAPI.Response: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let oldContent = content
let oldLinks = links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ extension JSONSchema: LocallyDereferenceable {
}

extension JSONSchema: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let newSchema: JSONSchema
let newComponents: OpenAPI.Components

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Security/SecurityScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ extension OpenAPI.SecurityScheme: LocallyDereferenceable {
}

extension OpenAPI.SecurityScheme: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
return (self, .init())
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ extension OpenAPI.Server.Variable {
}

extension OpenAPI.Server: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoaderContext>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
return (self, .init())
}
}
Expand Down
Loading

0 comments on commit e4a2b52

Please sign in to comment.