Skip to content

Commit d875fe4

Browse files
Add missing comments to the new implementations
1 parent ef855f6 commit d875fe4

File tree

3 files changed

+96
-40
lines changed

3 files changed

+96
-40
lines changed

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ extension KeywordKind {
16281628
}
16291629

16301630
extension Declaration {
1631-
/// Returns a new deprecated variant of the declaration if `shouldDeprecate` is true.
1631+
/// Returns a new deprecated variant of the declaration if the provided `description` is not `nil`.
16321632
func deprecate(if description: DeprecationDescription?) -> Self {
16331633
if let description { return .deprecated(description, self) }
16341634
return self

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift

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

1616
extension TypesFileTranslator {
17+
/// Returns a declaration of a server URL static function defined in
18+
/// the OpenAPI document using the supplied name identifier and
19+
/// variable generators.
20+
///
21+
/// If the `deprecated` parameter is supplied the static function
22+
/// will be generated with a name that matches the previous, now
23+
/// deprecated API.
24+
///
25+
/// - Important: The variable generators provided should all
26+
/// be ``RawStringTranslatedServerVariable`` to ensure
27+
/// the generated function matches the previous implementation, this
28+
/// is **not** asserted by this translate function.
29+
///
30+
/// If the `deprecated` parameter is `nil` then the function will
31+
/// be generated with the identifier `url` and must be a member
32+
/// of a namespace to avoid conflicts with other server URL static
33+
/// functions.
34+
///
35+
/// - Parameters:
36+
/// - index: The index of the server in the list of servers defined
37+
/// in the OpenAPI document.
38+
/// - server: The server URL information.
39+
/// - deprecated: A deprecation `@available` annotation to attach
40+
/// to this declaration, or `nil` if the declaration should not be deprecated.
41+
/// - variables: The generators for variables the server has defined.
42+
/// - Returns: A static method declaration, and a name for the variable to
43+
/// declare the method under.
1744
private func translateServerStaticFunction(
45+
index: Int,
46+
server: OpenAPI.Server,
1847
deprecated: DeprecationDescription?,
19-
abstract: String?,
20-
name: String,
21-
url: String,
2248
variableGenerators variables: [any ServerVariableGenerator]
2349
) -> Declaration {
50+
let name = deprecated == nil ? Constants.ServerURL.urlStaticFunc : "\(Constants.ServerURL.propertyPrefix)\(index + 1)"
2451
return .commentable(
25-
.functionComment(abstract: abstract, parameters: variables.map(\.functionComment)),
52+
.functionComment(abstract: server.description, parameters: variables.map(\.functionComment)),
2653
.function(
2754
accessModifier: config.access,
2855
kind: .function(name: name, isStatic: true),
@@ -36,7 +63,7 @@ extension TypesFileTranslator {
3663
.call([
3764
.init(
3865
label: "validatingOpenAPIServerURL",
39-
expression: .literal(.string(url))
66+
expression: .literal(.string(server.urlTemplate.absoluteString))
4067
),
4168
.init(
4269
label: "variables",
@@ -61,13 +88,10 @@ extension TypesFileTranslator {
6188
/// - Returns: A static function declaration.
6289
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server, renamedTo pathToReplacementSymbol: String) -> Declaration {
6390
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: false)
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-
)
91+
return translateServerStaticFunction(index: index,
92+
server: server,
93+
deprecated: DeprecationDescription(renamed: pathToReplacementSymbol),
94+
variableGenerators: serverVariables)
7195
}
7296

7397
/// Returns a namespace (enum) declaration for a server defined in
@@ -86,12 +110,11 @@ extension TypesFileTranslator {
86110
/// - Returns: A static function declaration.
87111
func translateServer(index: Int, server: OpenAPI.Server) -> (pathToStaticFunction: String, decl: Declaration) {
88112
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: true)
89-
let methodDecl = translateServerStaticFunction(deprecated: nil,
90-
abstract: server.description,
91-
name: Constants.ServerURL.urlStaticFunc,
92-
url: server.urlTemplate.absoluteString,
113+
let methodDecl = translateServerStaticFunction(index: index,
114+
server: server,
115+
deprecated: nil,
93116
variableGenerators: serverVariables)
94-
117+
95118
let namespaceName = "\(Constants.ServerURL.serverNamespacePrefix)\(index + 1)"
96119
let typeName = TypeName(swiftKeyPath: [Constants.ServerURL.namespace, namespaceName, Constants.ServerURL.urlStaticFunc])
97120
let decl = Declaration.commentable(
@@ -112,15 +135,15 @@ extension TypesFileTranslator {
112135
/// - Returns: A declaration of an enum namespace of the server URLs type.
113136
func translateServers(_ servers: [OpenAPI.Server]) -> Declaration {
114137
var serverDecls: [Declaration] = []
115-
138+
116139
for (index, server) in servers.enumerated() {
117140
let translatedServer = translateServer(index: index, server: server)
118141
serverDecls.append(contentsOf: [
119142
translatedServer.decl,
120143
translateServerAsDeprecated(index: index, server: server, renamedTo: translatedServer.pathToStaticFunction)
121144
])
122145
}
123-
146+
124147
return .commentable(
125148
.doc("Server URLs defined in the OpenAPI document."),
126149
.enum(accessModifier: config.access, name: Constants.ServerURL.namespace, members: serverDecls)

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServersVariables.swift

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ extension TypesFileTranslator {
2424
/// - Returns: A declaration of the server variables namespace, or `nil` if no
2525
/// variables are declared.
2626
func translateServerVariables(index: Int, server: OpenAPI.Server, generateAsEnum: Bool) -> [any ServerVariableGenerator] {
27-
return server.variables.map { (key, variable) in
27+
return server.variables.map { key, variable in
2828
guard generateAsEnum, let enumValues = variable.enum else {
2929
return RawStringTranslatedServerVariable(
3030
key: key,
3131
variable: variable,
3232
context: context
3333
)
3434
}
35-
35+
3636
return GeneratedEnumTranslatedServerVariable(
3737
key: key,
3838
variable: variable,
@@ -47,9 +47,8 @@ extension TypesFileTranslator {
4747

4848
/// Represents a server variable and the function of generation that should be applied.
4949
protocol ServerVariableGenerator {
50-
/// Returns the declaration (enum) that should be added to the `Variables.Server#`
51-
/// namespace. If the server variable does not require any codegen then it should
52-
/// return `nil`.
50+
/// Returns the declaration (enum) that should be added to the server's namespace.
51+
/// If the server variable does not require any codegen then it should return `nil`.
5352
var declaration: Declaration? { get }
5453

5554
/// Returns the description of the parameter that will be used to define the variable
@@ -64,22 +63,39 @@ extension TypesFileTranslator {
6463
/// the server's static method.
6564
var functionComment: (name: String, comment: String?) { get }
6665
}
67-
66+
6867
/// Represents a variable that is required to be represented as a `Swift.String`.
6968
private struct RawStringTranslatedServerVariable: ServerVariableGenerator {
69+
/// The key of the variable defined in the Open API document.
7070
let key: String
71+
72+
/// The ``key`` after being santized for use as an identifier.
7173
let swiftSafeKey: String
74+
75+
/// The server variable information.
7276
let variable: OpenAPI.Server.Variable
7377

78+
/// Create a generator for an Open API "Server Variable Object" that is represented
79+
/// by a `Swift.String` in the generated output.
80+
///
81+
/// - Parameters:
82+
/// - key: The key of the variable defined in the Open API document.
83+
/// - variable: The server variable information.
84+
/// - context: The translator context the generator should use to create
85+
/// Swift safe identifiers.
7486
init(key: String, variable: OpenAPI.Server.Variable, context: TranslatorContext) {
7587
self.key = key
7688
swiftSafeKey = context.asSwiftSafeName(key)
7789
self.variable = variable
7890
}
7991

80-
/// A variable being represented by a `Swift.String` does not have a declaration that needs to
81-
/// be added to the `Variables.Server#` namespace.
82-
var declaration: Declaration? { nil }
92+
/// Returns the declaration (enum) that should be added to the server's namespace.
93+
/// If the server variable does not require any codegen then it should return `nil`.
94+
var declaration: Declaration? {
95+
// A variable being represented by a `Swift.String` does not have a declaration that needs to
96+
// be added to the server's namespace.
97+
return nil
98+
}
8399

84100
/// Returns the description of the parameter that will be used to define the variable
85101
/// in the static method for a given server.
@@ -114,32 +130,53 @@ extension TypesFileTranslator {
114130
}
115131
}
116132

117-
/// Represents a variable that will be generated as an enum and added to the `Variables.Server#`
118-
/// namespace. The enum will contain a `default` static case which returns the default defined in
119-
/// the OpenAPI Document.
133+
/// Represents an Open API "Server Variable Object" that will be generated as an enum and added
134+
/// to the server's namespace.
120135
private struct GeneratedEnumTranslatedServerVariable: ServerVariableGenerator {
136+
/// The key of the variable defined in the Open API document.
121137
let key: String
138+
139+
/// The ``key`` after being santized for use as an identifier.
122140
let swiftSafeKey: String
141+
142+
/// The ``key`` after being santized for use as the enum identifier.
123143
let enumName: String
144+
145+
/// The server variable information.
124146
let variable: OpenAPI.Server.Variable
147+
148+
/// The 'enum' values of the variable as defined in the Open API document.
125149
let enumValues: [String]
126150

151+
/// The access modifier to use for generated declarations.
127152
let accessModifier: AccessModifier
153+
154+
/// The translator context the generator should use to create Swift safe identifiers.
128155
let context: TranslatorContext
129156

157+
/// Create a generator for an Open API "Server Variable Object" that is represented
158+
/// by an enumeration in the generated output.
159+
///
160+
/// - Parameters:
161+
/// - key: The key of the variable defined in the Open API document.
162+
/// - variable: The server variable information.
163+
/// - enumValues: The 'enum' values of the variable as defined in the Open API document.
164+
/// - accessModifier: The access modifier to use for generated declarations.
165+
/// - context: The translator context the generator should use to create
166+
/// Swift safe identifiers.
130167
init(key: String, variable: OpenAPI.Server.Variable, enumValues: [String], accessModifier: AccessModifier, context: TranslatorContext) {
131168
self.key = key
132169
swiftSafeKey = context.asSwiftSafeName(key)
133170
enumName = context.asSwiftSafeName(key.localizedCapitalized)
134171
self.variable = variable
135172
self.enumValues = enumValues
136-
173+
137174
self.context = context
138175
self.accessModifier = accessModifier
139176
}
140177

141-
/// Returns the declaration (enum) that should be added to the `Variables.Server#`
142-
/// namespace.
178+
/// Returns the declaration (enum) that should be added to the server's namespace.
179+
/// If the server variable does not require any codegen then it should return `nil`.
143180
var declaration: Declaration? {
144181
let description: String = if let description = variable.description {
145182
description + "\n\n"
@@ -204,11 +241,7 @@ extension TypesFileTranslator {
204241
/// - Returns: A declaration of an enum case.
205242
private func translateVariableCase(_ name: String) -> Declaration {
206243
let caseName = context.asSwiftSafeName(name)
207-
if caseName == name {
208-
return .enumCase(name: caseName, kind: .nameOnly)
209-
} else {
210-
return .enumCase(name: caseName, kind: .nameWithRawValue(.string(name)))
211-
}
244+
return .enumCase(name: caseName, kind: caseName == name ? .nameOnly : .nameWithRawValue(.string(name)))
212245
}
213246
}
214247
}

0 commit comments

Comments
 (0)