Skip to content

Commit 7364ff4

Browse files
authored
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.
1 parent ad75749 commit 7364ff4

File tree

8 files changed

+25
-52
lines changed

8 files changed

+25
-52
lines changed

Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,30 @@ struct TextBasedRenderer: RendererProtocol {
7373

7474
/// Renders a single import statement.
7575
func renderImport(_ description: ImportDescription) -> String {
76-
func render(moduleName: String, moduleTypes: [String]? = nil, spi: String?, preconcurrency: Bool) -> String {
77-
let spiPrefix = spi.map { "@_spi(\($0)) " } ?? ""
76+
func render(preconcurrency: Bool) -> String {
77+
let spiPrefix = description.spi.map { "@_spi(\($0)) " } ?? ""
7878
let preconcurrencyPrefix = preconcurrency ? "@preconcurrency " : ""
7979
var types = [String]()
80-
if let moduleTypes, preconcurrency {
80+
if let moduleTypes = description.moduleTypes {
8181
types = moduleTypes.map {
8282
"\(preconcurrencyPrefix)\(spiPrefix)import \($0)"
8383
}
8484
return types.joinedLines()
8585
}
86-
return "\(preconcurrencyPrefix)\(spiPrefix)import \(moduleName)"
86+
return "\(preconcurrencyPrefix)\(spiPrefix)import \(description.moduleName)"
8787
}
8888

8989
switch description.preconcurrency {
9090
case .always:
91-
return render(moduleName: description.moduleName, spi: description.spi, preconcurrency: true)
91+
return render(preconcurrency: true)
9292
case .never:
93-
return render(moduleName: description.moduleName, spi: description.spi, preconcurrency: false)
93+
return render(preconcurrency: false)
9494
case .onOS(let operatingSystems):
9595
var lines = [String]()
9696
lines.append("#if \(operatingSystems.map { "os(\($0))" }.joined(separator: " || "))")
97-
lines.append(
98-
render(
99-
moduleName: description.moduleName,
100-
moduleTypes: description.moduleTypes,
101-
spi: description.spi,
102-
preconcurrency: true
103-
)
104-
)
97+
lines.append(render(preconcurrency: true))
10598
lines.append("#else")
106-
lines.append(render(moduleName: description.moduleName, spi: description.spi, preconcurrency: false))
99+
lines.append(render(preconcurrency: false))
107100
lines.append("#endif")
108101
return lines.joinedLines()
109102
}

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ struct ClientFileTranslator: FileTranslator {
3838

3939
let imports =
4040
Constants.File.imports
41-
+ Constants.Client.scopedImports
4241
+ config.additionalImports
4342
.map { ImportDescription(moduleName: $0) }
4443

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ enum Constants {
2525

2626
/// The descriptions of modules imported by every generated file.
2727
static let imports: [ImportDescription] = [
28-
ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated")
28+
ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated"),
29+
ImportDescription(
30+
moduleName: "Foundation",
31+
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data", "struct Foundation.Date"],
32+
preconcurrency: .onOS(["Linux"])
33+
),
2934
]
3035
}
3136

@@ -61,15 +66,6 @@ enum Constants {
6166
/// Constants related to the generated client type.
6267
enum Client {
6368

64-
/// An array of scoped imports specific to the `Client` namespace.
65-
static let scopedImports: [ImportDescription] = [
66-
ImportDescription(
67-
moduleName: "Foundation",
68-
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"],
69-
preconcurrency: .onOS(["Linux"])
70-
)
71-
]
72-
7369
/// The name of the client type.
7470
static let typeName: String = "Client"
7571

@@ -99,30 +95,9 @@ enum Constants {
9995
}
10096
}
10197

102-
enum Types {
103-
104-
/// An array of scoped imports specific to the `Types` namespace.
105-
static let scopedImports: [ImportDescription] = [
106-
ImportDescription(
107-
moduleName: "Foundation",
108-
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data", "struct Foundation.Date"],
109-
preconcurrency: .onOS(["Linux"])
110-
)
111-
]
112-
}
113-
11498
/// Constants related to the generated server types.
11599
enum Server {
116100

117-
/// An array of scoped imports specific to the `Server` namespace.
118-
static let scopedImports: [ImportDescription] = [
119-
ImportDescription(
120-
moduleName: "Foundation",
121-
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"],
122-
preconcurrency: .onOS(["Linux"])
123-
)
124-
]
125-
126101
/// Constants related to the universal server.
127102
enum Universal {
128103

Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ struct ServerFileTranslator: FileTranslator {
3636

3737
let imports =
3838
Constants.File.imports
39-
+ Constants.Server.scopedImports
4039
+ config.additionalImports
4140
.map { ImportDescription(moduleName: $0) }
4241

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ struct TypesFileTranslator: FileTranslator {
3838

3939
let imports =
4040
Constants.File.imports
41-
+ Constants.Types.scopedImports
4241
+ config.additionalImports
4342
.map { ImportDescription(moduleName: $0) }
4443

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#if os(Linux)
44
@preconcurrency import struct Foundation.URL
55
@preconcurrency import struct Foundation.Data
6+
@preconcurrency import struct Foundation.Date
67
#else
7-
import Foundation
8+
import struct Foundation.URL
9+
import struct Foundation.Data
10+
import struct Foundation.Date
811
#endif
912
/// Service for managing pet metadata.
1013
///

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#if os(Linux)
44
@preconcurrency import struct Foundation.URL
55
@preconcurrency import struct Foundation.Data
6+
@preconcurrency import struct Foundation.Date
67
#else
7-
import Foundation
8+
import struct Foundation.URL
9+
import struct Foundation.Data
10+
import struct Foundation.Date
811
#endif
912
extension APIProtocol {
1013
/// Registers each operation handler with the provided transport.

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
@preconcurrency import struct Foundation.Data
66
@preconcurrency import struct Foundation.Date
77
#else
8-
import Foundation
8+
import struct Foundation.URL
9+
import struct Foundation.Data
10+
import struct Foundation.Date
911
#endif
1012
/// A type that performs HTTP operations defined by the OpenAPI document.
1113
public protocol APIProtocol: Sendable {

0 commit comments

Comments
 (0)