Skip to content

Commit 0504c42

Browse files
Introduce snippet-based reference tests for server variable translation
1 parent 78bf584 commit 0504c42

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ extension TypesFileTranslator {
7878

7979
var serverDecls: [Declaration] = []
8080

81-
8281
for (index, decl) in servers.enumerated() {
8382
let serverDecl = translateServer(index: index, server: decl, variablesNamespace: &variablesNamespace)
8483
serverDecls.append(serverDecl)

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5181,6 +5181,193 @@ final class SnippetBasedReferenceTests: XCTestCase {
51815181
)
51825182
}
51835183

5184+
func testServerWithNoVariables() throws {
5185+
try self.assertServerTranslation(
5186+
"""
5187+
url: https://example.com/api
5188+
""",
5189+
"""
5190+
public enum Servers {
5191+
public static func server1() throws -> Foundation.URL {
5192+
try Foundation.URL(
5193+
validatingOpenAPIServerURL: "https://example.com/api",
5194+
variables: []
5195+
)
5196+
}
5197+
}
5198+
"""
5199+
)
5200+
}
5201+
5202+
func testServerWithNoVariablesAndFeatureFlagEnabled() throws {
5203+
try self.assertServerTranslation(
5204+
"""
5205+
url: https://example.com/api
5206+
""",
5207+
"""
5208+
public enum Servers {
5209+
public static func server1() throws -> Foundation.URL {
5210+
try Foundation.URL(
5211+
validatingOpenAPIServerURL: "https://example.com/api",
5212+
variables: []
5213+
)
5214+
}
5215+
}
5216+
""",
5217+
featureFlags: [.serverVariablesAsEnums]
5218+
)
5219+
}
5220+
5221+
func testServerWithDefaultVariable() throws {
5222+
try self.assertServerTranslation(
5223+
"""
5224+
url: '{protocol}://example.com/api'
5225+
description: A custom domain.
5226+
variables:
5227+
protocol:
5228+
default: https
5229+
description: A network protocol.
5230+
""",
5231+
"""
5232+
public enum Servers {
5233+
public static func server1(_protocol: Swift.String = "https") throws -> Foundation.URL {
5234+
try Foundation.URL(
5235+
validatingOpenAPIServerURL: "{protocol}://example.com/api",
5236+
variables: [
5237+
.init(
5238+
name: "protocol",
5239+
value: _protocol
5240+
)
5241+
]
5242+
)
5243+
}
5244+
}
5245+
"""
5246+
)
5247+
}
5248+
5249+
func testServerWithDefaultVariableAndFeatureFlagEnabled() throws {
5250+
try self.assertServerTranslation(
5251+
"""
5252+
url: '{protocol}://example.com/api'
5253+
description: A custom domain.
5254+
variables:
5255+
protocol:
5256+
default: https
5257+
description: A network protocol.
5258+
""",
5259+
"""
5260+
public enum Servers {
5261+
public static func server1(_protocol: Swift.String = "https") throws -> Foundation.URL {
5262+
try Foundation.URL(
5263+
validatingOpenAPIServerURL: "{protocol}://example.com/api",
5264+
variables: [
5265+
.init(
5266+
name: "protocol",
5267+
value: _protocol
5268+
)
5269+
]
5270+
)
5271+
}
5272+
}
5273+
""",
5274+
featureFlags: [.serverVariablesAsEnums]
5275+
)
5276+
}
5277+
5278+
func testServerWithDefaultAndEnumVariables() throws {
5279+
try self.assertServerTranslation(
5280+
"""
5281+
url: 'https://{environment}.example.com/api/{version}'
5282+
description: A custom domain.
5283+
variables:
5284+
environment:
5285+
enum:
5286+
- production
5287+
- sandbox
5288+
default: production
5289+
version:
5290+
default: v1
5291+
""",
5292+
"""
5293+
public enum Servers {
5294+
public static func server1(
5295+
environment: Swift.String = "production",
5296+
version: Swift.String = "v1"
5297+
) throws -> Foundation.URL {
5298+
try Foundation.URL(
5299+
validatingOpenAPIServerURL: "https://{environment}.example.com/api/{version}",
5300+
variables: [
5301+
.init(
5302+
name: "environment",
5303+
value: environment,
5304+
allowedValues: [
5305+
"production",
5306+
"sandbox"
5307+
]
5308+
),
5309+
.init(
5310+
name: "version",
5311+
value: version
5312+
)
5313+
]
5314+
)
5315+
}
5316+
}
5317+
"""
5318+
)
5319+
}
5320+
5321+
func testServerWithDefaultAndEnumVariablesAndFeatureFlagEnabled() throws {
5322+
try self.assertServerTranslation(
5323+
"""
5324+
url: 'https://{environment}.example.com/api/{version}'
5325+
description: A custom domain.
5326+
variables:
5327+
environment:
5328+
enum:
5329+
- production
5330+
- sandbox
5331+
default: production
5332+
version:
5333+
default: v1
5334+
""",
5335+
"""
5336+
public enum Servers {
5337+
public enum Variables {
5338+
public enum Server1 {
5339+
@frozen public enum Environment: Swift.String {
5340+
case production
5341+
case sandbox
5342+
public static var `default`: Environment {
5343+
return Environment.production
5344+
}
5345+
}
5346+
}
5347+
}
5348+
public static func server1(
5349+
environment: Variables.Server1.Environment = Variables.Server1.Environment.default,
5350+
version: Swift.String = "v1"
5351+
) throws -> Foundation.URL {
5352+
try Foundation.URL(
5353+
validatingOpenAPIServerURL: "https://{environment}.example.com/api/{version}",
5354+
variables: [
5355+
.init(
5356+
name: "environment",
5357+
value: environment.rawValue
5358+
),
5359+
.init(
5360+
name: "version",
5361+
value: version
5362+
)
5363+
]
5364+
)
5365+
}
5366+
}
5367+
""",
5368+
featureFlags: [.serverVariablesAsEnums]
5369+
)
5370+
}
51845371
}
51855372

