From 7364ff4790df62d835770bfe9b357a839f540fc1 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 12 Sep 2023 14:24:15 +0200 Subject: [PATCH] Use per-type imports everywhere (#264) ### Motivation The PR #184 broke the build when Foundation.Date was used in specific locations on client/server. ### Modifications Fixed the bug, but also made things even more consistent by using per-type imports on all platforms. ### Result All the necessary Foundation imports are now present, plus code got simplified a bit. ### Test Plan Now that all platforms emit the same imports (except for the `@preconcurrency` attribute), it's easier to catch similar issues even during local dev. Verified on a sample proj this now works correctly. Updated reference tests. --- .../Renderer/TextBasedRenderer.swift | 23 ++++-------- .../ClientTranslator/ClientTranslator.swift | 1 - .../Translator/CommonTypes/Constants.swift | 37 +++---------------- .../ServerTranslator/ServerTranslator.swift | 1 - .../TypesTranslator/TypesFileTranslator.swift | 1 - .../ReferenceSources/Petstore/Client.swift | 5 ++- .../ReferenceSources/Petstore/Server.swift | 5 ++- .../ReferenceSources/Petstore/Types.swift | 4 +- 8 files changed, 25 insertions(+), 52 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift index 8e797a1d..b67f7953 100644 --- a/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift +++ b/Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift @@ -73,37 +73,30 @@ struct TextBasedRenderer: RendererProtocol { /// Renders a single import statement. func renderImport(_ description: ImportDescription) -> String { - func render(moduleName: String, moduleTypes: [String]? = nil, spi: String?, preconcurrency: Bool) -> String { - let spiPrefix = spi.map { "@_spi(\($0)) " } ?? "" + func render(preconcurrency: Bool) -> String { + let spiPrefix = description.spi.map { "@_spi(\($0)) " } ?? "" let preconcurrencyPrefix = preconcurrency ? "@preconcurrency " : "" var types = [String]() - if let moduleTypes, preconcurrency { + if let moduleTypes = description.moduleTypes { types = moduleTypes.map { "\(preconcurrencyPrefix)\(spiPrefix)import \($0)" } return types.joinedLines() } - return "\(preconcurrencyPrefix)\(spiPrefix)import \(moduleName)" + return "\(preconcurrencyPrefix)\(spiPrefix)import \(description.moduleName)" } switch description.preconcurrency { case .always: - return render(moduleName: description.moduleName, spi: description.spi, preconcurrency: true) + return render(preconcurrency: true) case .never: - return render(moduleName: description.moduleName, spi: description.spi, preconcurrency: false) + return render(preconcurrency: false) case .onOS(let operatingSystems): var lines = [String]() lines.append("#if \(operatingSystems.map { "os(\($0))" }.joined(separator: " || "))") - lines.append( - render( - moduleName: description.moduleName, - moduleTypes: description.moduleTypes, - spi: description.spi, - preconcurrency: true - ) - ) + lines.append(render(preconcurrency: true)) lines.append("#else") - lines.append(render(moduleName: description.moduleName, spi: description.spi, preconcurrency: false)) + lines.append(render(preconcurrency: false)) lines.append("#endif") return lines.joinedLines() } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift index d3733f53..393b0875 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift @@ -38,7 +38,6 @@ struct ClientFileTranslator: FileTranslator { let imports = Constants.File.imports - + Constants.Client.scopedImports + config.additionalImports .map { ImportDescription(moduleName: $0) } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift index 0289ffa7..191f94fe 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift @@ -25,7 +25,12 @@ enum Constants { /// The descriptions of modules imported by every generated file. static let imports: [ImportDescription] = [ - ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated") + ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated"), + ImportDescription( + moduleName: "Foundation", + moduleTypes: ["struct Foundation.URL", "struct Foundation.Data", "struct Foundation.Date"], + preconcurrency: .onOS(["Linux"]) + ), ] } @@ -61,15 +66,6 @@ enum Constants { /// Constants related to the generated client type. enum Client { - /// An array of scoped imports specific to the `Client` namespace. - static let scopedImports: [ImportDescription] = [ - ImportDescription( - moduleName: "Foundation", - moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"], - preconcurrency: .onOS(["Linux"]) - ) - ] - /// The name of the client type. static let typeName: String = "Client" @@ -99,30 +95,9 @@ enum Constants { } } - enum Types { - - /// An array of scoped imports specific to the `Types` namespace. - static let scopedImports: [ImportDescription] = [ - ImportDescription( - moduleName: "Foundation", - moduleTypes: ["struct Foundation.URL", "struct Foundation.Data", "struct Foundation.Date"], - preconcurrency: .onOS(["Linux"]) - ) - ] - } - /// Constants related to the generated server types. enum Server { - /// An array of scoped imports specific to the `Server` namespace. - static let scopedImports: [ImportDescription] = [ - ImportDescription( - moduleName: "Foundation", - moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"], - preconcurrency: .onOS(["Linux"]) - ) - ] - /// Constants related to the universal server. enum Universal { diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift index acb2b663..b183cffb 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift @@ -36,7 +36,6 @@ struct ServerFileTranslator: FileTranslator { let imports = Constants.File.imports - + Constants.Server.scopedImports + config.additionalImports .map { ImportDescription(moduleName: $0) } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift index 448c814f..485e1acc 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift @@ -38,7 +38,6 @@ struct TypesFileTranslator: FileTranslator { let imports = Constants.File.imports - + Constants.Types.scopedImports + config.additionalImports .map { ImportDescription(moduleName: $0) } diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift index 0ea916b7..df3788bf 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift @@ -3,8 +3,11 @@ #if os(Linux) @preconcurrency import struct Foundation.URL @preconcurrency import struct Foundation.Data +@preconcurrency import struct Foundation.Date #else -import Foundation +import struct Foundation.URL +import struct Foundation.Data +import struct Foundation.Date #endif /// Service for managing pet metadata. /// diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift index cfe22e62..409c2980 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift @@ -3,8 +3,11 @@ #if os(Linux) @preconcurrency import struct Foundation.URL @preconcurrency import struct Foundation.Data +@preconcurrency import struct Foundation.Date #else -import Foundation +import struct Foundation.URL +import struct Foundation.Data +import struct Foundation.Date #endif extension APIProtocol { /// Registers each operation handler with the provided transport. diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift index d78e5ba8..9f592249 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift @@ -5,7 +5,9 @@ @preconcurrency import struct Foundation.Data @preconcurrency import struct Foundation.Date #else -import Foundation +import struct Foundation.URL +import struct Foundation.Data +import struct Foundation.Date #endif /// A type that performs HTTP operations defined by the OpenAPI document. public protocol APIProtocol: Sendable {