Skip to content

Commit 4d30ace

Browse files
Address review feedback
1 parent 8d56c51 commit 4d30ace

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,12 @@ extension KeywordKind {
16281628
}
16291629

16301630
extension Declaration {
1631+
/// Returns a new deprecated variant of the declaration if `shouldDeprecate` is true.
1632+
func deprecate(if description: DeprecationDescription?) -> Self {
1633+
if let description { return .deprecated(description, self) }
1634+
return self
1635+
}
1636+
16311637
/// Returns a new deprecated variant of the declaration if `shouldDeprecate` is true.
16321638
func deprecate(if shouldDeprecate: Bool, description: @autoclosure () -> DeprecationDescription = .init()) -> Self {
16331639
if shouldDeprecate { return .deprecated(description(), self) }

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import OpenAPIKit
1515

1616
extension TypesFileTranslator {
17-
func translateServerStaticFunction(
18-
isDeprecated: Bool,
17+
private func translateServerStaticFunction(
18+
deprecated: DeprecationDescription?,
1919
abstract: String?,
2020
name: String,
2121
url: String,
@@ -46,10 +46,7 @@ extension TypesFileTranslator {
4646
)
4747
)
4848
]
49-
).deprecate(
50-
if: isDeprecated,
51-
description: .init(message: "Migrate to the new type-safe API for server URLs.")
52-
)
49+
).deprecate(if: deprecated)
5350
)
5451
}
5552

@@ -62,13 +59,15 @@ extension TypesFileTranslator {
6259
/// in the OpenAPI document.
6360
/// - server: The server URL information.
6461
/// - Returns: A static function declaration.
65-
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server) -> Declaration {
62+
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server, renamedTo pathToReplacementSymbol: String) -> Declaration {
6663
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: false)
67-
return translateServerStaticFunction(isDeprecated: true,
68-
abstract: server.description,
69-
name: "\(Constants.ServerURL.propertyPrefix)\(index + 1)",
70-
url: server.urlTemplate.absoluteString,
71-
variableGenerators: serverVariables)
64+
return translateServerStaticFunction(
65+
deprecated: DeprecationDescription(renamed: pathToReplacementSymbol),
66+
abstract: server.description,
67+
name: "\(Constants.ServerURL.propertyPrefix)\(index + 1)",
68+
url: server.urlTemplate.absoluteString,
69+
variableGenerators: serverVariables
70+
)
7271
}
7372

