From 8bd7363cf20c0699b95a1026925ad3c2ae6986e3 Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Tue, 4 Jul 2023 07:10:18 +0330 Subject: [PATCH 1/4] Use existential any --- .../StructuredSwiftRepresentation.swift | 5 +++ .../Renderer/TextBasedRenderer.swift | 3 ++ .../ClientTranslator/ClientTranslator.swift | 8 +++- .../CommonTranslations/translateCodable.swift | 4 +- .../ServerTranslator/ServerTranslator.swift | 3 +- .../ReferenceSources/Petstore/Client.swift | 4 +- .../ReferenceSources/Petstore/Server.swift | 4 +- .../ReferenceSources/Petstore/Types.swift | 38 ++++++++++--------- 8 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift index 29989ae3..72d1ed8d 100644 --- a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift +++ b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift @@ -377,6 +377,11 @@ struct ParameterDescription: Equatable, Codable { /// For example, in `bar baz: String = "hi"`, `name` is `baz`. var name: String? = nil + /// The existential any keyword. + /// + /// For example, in `bar baz: any Client = ...`, `any` is the keyword referred to here. + var anyKeyword: Bool = false + /// The type name of the parameter. /// /// For example, in `bar baz: String = "hi"`, `type` is `String`. diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 4985ebb9..33e26861 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -610,6 +610,9 @@ struct TextBasedRenderer: RendererProtocol { } } words.append(":") + if parameterDescription.anyKeyword { + words.append("any") + } words.append(parameterDescription.type) if let defaultValue = parameterDescription.defaultValue { words.append("=") diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift index 49abd18b..d1b94b85 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift @@ -79,10 +79,14 @@ struct ClientFileTranslator: FileTranslator { type: Constants.Configuration.typeName, defaultValue: .dot("init").call([]) ), - .init(label: "transport", type: Constants.Client.Transport.typeName), + .init( + label: "transport", + anyKeyword: true, + type: Constants.Client.Transport.typeName + ), .init( label: "middlewares", - type: "[\(Constants.Client.Middleware.typeName)]", + type: "[any \(Constants.Client.Middleware.typeName)]", defaultValue: .literal(.array([])) ), ], diff --git a/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift b/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift index cba769d4..5477034d 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift @@ -593,7 +593,7 @@ fileprivate extension FileTranslator { accessModifier: config.access, kind: .function(name: "encode"), parameters: [ - .init(label: "to", name: "encoder", type: "Encoder") + .init(label: "to", name: "encoder", anyKeyword: true, type: "Encoder") ], keywords: [ .throws @@ -610,7 +610,7 @@ fileprivate extension FileTranslator { accessModifier: config.access, kind: .initializer, parameters: [ - .init(label: "from", name: "decoder", type: "Decoder") + .init(label: "from", name: "decoder", anyKeyword: true, type: "Decoder") ], keywords: [ .throws diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift index f1360371..15a33618 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift @@ -79,6 +79,7 @@ struct ServerFileTranslator: FileTranslator { .init( label: "on", name: "transport", + anyKeyword: true, type: Constants.Server.Transport.typeName ), .init( @@ -93,7 +94,7 @@ struct ServerFileTranslator: FileTranslator { ), .init( label: "middlewares", - type: "[\(Constants.Server.Middleware.typeName)]", + type: "[any \(Constants.Server.Middleware.typeName)]", defaultValue: .literal(.array([])) ), ], diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift index eec517b0..d8cd6c51 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift @@ -22,8 +22,8 @@ public struct Client: APIProtocol { public init( serverURL: URL, configuration: Configuration = .init(), - transport: ClientTransport, - middlewares: [ClientMiddleware] = [] + transport: any ClientTransport, + middlewares: [any ClientMiddleware] = [] ) { self.client = .init( serverURL: serverURL, diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift index 88155753..65bb9c9c 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift @@ -14,10 +14,10 @@ extension APIProtocol { /// - configuration: A set of configuration values for the server. /// - middlewares: A list of middlewares to call before the handler. public func registerHandlers( - on transport: ServerTransport, + on transport: any ServerTransport, serverURL: URL = .defaultOpenAPIServerURL, configuration: Configuration = .init(), - middlewares: [ServerMiddleware] = [] + middlewares: [any ServerMiddleware] = [] ) throws { let server = UniversalServer( serverURL: serverURL, diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift index a510aad7..1323ed61 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift @@ -177,8 +177,10 @@ public enum Components { /// - Parameters: /// - value1: public init(value1: Components.Schemas.ExtraInfo) { self.value1 = value1 } - public init(from decoder: Decoder) throws { value1 = try .init(from: decoder) } - public func encode(to encoder: Encoder) throws { try value1.encode(to: encoder) } + public init(from decoder: any Decoder) throws { value1 = try .init(from: decoder) } + public func encode(to encoder: any Encoder) throws { + try value1.encode(to: encoder) + } } /// Extra information about the error. /// @@ -268,7 +270,7 @@ public enum Components { /// - foo: public init(foo: Swift.String? = nil) { self.foo = foo } public enum CodingKeys: String, CodingKey { case foo } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) foo = try container.decodeIfPresent(Swift.String.self, forKey: .foo) try decoder.ensureNoAdditionalProperties(knownKeys: ["foo"]) @@ -293,12 +295,12 @@ public enum Components { self.additionalProperties = additionalProperties } public enum CodingKeys: String, CodingKey { case foo } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) foo = try container.decodeIfPresent(Swift.String.self, forKey: .foo) additionalProperties = try decoder.decodeAdditionalProperties(knownKeys: ["foo"]) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(foo, forKey: .foo) try encoder.encodeAdditionalProperties(additionalProperties) @@ -323,12 +325,12 @@ public enum Components { self.additionalProperties = additionalProperties } public enum CodingKeys: String, CodingKey { case foo } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) foo = try container.decodeIfPresent(Swift.String.self, forKey: .foo) additionalProperties = try decoder.decodeAdditionalProperties(knownKeys: ["foo"]) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(foo, forKey: .foo) try encoder.encodeAdditionalProperties(additionalProperties) @@ -374,11 +376,11 @@ public enum Components { self.value1 = value1 self.value2 = value2 } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { value1 = try .init(from: decoder) value2 = try .init(from: decoder) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { try value1.encode(to: encoder) try value2.encode(to: encoder) } @@ -412,7 +414,7 @@ public enum Components { self.value1 = value1 self.value2 = value2 } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { value1 = try? .init(from: decoder) value2 = try? .init(from: decoder) try DecodingError.verifyAtLeastOneSchemaIsNotNil( @@ -421,7 +423,7 @@ public enum Components { codingPath: decoder.codingPath ) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { try value1?.encode(to: encoder) try value2?.encode(to: encoder) } @@ -449,7 +451,7 @@ public enum Components { case case4(Components.Schemas.OneOfAny.Case4Payload) /// Parsed a case that was not defined in the OpenAPI document. case undocumented(OpenAPIRuntime.OpenAPIValueContainer) - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { do { self = .case1(try .init(from: decoder)) return @@ -470,7 +472,7 @@ public enum Components { let value = try container.decode(OpenAPIRuntime.OpenAPIValueContainer.self) self = .undocumented(value) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { switch self { case let .case1(value): try value.encode(to: encoder) case let .case2(value): try value.encode(to: encoder) @@ -540,11 +542,11 @@ public enum Components { self.value1 = value1 self.value2 = value2 } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { value1 = try .init(from: decoder) value2 = try .init(from: decoder) } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { try value1.encode(to: encoder) try value2.encode(to: encoder) } @@ -558,7 +560,7 @@ public enum Components { /// Parsed a case that was not defined in the OpenAPI document. case undocumented(OpenAPIRuntime.OpenAPIObjectContainer) public enum CodingKeys: String, CodingKey { case kind } - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let discriminator = try container.decode(String.self, forKey: .kind) switch discriminator { @@ -570,7 +572,7 @@ public enum Components { self = .undocumented(value) } } - public func encode(to encoder: Encoder) throws { + public func encode(to encoder: any Encoder) throws { switch self { case let .Walk(value): try value.encode(to: encoder) case let .MessagedExercise(value): try value.encode(to: encoder) @@ -583,7 +585,7 @@ public enum Components { public struct DeprecatedObject: Codable, Equatable, Hashable, Sendable { /// Creates a new `DeprecatedObject`. public init() {} - public init(from decoder: Decoder) throws { + public init(from decoder: any Decoder) throws { try decoder.ensureNoAdditionalProperties(knownKeys: []) } } From 2b697549daedbf5feb8f3177e43adf56880e7add Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Tue, 11 Jul 2023 13:09:54 +0330 Subject: [PATCH 2/4] move "any"s to the type names --- .../Layers/StructuredSwiftRepresentation.swift | 5 ----- .../Renderer/TextBasedRenderer.swift | 3 --- .../Translator/ClientTranslator/ClientTranslator.swift | 3 +-- .../Translator/CommonTranslations/translateCodable.swift | 4 ++-- .../Translator/CommonTypes/Constants.swift | 8 ++++---- .../Translator/ServerTranslator/ServerTranslator.swift | 3 +-- 6 files changed, 8 insertions(+), 18 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift index 72d1ed8d..29989ae3 100644 --- a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift +++ b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift @@ -377,11 +377,6 @@ struct ParameterDescription: Equatable, Codable { /// For example, in `bar baz: String = "hi"`, `name` is `baz`. var name: String? = nil - /// The existential any keyword. - /// - /// For example, in `bar baz: any Client = ...`, `any` is the keyword referred to here. - var anyKeyword: Bool = false - /// The type name of the parameter. /// /// For example, in `bar baz: String = "hi"`, `type` is `String`. diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 33e26861..4985ebb9 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -610,9 +610,6 @@ struct TextBasedRenderer: RendererProtocol { } } words.append(":") - if parameterDescription.anyKeyword { - words.append("any") - } words.append(parameterDescription.type) if let defaultValue = parameterDescription.defaultValue { words.append("=") diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift index d1b94b85..1f7ecf8f 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift @@ -81,12 +81,11 @@ struct ClientFileTranslator: FileTranslator { ), .init( label: "transport", - anyKeyword: true, type: Constants.Client.Transport.typeName ), .init( label: "middlewares", - type: "[any \(Constants.Client.Middleware.typeName)]", + type: "[\(Constants.Client.Middleware.typeName)]", defaultValue: .literal(.array([])) ), ], diff --git a/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift b/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift index 5477034d..47247f08 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift @@ -593,7 +593,7 @@ fileprivate extension FileTranslator { accessModifier: config.access, kind: .function(name: "encode"), parameters: [ - .init(label: "to", name: "encoder", anyKeyword: true, type: "Encoder") + .init(label: "to", name: "encoder", type: "any Encoder") ], keywords: [ .throws @@ -610,7 +610,7 @@ fileprivate extension FileTranslator { accessModifier: config.access, kind: .initializer, parameters: [ - .init(label: "from", name: "decoder", anyKeyword: true, type: "Decoder") + .init(label: "from", name: "decoder", type: "any Decoder") ], keywords: [ .throws diff --git a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift index 042ac6ff..95b8d097 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift @@ -80,14 +80,14 @@ enum Constants { enum Transport { /// The name of the client transport type. - static let typeName: String = "ClientTransport" + static let typeName: String = "any ClientTransport" } /// Constants related to the client middleware type. enum Middleware { /// The name of the client middleware type. - static let typeName: String = "ClientMiddleware" + static let typeName: String = "any ClientMiddleware" } } @@ -110,14 +110,14 @@ enum Constants { enum Transport { /// The name of the server transport type. - static let typeName: String = "ServerTransport" + static let typeName: String = "any ServerTransport" } /// Constants related to the server middleware type. enum Middleware { /// The name of the server middleware type. - static let typeName: String = "ServerMiddleware" + static let typeName: String = "any ServerMiddleware" } } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift index 15a33618..f1360371 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift @@ -79,7 +79,6 @@ struct ServerFileTranslator: FileTranslator { .init( label: "on", name: "transport", - anyKeyword: true, type: Constants.Server.Transport.typeName ), .init( @@ -94,7 +93,7 @@ struct ServerFileTranslator: FileTranslator { ), .init( label: "middlewares", - type: "[any \(Constants.Server.Middleware.typeName)]", + type: "[\(Constants.Server.Middleware.typeName)]", defaultValue: .literal(.array([])) ), ], From 60ec9408dcaacf2566a8cf0e3e8da698fd5354df Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Tue, 11 Jul 2023 13:16:36 +0330 Subject: [PATCH 3/4] Use `any` for all existential types. --- Package.swift | 22 ++++++++++++++----- .../GeneratorPipeline.swift | 10 ++++----- .../Parser/ParserProtocol.swift | 2 +- .../Parser/YamsParser.swift | 4 ++-- .../Renderer/RendererProtocol.swift | 2 +- .../Renderer/TextBasedRenderer.swift | 2 +- .../ClientTranslator/ClientTranslator.swift | 2 +- .../Translator/FileTranslator.swift | 2 +- .../Translator/MultiplexTranslator.swift | 4 ++-- .../ServerTranslator/ServerTranslator.swift | 2 +- .../Translator/TranslatorProtocol.swift | 2 +- .../TypesTranslator/TypesFileTranslator.swift | 2 +- .../GenerateOptions+runGenerator.swift | 2 +- .../runGenerator.swift | 4 ++-- .../TestUtilities.swift | 6 ++--- .../Test_isSchemaSupported.swift | 2 +- .../ReferenceTest.swift | 6 ++--- 17 files changed, 44 insertions(+), 32 deletions(-) diff --git a/Package.swift b/Package.swift index bebbf51a..18be1acd 100644 --- a/Package.swift +++ b/Package.swift @@ -14,6 +14,13 @@ //===----------------------------------------------------------------------===// import PackageDescription +// General Swift-settings for all targets. +let swiftSettings: [SwiftSetting] = [ + // https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md + // Require `any` for existential types. + .enableUpcomingFeature("ExistentialAny"), +] + let package = Package( name: "swift-openapi-generator", platforms: [ @@ -83,7 +90,8 @@ let package = Package( .product(name: "SwiftSyntaxBuilder", package: "swift-syntax"), .product(name: "SwiftFormat", package: "swift-format"), .product(name: "SwiftFormatConfiguration", package: "swift-format"), - ] + ], + swiftSettings: swiftSettings ), // Generator Core Tests @@ -91,7 +99,8 @@ let package = Package( name: "OpenAPIGeneratorCoreTests", dependencies: [ "_OpenAPIGeneratorCore", - ] + ], + swiftSettings: swiftSettings ), // GeneratorReferenceTests @@ -104,7 +113,8 @@ let package = Package( ], resources: [ .copy("Resources"), - ] + ], + swiftSettings: swiftSettings ), // PetstoreConsumerTests @@ -114,7 +124,8 @@ let package = Package( name: "PetstoreConsumerTests", dependencies: [ .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - ] + ], + swiftSettings: swiftSettings ), // Generator CLI @@ -123,7 +134,8 @@ let package = Package( dependencies: [ "_OpenAPIGeneratorCore", .product(name: "ArgumentParser", package: "swift-argument-parser"), - ] + ], + swiftSettings: swiftSettings ), // Build Plugin diff --git a/Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift b/Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift index 108b1b7a..a9caec5e 100644 --- a/Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift +++ b/Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift @@ -89,7 +89,7 @@ struct GeneratorPipeline { public func runGenerator( input: InMemoryInputFile, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> InMemoryOutputFile { try makeGeneratorPipeline(config: config, diagnostics: diagnostics).run(input) } @@ -105,12 +105,12 @@ public func runGenerator( /// - Returns: A configured generator pipeline that can be executed with /// ``GeneratorPipeline/run(_:)``. func makeGeneratorPipeline( - parser: ParserProtocol = YamsParser(), - translator: TranslatorProtocol = MultiplexTranslator(), - renderer: RendererProtocol = TextBasedRenderer(), + parser: any ParserProtocol = YamsParser(), + translator: any TranslatorProtocol = MultiplexTranslator(), + renderer: any RendererProtocol = TextBasedRenderer(), formatter: @escaping (InMemoryOutputFile) throws -> InMemoryOutputFile = { try $0.swiftFormatted }, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) -> GeneratorPipeline { return .init( parseOpenAPIFileStage: .init( diff --git a/Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift b/Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift index 45dff913..30df1df8 100644 --- a/Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift +++ b/Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift @@ -27,6 +27,6 @@ protocol ParserProtocol { func parseOpenAPI( _ input: InMemoryInputFile, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> ParsedOpenAPIRepresentation } diff --git a/Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift b/Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift index b7ccc238..6398d500 100644 --- a/Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift +++ b/Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift @@ -21,7 +21,7 @@ struct YamsParser: ParserProtocol { func parseOpenAPI( _ input: InMemoryInputFile, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> ParsedOpenAPIRepresentation { let decoder = YAMLDecoder() let openapiData = input.contents @@ -86,7 +86,7 @@ struct YamsParser: ParserProtocol { location: .init(filePath: input.absolutePath.path, lineNumber: yamlMark.line - 1) ) } - } else if let openAPIError = context.underlyingError as? OpenAPIError { + } else if let openAPIError = context.underlyingError as? (any OpenAPIError) { throw Diagnostic.error( message: openAPIError.localizedDescription, location: .init(filePath: input.absolutePath.path) diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/RendererProtocol.swift b/Sources/_OpenAPIGeneratorCore/Renderer/RendererProtocol.swift index 54651a46..c76842f9 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/RendererProtocol.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/RendererProtocol.swift @@ -27,6 +27,6 @@ protocol RendererProtocol { func render( structured code: StructuredSwiftRepresentation, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> InMemoryOutputFile } diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 4985ebb9..e0da301c 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -20,7 +20,7 @@ struct TextBasedRenderer: RendererProtocol { func render( structured: StructuredSwiftRepresentation, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> InMemoryOutputFile { let namedFile = structured.file return InMemoryOutputFile( diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift index 1f7ecf8f..c02eac5d 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift @@ -25,7 +25,7 @@ import OpenAPIKit30 struct ClientFileTranslator: FileTranslator { var config: Config - var diagnostics: DiagnosticCollector + var diagnostics: any DiagnosticCollector var components: OpenAPI.Components func translateFile( diff --git a/Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift index e6843e41..c81a71a9 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift @@ -29,7 +29,7 @@ protocol FileTranslator { /// The collector receives diagnostics from the translator, which should /// be surfaced to the user in some way. - var diagnostics: DiagnosticCollector { get } + var diagnostics: any DiagnosticCollector { get } /// The components section of the OpenAPI document is required by the /// translator logic to follow JSON references to schemas, parameters, diff --git a/Sources/_OpenAPIGeneratorCore/Translator/MultiplexTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/MultiplexTranslator.swift index f1f0e16e..a617e2c4 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/MultiplexTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/MultiplexTranslator.swift @@ -19,9 +19,9 @@ struct MultiplexTranslator: TranslatorProtocol { func translate( parsedOpenAPI: ParsedOpenAPIRepresentation, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> StructuredSwiftRepresentation { - let translator: FileTranslator + let translator: any FileTranslator switch config.mode { case .types: translator = TypesFileTranslator( diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift index f1360371..9a87ea3a 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift @@ -23,7 +23,7 @@ import OpenAPIKit30 struct ServerFileTranslator: FileTranslator { var config: Config - var diagnostics: DiagnosticCollector + var diagnostics: any DiagnosticCollector var components: OpenAPI.Components func translateFile( diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TranslatorProtocol.swift b/Sources/_OpenAPIGeneratorCore/Translator/TranslatorProtocol.swift index aab38ba3..d5b8ad17 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TranslatorProtocol.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TranslatorProtocol.swift @@ -28,6 +28,6 @@ protocol TranslatorProtocol { func translate( parsedOpenAPI: ParsedOpenAPIRepresentation, config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws -> StructuredSwiftRepresentation } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift index 703115b1..68bd32d4 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift @@ -25,7 +25,7 @@ import OpenAPIKit30 struct TypesFileTranslator: FileTranslator { var config: Config - var diagnostics: DiagnosticCollector + var diagnostics: any DiagnosticCollector var components: OpenAPI.Components func translateFile( diff --git a/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift b/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift index 02627486..01314929 100644 --- a/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift +++ b/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift @@ -40,7 +40,7 @@ extension _GenerateOptions { additionalImports: resolvedAdditionalImports ) } - let diagnostics: DiagnosticCollector + let diagnostics: any DiagnosticCollector let finalizeDiagnostics: () throws -> Void if let diagnosticsOutputPath { let _diagnostics = _YamlFileDiagnosticsCollector(url: diagnosticsOutputPath) diff --git a/Sources/swift-openapi-generator/runGenerator.swift b/Sources/swift-openapi-generator/runGenerator.swift index 1300f04c..7fc2f56a 100644 --- a/Sources/swift-openapi-generator/runGenerator.swift +++ b/Sources/swift-openapi-generator/runGenerator.swift @@ -31,7 +31,7 @@ extension _Tool { configs: [Config], isPluginInvocation: Bool, outputDirectory: URL, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws { let docData: Data do { @@ -73,7 +73,7 @@ extension _Tool { docData: Data, config: Config, outputFilePath: URL, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) throws { let didChange = try replaceFileContents(at: outputFilePath) { let output = try _OpenAPIGeneratorCore.runGenerator( diff --git a/Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift b/Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift index bc4b5076..a9af2b79 100644 --- a/Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift +++ b/Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift @@ -25,8 +25,8 @@ class Test_Core: XCTestCase { func makeTranslator( components: OpenAPI.Components = .noComponents, - diagnostics: DiagnosticCollector = PrintingDiagnosticCollector() - ) -> FileTranslator { + diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector() + ) -> any FileTranslator { makeTypesTranslator( components: components, diagnostics: diagnostics @@ -35,7 +35,7 @@ class Test_Core: XCTestCase { func makeTypesTranslator( components: OpenAPI.Components = .noComponents, - diagnostics: DiagnosticCollector = PrintingDiagnosticCollector() + diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector() ) -> TypesFileTranslator { TypesFileTranslator( config: .init(mode: .types), diff --git a/Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_isSchemaSupported.swift b/Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_isSchemaSupported.swift index 10c77b4c..e4bdac4d 100644 --- a/Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_isSchemaSupported.swift +++ b/Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_isSchemaSupported.swift @@ -17,7 +17,7 @@ import OpenAPIKit30 class Test_isSchemaSupported: XCTestCase { - var translator: FileTranslator { + var translator: any FileTranslator { TypesFileTranslator( config: .init(mode: .types), diagnostics: PrintingDiagnosticCollector(), diff --git a/Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift b/Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift index 9927dd08..d68c13c0 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift @@ -82,7 +82,7 @@ class ReferenceTests: XCTestCase { ) // Run the requested generator invocation - let diagnostics: DiagnosticCollector = strictDiagnosticsCollector + let diagnostics: any DiagnosticCollector = strictDiagnosticsCollector let generatorPipeline = self.makeGeneratorPipeline( config: referenceTest.asConfig, diagnostics: diagnostics @@ -160,13 +160,13 @@ struct StrictDiagnosticsCollector: DiagnosticCollector { extension ReferenceTests { - var strictDiagnosticsCollector: DiagnosticCollector { + var strictDiagnosticsCollector: any DiagnosticCollector { StrictDiagnosticsCollector(test: self) } private func makeGeneratorPipeline( config: Config, - diagnostics: DiagnosticCollector + diagnostics: any DiagnosticCollector ) -> GeneratorPipeline { let parser = YamsParser() From 11bb73d54c6bfe97de28460983130cd5b6c4074c Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Tue, 11 Jul 2023 14:22:47 +0330 Subject: [PATCH 4/4] Fix for Soundness --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index f21e7185..808b7d15 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ import PackageDescription let swiftSettings: [SwiftSetting] = [ // https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md // Require `any` for existential types. - .enableUpcomingFeature("ExistentialAny"), + .enableUpcomingFeature("ExistentialAny") ] let package = Package( @@ -124,7 +124,7 @@ let package = Package( .testTarget( name: "PetstoreConsumerTests", dependencies: [ - .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime") ], swiftSettings: swiftSettings ),