Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port of ExternallyDereferenceable to OpenAPIKit30 module #373

Merged
merged 11 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/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: ExternalLoader>(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: ExternalLoader>(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
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
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: ExternalLoader>(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/OpenAPIKit/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ extension OpenAPI.Example: LocallyDereferenceable {
}

extension OpenAPI.Example: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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/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)
}
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/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: ExternalLoader>(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
4 changes: 2 additions & 2 deletions Sources/OpenAPIKit/JSONReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ extension JSONReference: LocallyDereferenceable where ReferenceType: LocallyDere
}

extension JSONReference: ExternallyDereferenceable where ReferenceType: ExternallyDereferenceable & Decodable & Equatable {
public func externallyDereferenced<Context: ExternalLoader>(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 Expand Up @@ -580,7 +580,7 @@ extension OpenAPI.Reference: LocallyDereferenceable where ReferenceType: Locally
}

extension OpenAPI.Reference: ExternallyDereferenceable where ReferenceType: ExternallyDereferenceable & Decodable & Equatable {
public func externallyDereferenced<Context: ExternalLoader>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
let (newRef, components) = try await jsonReference.externallyDereferenced(with: loader)
return (.init(newRef), components)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Link.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ extension OpenAPI.Link: LocallyDereferenceable {
}

extension OpenAPI.Link: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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/OpenAPIKit/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: ExternalLoader>(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/OpenAPIKit/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: ExternalLoader>(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 @@ -71,7 +71,7 @@ extension OpenAPI.Parameter.SchemaContext: LocallyDereferenceable {
}

extension OpenAPI.Parameter.SchemaContext: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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
85 changes: 27 additions & 58 deletions Sources/OpenAPIKit/Path Item/DereferencedPathItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ extension OpenAPI.PathItem: LocallyDereferenceable {
}

extension OpenAPI.PathItem: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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 All @@ -154,77 +154,46 @@ extension OpenAPI.PathItem: ExternallyDereferenceable {

async let (newParameters, c1) = oldParameters.externallyDereferenced(with: loader)
// async let (newServers, c2) = oldServers.externallyDereferenced(with: loader)
// async let (newGet, c3) = oldGet.externallyDereferenced(with: loader)
// async let (newPut, c4) = oldPut.externallyDereferenced(with: loader)
// async let (newPost, c5) = oldPost.externallyDereferenced(with: loader)
// async let (newDelete, c6) = oldDelete.externallyDereferenced(with: loader)
// async let (newOptions, c7) = oldOptions.externallyDereferenced(with: loader)
// async let (newHead, c8) = oldHead.externallyDereferenced(with: loader)
// async let (newPatch, c9) = oldPatch.externallyDereferenced(with: loader)
// async let (newTrace, c10) = oldTrace.externallyDereferenced(with: loader)
async let (newGet, c3) = oldGet.externallyDereferenced(with: loader)
async let (newPut, c4) = oldPut.externallyDereferenced(with: loader)
async let (newPost, c5) = oldPost.externallyDereferenced(with: loader)
async let (newDelete, c6) = oldDelete.externallyDereferenced(with: loader)
async let (newOptions, c7) = oldOptions.externallyDereferenced(with: loader)
async let (newHead, c8) = oldHead.externallyDereferenced(with: loader)
async let (newPatch, c9) = oldPatch.externallyDereferenced(with: loader)
async let (newTrace, c10) = oldTrace.externallyDereferenced(with: loader)

var pathItem = self
var newComponents = try await c1

// ideally we would async let all of the props above and then set them here,
// but for now since there seems to be some sort of compiler bug we will do
// most of them in if lets below
// newServers in an if let below
pathItem.parameters = try await newParameters
pathItem.get = try await newGet
pathItem.put = try await newPut
pathItem.post = try await newPost
pathItem.delete = try await newDelete
pathItem.options = try await newOptions
pathItem.head = try await newHead
pathItem.patch = try await newPatch
pathItem.trace = try await newTrace

try await newComponents.merge(c3)
try await newComponents.merge(c4)
try await newComponents.merge(c5)
try await newComponents.merge(c6)
try await newComponents.merge(c7)
try await newComponents.merge(c8)
try await newComponents.merge(c9)
try await newComponents.merge(c10)

if let oldServers {
async let (newServers, c2) = oldServers.externallyDereferenced(with: loader)
pathItem.servers = try await newServers
try await newComponents.merge(c2)
}

if let oldGet {
async let (newGet, c3) = oldGet.externallyDereferenced(with: loader)
pathItem.get = try await newGet
try await newComponents.merge(c3)
}

if let oldPut {
async let (newPut, c4) = oldPut.externallyDereferenced(with: loader)
pathItem.put = try await newPut
try await newComponents.merge(c4)
}

if let oldPost {
async let (newPost, c5) = oldPost.externallyDereferenced(with: loader)
pathItem.post = try await newPost
try await newComponents.merge(c5)
}

if let oldDelete {
async let (newDelete, c6) = oldDelete.externallyDereferenced(with: loader)
pathItem.delete = try await newDelete
try await newComponents.merge(c6)
}

if let oldOptions {
async let (newOptions, c7) = oldOptions.externallyDereferenced(with: loader)
pathItem.options = try await newOptions
try await newComponents.merge(c7)
}

if let oldHead {
async let (newHead, c8) = oldHead.externallyDereferenced(with: loader)
pathItem.head = try await newHead
try await newComponents.merge(c8)
}

if let oldPatch {
async let (newPatch, c9) = oldPatch.externallyDereferenced(with: loader)
pathItem.patch = try await newPatch
try await newComponents.merge(c9)
}

if let oldTrace {
async let (newTrace, c10) = oldTrace.externallyDereferenced(with: loader)
pathItem.trace = try await newTrace
try await newComponents.merge(c10)
}

return (pathItem, newComponents)
}
}
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/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: ExternalLoader>(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
6 changes: 4 additions & 2 deletions Sources/OpenAPIKit/Response/DereferencedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ extension OpenAPI.Response: LocallyDereferenceable {
}

extension OpenAPI.Response: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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
let oldHeaders = headers

async let (newContent, c1) = oldContent.externallyDereferenced(with: loader)
async let (newLinks, c2) = oldLinks.externallyDereferenced(with: loader)
// async let (newHeaders, c3) = oldHeaders.externallyDereferenced(with: loader)

var response = self
response.content = try await newContent
Expand All @@ -92,7 +94,7 @@ extension OpenAPI.Response: ExternallyDereferenceable {
var components = try await c1
try await components.merge(c2)

if let oldHeaders = headers {
if let oldHeaders {
let (newHeaders, c3) = try await oldHeaders.externallyDereferenced(with: loader)
response.headers = newHeaders
try components.merge(c3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ extension JSONSchema: LocallyDereferenceable {
}

extension JSONSchema: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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
6 changes: 3 additions & 3 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import OpenAPIKitCore
public struct JSONSchema: JSONSchemaContext, HasWarnings {

public let warnings: [OpenAPI.Warning]
public let value: Schema
public var value: Schema

internal init(warnings: [OpenAPI.Warning], schema: Schema) {
self.warnings = warnings
Expand Down Expand Up @@ -441,8 +441,8 @@ extension JSONSchema: VendorExtendable {
get {
coreContext.vendorExtensions
}
set {
coreContext.vendorExtensions
set(extensions) {
self.value = value.with(vendorExtensions: extensions)
}
}

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

extension OpenAPI.SecurityScheme: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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())
}
}
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ extension OpenAPI.Server.Variable {
}

extension OpenAPI.Server: ExternallyDereferenceable {
public func externallyDereferenced<Context: ExternalLoader>(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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import OpenAPIKitCore

extension Array where Element: ExternallyDereferenceable {

public func externallyDereferenced<Context: ExternalLoader>(with loader: Context.Type) async throws -> (Self, OpenAPI.Components) {
public func externallyDereferenced<Loader: ExternalLoader>(with loader: Loader.Type) async throws -> (Self, OpenAPI.Components) {
try await withThrowingTaskGroup(of: (Int, (Element, OpenAPI.Components)).self) { group in
for (idx, elem) in zip(self.indices, self) {
group.addTask {
Expand Down
Loading
Loading