7473
/// Returns a namespace (enum) declaration for a server defined in
@@ -85,21 +84,25 @@ extension TypesFileTranslator {
8584
/// in the OpenAPI document.
8685
/// - server: The server URL information.
8786
/// - Returns: A static function declaration.
88-
func translateServer(index: Int, server: OpenAPI.Server) -> Declaration {
87+
func translateServer(index: Int, server: OpenAPI.Server) -> (pathToStaticFunction: String, decl: Declaration) {
8988
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: true)
90-
let methodDecl = translateServerStaticFunction(isDeprecated: false,
91-
abstract: nil, // server.description is on the namespace now
89+
let methodDecl = translateServerStaticFunction(deprecated: nil,
90+
abstract: server.description,
9291
name: Constants.ServerURL.urlStaticFunc,
9392
url: server.urlTemplate.absoluteString,
9493
variableGenerators: serverVariables)
95-
return .commentable(
94+
95+
let namespaceName = "\(Constants.ServerURL.serverNamespacePrefix)\(index + 1)"
96+
let typeName = TypeName(swiftKeyPath: [Constants.ServerURL.namespace, namespaceName, Constants.ServerURL.urlStaticFunc])
97+
let decl = Declaration.commentable(
9698
server.description.map(Comment.doc(_:)),
9799
.enum(
98100
accessModifier: config.access,
99-
name: "\(Constants.ServerURL.serverNamespacePrefix)\(index + 1)",
101+
name: namespaceName,
100102
members: serverVariables.compactMap(\.declaration) + CollectionOfOne(methodDecl)
101103
)
102104
)
105+
return (pathToStaticFunction: typeName.fullyQualifiedSwiftName, decl: decl)
103106
}
104107

105108
/// Returns a declaration of a namespace (enum) called "Servers" that
@@ -111,9 +114,10 @@ extension TypesFileTranslator {
111114
var serverDecls: [Declaration] = []
112115

113116
for (index, server) in servers.enumerated() {
117+
let translatedServer = translateServer(index: index, server: server)
114118
serverDecls.append(contentsOf: [
115-
translateServer(index: index, server: server),
116-
translateServerAsDeprecated(index: index, server: server)
119+
translatedServer.decl,
120+
translateServerAsDeprecated(index: index, server: server, renamedTo: translatedServer.pathToStaticFunction)
117121
])
118122
}
119123

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServersVariables.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,10 @@ extension TypesFileTranslator {
166166
/// Returns the description of the parameter that will be used to define the variable
167167
/// in the static method for a given server.
168168
var parameter: ParameterDescription {
169-
let safeDefault = context.asSwiftSafeName(variable.default)
170-
let memberPath: [String] = [
171-
enumName
172-
]
173169
return .init(
174170
label: swiftSafeKey,
175-
type: .member(memberPath),
176-
defaultValue: .identifierType(.member(memberPath + CollectionOfOne(safeDefault)))
171+
type: .member([enumName]),
172+
defaultValue: .memberAccess(.dot(context.asSwiftSafeName(variable.default)))
177173
)
178174
}
179175

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ extension APIProtocol {
155155
public enum Servers {
156156
/// Example Petstore implementation service
157157
public enum Server1 {
158+
/// Example Petstore implementation service
158159
public static func url() throws -> Foundation.URL {
159160
try Foundation.URL(
160161
validatingOpenAPIServerURL: "https://example.com/api",
@@ -163,7 +164,7 @@ public enum Servers {
163164
}
164165
}
165166
/// Example Petstore implementation service
166-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
167+
@available(*, deprecated, renamed: "Servers.Server1.url")
167168
public static func server1() throws -> Foundation.URL {
168169
try Foundation.URL(
169170
validatingOpenAPIServerURL: "https://example.com/api",
@@ -178,7 +179,7 @@ public enum Servers {
178179
)
179180
}
180181
}
181-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
182+
@available(*, deprecated, renamed: "Servers.Server2.url")
182183
public static func server2() throws -> Foundation.URL {
183184
try Foundation.URL(
184185
validatingOpenAPIServerURL: "/api",
@@ -192,6 +193,7 @@ public enum Servers {
192193
case _443 = "443"
193194
case _8443 = "8443"
194195
}
196+
/// A custom domain.
195197
///
196198
/// - Parameters:
197199
/// - _protocol:
@@ -201,7 +203,7 @@ public enum Servers {
201203
public static func url(
202204
_protocol: Swift.String = "https",
203205
subdomain: Swift.String = "test",
204-
port: Port = Port._443,
206+
port: Port = ._443,
205207
basePath: Swift.String = "v1"
206208
) throws -> Foundation.URL {
207209
try Foundation.URL(
@@ -234,7 +236,7 @@ public enum Servers {
234236
/// - subdomain: A subdomain name.
235237
/// - port:
236238
/// - basePath: The base API path.
237-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
239+
@available(*, deprecated, renamed: "Servers.Server3.url")
238240
public static func server3(
239241
_protocol: Swift.String = "https",
240242
subdomain: Swift.String = "test",

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5196,7 +5196,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
51965196
)
51975197
}
51985198
}
5199-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5199+
@available(*, deprecated, renamed: "Servers.Server1.url")
52005200
public static func server1() throws -> Foundation.URL {
52015201
try Foundation.URL(
52025202
validatingOpenAPIServerURL: "https://example.com/api",
@@ -5233,7 +5233,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
52335233
)
52345234
}
52355235
}
5236-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5236+
@available(*, deprecated, renamed: "Servers.Server1.url")
52375237
public static func server1(_protocol: Swift.String = "https") throws -> Foundation.URL {
52385238
try Foundation.URL(
52395239
validatingOpenAPIServerURL: "{protocol}://example.com/api",
@@ -5272,7 +5272,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
52725272
case sandbox
52735273
}
52745274
public static func url(
5275-
environment: Environment = Environment.production,
5275+
environment: Environment = .production,
52765276
version: Swift.String = "v1"
52775277
) throws -> Foundation.URL {
52785278
try Foundation.URL(
@@ -5290,7 +5290,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
52905290
)
52915291
}
52925292
}
5293-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5293+
@available(*, deprecated, renamed: "Servers.Server1.url")
52945294
public static func server1(
52955295
environment: Swift.String = "production",
52965296
version: Swift.String = "v1"
@@ -5354,7 +5354,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
53545354
case sandbox
53555355
}
53565356
public static func url(
5357-
environment: Environment = Environment.production,
5357+
environment: Environment = .production,
53585358
version: Swift.String = "v1"
53595359
) throws -> Foundation.URL {
53605360
try Foundation.URL(
@@ -5372,7 +5372,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
53725372
)
53735373
}
53745374
}
5375-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5375+
@available(*, deprecated, renamed: "Servers.Server1.url")
53765376
public static func server1(
53775377
environment: Swift.String = "production",
53785378
version: Swift.String = "v1"
@@ -5400,7 +5400,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
54005400
case sandbox
54015401
case develop
54025402
}
5403-
public static func url(environment: Environment = Environment.develop) throws -> Foundation.URL {
5403+
public static func url(environment: Environment = .develop) throws -> Foundation.URL {
54045404
try Foundation.URL(
54055405
validatingOpenAPIServerURL: "https://{environment}.api.example.com/",
54065406
variables: [
@@ -5412,7 +5412,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
54125412
)
54135413
}
54145414
}
5415-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5415+
@available(*, deprecated, renamed: "Servers.Server2.url")
54165416
public static func server2(environment: Swift.String = "develop") throws -> Foundation.URL {
54175417
try Foundation.URL(
54185418
validatingOpenAPIServerURL: "https://{environment}.api.example.com/",
@@ -5441,7 +5441,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
54415441
)
54425442
}
54435443
}
5444-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5444+
@available(*, deprecated, renamed: "Servers.Server3.url")
54455445
public static func server3(version: Swift.String = "v1") throws -> Foundation.URL {
54465446
try Foundation.URL(
54475447
validatingOpenAPIServerURL: "https://example.com/api/{version}",
@@ -5461,7 +5461,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
54615461
)
54625462
}
54635463
}
5464-
@available(*, deprecated, message: "Migrate to the new type-safe API for server URLs.")
5464+
@available(*, deprecated, renamed: "Servers.Server4.url")
54655465
public static func server4() throws -> Foundation.URL {
54665466
try Foundation.URL(
54675467
validatingOpenAPIServerURL: "https://api.example.com/",

0 commit comments

Comments
 (0)