51865373
extension SnippetBasedReferenceTests {
@@ -5206,6 +5393,19 @@ extension SnippetBasedReferenceTests {
52065393
components: components
52075394
)
52085395
}
5396+
5397+
func makeTypesTranslator(
5398+
accessModifier: AccessModifier = .public,
5399+
featureFlags: FeatureFlags = [],
5400+
ignoredDiagnosticMessages: Set<String> = [],
5401+
components: OpenAPI.Components = .noComponents
5402+
) throws -> TypesFileTranslator {
5403+
return TypesFileTranslator(
5404+
config: Config(mode: .types, access: accessModifier, featureFlags: featureFlags),
5405+
diagnostics: XCTestDiagnosticCollector(test: self, ignoredDiagnosticMessages: ignoredDiagnosticMessages),
5406+
components: components
5407+
)
5408+
}
52095409

52105410
func makeTranslators(
52115411
components: OpenAPI.Components = .noComponents,
@@ -5473,6 +5673,21 @@ extension SnippetBasedReferenceTests {
54735673
let (registerHandlersDecl, _) = try translator.translateRegisterHandlers(operations)
54745674
try XCTAssertSwiftEquivalent(registerHandlersDecl, expectedSwift, file: file, line: line)
54755675
}
5676+
5677+
func assertServerTranslation(
5678+
_ serverYAML: String,
5679+
_ expectedSwift: String,
5680+
accessModifier: AccessModifier = .public,
5681+
featureFlags: FeatureFlags = [],
5682+
file: StaticString = #filePath,
5683+
line: UInt = #line
5684+
) throws {
5685+
continueAfterFailure = false
5686+
let server = try YAMLDecoder().decode(OpenAPI.Server.self, from: serverYAML)
5687+
let translator = try makeTypesTranslator(accessModifier: accessModifier, featureFlags: featureFlags)
5688+
let translation = translator.translateServers([server])
5689+
try XCTAssertSwiftEquivalent(translation, expectedSwift, file: file, line: line)
5690+
}
54765691
}
54775692

54785693
private func XCTAssertEqualWithDiff(

0 commit comments

Comments
 (0)