Skip to content

Commit 924362c

Browse files
Adopt approach that does not require feature flags
1 parent ad29a41 commit 924362c

File tree

7 files changed

+296
-389
lines changed

7 files changed

+296
-389
lines changed

Sources/_OpenAPIGeneratorCore/FeatureFlags.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
public enum FeatureFlag: String, Hashable, Codable, CaseIterable, Sendable {
2929
// needs to be here for the enum to compile
3030
case empty
31-
32-
/// When enabled any server URL templating variables that have a defined
33-
/// enum field will be generated as an enum instead of raw Strings.
34-
case serverVariablesAsEnums
3531
}
3632

3733
/// A set of enabled feature flags.

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,8 +1629,8 @@ extension KeywordKind {
16291629

16301630
extension Declaration {
16311631
/// Returns a new deprecated variant of the declaration if `shouldDeprecate` is true.
1632-
func deprecate(if shouldDeprecate: Bool) -> Self {
1633-
if shouldDeprecate { return .deprecated(.init(), self) }
1632+
func deprecate(if shouldDeprecate: Bool, description: @autoclosure () -> DeprecationDescription = .init()) -> Self {
1633+
if shouldDeprecate { return .deprecated(description(), self) }
16341634
return self
16351635
}
16361636

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,12 @@ enum Constants {
5656

5757
/// The prefix of each generated method name.
5858
static let propertyPrefix: String = "server"
59-
60-
/// The name of the server variables namespace.
61-
static let variablesNamespace: String = "Variables"
59+
60+
/// The name of each generated static function.
61+
static let urlStaticFunc: String = "url"
6262

6363
/// The prefix of the namespace that contains server specific variables.
64-
static let serverVariablesNamespacePrefix: String = "Server"
65-
66-
/// The name of the generated default computed property.
67-
static let defaultPropertyName: String = "default"
64+
static let serverNamespacePrefix: String = "Server"
6865
}
6966

7067
/// Constants related to the configuration type, which is used by both

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift

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

1616
extension TypesFileTranslator {
17-
/// Returns a declaration of a server URL static method defined in
18-
/// the OpenAPI document. Also appends server variables enums
19-
/// into the variables namespace parameter, if any enums were
20-
/// generated.
21-
/// - Parameters:
22-
/// - index: The index of the server in the list of servers defined
23-
/// in the OpenAPI document.
24-
/// - server: The server URL information.
25-
/// - variablesNamespace: The enum namespace which is to contain
26-
/// any variable enum definitions that may be required.
27-
/// - Returns: A static method declaration, and a name for the variable to
28-
/// declare the method under.
29-
func translateServer(index: Int, server: OpenAPI.Server, variablesNamespace: inout EnumDescription) -> Declaration {
30-
let serverVariables = translateServerVariableNamespace(index: index, server: server, variablesNamespaceName: variablesNamespace.name)
31-
32-
if let serverVariablesNamespace = serverVariables.decl {
33-
variablesNamespace.members.append(serverVariablesNamespace)
34-
}
35-
36-
let parameters = serverVariables.variables.map(\.parameter)
37-
let variableInitializers = serverVariables.variables.map(\.initializer)
38-
let functionComments = serverVariables.variables.map(\.functionComment)
39-
40-
let methodName = "\(Constants.ServerURL.propertyPrefix)\(index + 1)"
41-
42-
let methodDecl = Declaration.commentable(
43-
.functionComment(abstract: server.description, parameters: functionComments),
17+
func translateServerStaticFunction(
18+
isDeprecated: Bool,
19+
abstract: String?,
20+
name: String,
21+
url: String,
22+
variableGenerators variables: [any ServerVariableGenerator]
23+
) -> Declaration {
24+
return .commentable(
25+
.functionComment(abstract: abstract, parameters: variables.map(\.functionComment)),
4426
.function(
4527
accessModifier: config.access,
46-
kind: .function(name: methodName, isStatic: true),
47-
parameters: parameters,
28+
kind: .function(name: name, isStatic: true),
29+
parameters: variables.map(\.parameter),
4830
keywords: [.throws],
4931
returnType: .identifierType(TypeName.url),
5032
body: [
@@ -54,15 +36,70 @@ extension TypesFileTranslator {
5436
.call([
5537
.init(
5638
label: "validatingOpenAPIServerURL",
57-
expression: .literal(.string(server.urlTemplate.absoluteString))
58-
), .init(label: "variables", expression: .literal(.array(variableInitializers))),
39+
expression: .literal(.string(url))
40+
),
41+
.init(
42+
label: "variables",
43+
expression: .literal(.array(variables.map(\.initializer)))
44+
)
5945
])
6046
)
6147
)
6248
]
49+
).deprecate(
50+
if: isDeprecated,
51+
description: .init(message: "Migrate to the new type-safe API for server URLs.")
52+
)
53+
)
54+
}
55+
56+
/// Returns a declaration of a server URL static function defined in
57+
/// the OpenAPI document. The function is marked as deprecated
58+
/// with a message informing the adopter to use the new type-safe
59+
/// API.
60+
/// - Parameters:
61+
/// - index: The index of the server in the list of servers defined
62+
/// in the OpenAPI document.
63+
/// - server: The server URL information.
64+
/// - Returns: A static function declaration.
65+
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server) -> Declaration {
66+
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)
72+
}
73+
74+
/// Returns a namespace (enum) declaration for a server defined in
75+
/// the OpenAPI document. Within the namespace are enums to
76+
/// represent any variables that also have enum values defined in the
77+
/// OpenAPI document, and a single static function named 'url' which
78+
/// at runtime returns the resolved server URL.
79+
///
80+
/// The server's namespace is named to identify the human-friendly
81+
/// index of the enum (e.g. Server1) and is present to ensure each
82+
/// server definition's variables do not conflict with one another.
83+
/// - Parameters:
84+
/// - index: The index of the server in the list of servers defined
85+
/// in the OpenAPI document.
86+
/// - server: The server URL information.
87+
/// - Returns: A static function declaration.
88+
func translateServer(index: Int, server: OpenAPI.Server) -> Declaration {
89+
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
92+
name: Constants.ServerURL.urlStaticFunc,
93+
url: server.urlTemplate.absoluteString,
94+
variableGenerators: serverVariables)
95+
return .commentable(
96+
server.description.map(Comment.doc(_:)),
97+
.enum(
98+
accessModifier: config.access,
99+
name: "\(Constants.ServerURL.serverNamespacePrefix)\(index + 1)",
100+
members: serverVariables.compactMap(\.declaration) + CollectionOfOne(methodDecl)
63101
)
64102
)
65-
return methodDecl
66103
}
67104

68105
/// Returns a declaration of a namespace (enum) called "Servers" that
@@ -71,23 +108,13 @@ extension TypesFileTranslator {
71108
/// - Parameter servers: The servers to include in the extension.
72109
/// - Returns: A declaration of an enum namespace of the server URLs type.
73110
func translateServers(_ servers: [OpenAPI.Server]) -> Declaration {
74-
var variablesNamespace = EnumDescription(
75-
accessModifier: config.access,
76-
name: Constants.ServerURL.variablesNamespace
77-
)
78-
79111
var serverDecls: [Declaration] = []
80112

81-
for (index, decl) in servers.enumerated() {
82-
let serverDecl = translateServer(index: index, server: decl, variablesNamespace: &variablesNamespace)
83-
serverDecls.append(serverDecl)
84-
}
85-
86-
if !variablesNamespace.members.isEmpty {
87-
serverDecls.insert(.commentable(
88-
.doc("Server URL variables defined in the OpenAPI document."),
89-
.enum(variablesNamespace)
90-
), at: 0)
113+
for (index, server) in servers.enumerated() {
114+
serverDecls.append(contentsOf: [
115+
translateServer(index: index, server: server),
116+
translateServerAsDeprecated(index: index, server: server)
117+
])
91118
}
92119

93120
return .commentable(

0 commit comments

Comments
 (0)