Skip to content

Commit

Permalink
Introduce snippet-based reference tests for server variable translation
Browse files Browse the repository at this point in the history
  • Loading branch information
theoriginalbit committed Sep 19, 2024
1 parent 78bf584 commit 0504c42
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ extension TypesFileTranslator {

var serverDecls: [Declaration] = []


for (index, decl) in servers.enumerated() {
let serverDecl = translateServer(index: index, server: decl, variablesNamespace: &variablesNamespace)
serverDecls.append(serverDecl)
Expand Down
215 changes: 215 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5181,6 +5181,193 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testServerWithNoVariables() throws {
try self.assertServerTranslation(
"""
url: https://example.com/api
""",
"""
public enum Servers {
public static func server1() throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "https://example.com/api",
variables: []
)
}
}
"""
)
}

func testServerWithNoVariablesAndFeatureFlagEnabled() throws {
try self.assertServerTranslation(
"""
url: https://example.com/api
""",
"""
public enum Servers {
public static func server1() throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "https://example.com/api",
variables: []
)
}
}
""",
featureFlags: [.serverVariablesAsEnums]
)
}

func testServerWithDefaultVariable() throws {
try self.assertServerTranslation(
"""
url: '{protocol}://example.com/api'
description: A custom domain.
variables:
protocol:
default: https
description: A network protocol.
""",
"""
public enum Servers {
public static func server1(_protocol: Swift.String = "https") throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "{protocol}://example.com/api",
variables: [
.init(
name: "protocol",
value: _protocol
)
]
)
}
}
"""
)
}

func testServerWithDefaultVariableAndFeatureFlagEnabled() throws {
try self.assertServerTranslation(
"""
url: '{protocol}://example.com/api'
description: A custom domain.
variables:
protocol:
default: https
description: A network protocol.
""",
"""
public enum Servers {
public static func server1(_protocol: Swift.String = "https") throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "{protocol}://example.com/api",
variables: [
.init(
name: "protocol",
value: _protocol
)
]
)
}
}
""",
featureFlags: [.serverVariablesAsEnums]
)
}

func testServerWithDefaultAndEnumVariables() throws {
try self.assertServerTranslation(
"""
url: 'https://{environment}.example.com/api/{version}'
description: A custom domain.
variables:
environment:
enum:
- production
- sandbox
default: production
version:
default: v1
""",
"""
public enum Servers {
public static func server1(
environment: Swift.String = "production",
version: Swift.String = "v1"
) throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "https://{environment}.example.com/api/{version}",
variables: [
.init(
name: "environment",
value: environment,
allowedValues: [
"production",
"sandbox"
]
),
.init(
name: "version",
value: version
)
]
)
}
}
"""
)
}

func testServerWithDefaultAndEnumVariablesAndFeatureFlagEnabled() throws {
try self.assertServerTranslation(
"""
url: 'https://{environment}.example.com/api/{version}'
description: A custom domain.
variables:
environment:
enum:
- production
- sandbox
default: production
version:
default: v1
""",
"""
public enum Servers {
public enum Variables {
public enum Server1 {
@frozen public enum Environment: Swift.String {
case production
case sandbox
public static var `default`: Environment {
return Environment.production
}
}
}
}
public static func server1(
environment: Variables.Server1.Environment = Variables.Server1.Environment.default,
version: Swift.String = "v1"
) throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "https://{environment}.example.com/api/{version}",
variables: [
.init(
name: "environment",
value: environment.rawValue
),
.init(
name: "version",
value: version
)
]
)
}
}
""",
featureFlags: [.serverVariablesAsEnums]
)
}
}

extension SnippetBasedReferenceTests {
Expand All @@ -5206,6 +5393,19 @@ extension SnippetBasedReferenceTests {
components: components
)
}

func makeTypesTranslator(
accessModifier: AccessModifier = .public,
featureFlags: FeatureFlags = [],
ignoredDiagnosticMessages: Set<String> = [],
components: OpenAPI.Components = .noComponents
) throws -> TypesFileTranslator {
return TypesFileTranslator(
config: Config(mode: .types, access: accessModifier, featureFlags: featureFlags),
diagnostics: XCTestDiagnosticCollector(test: self, ignoredDiagnosticMessages: ignoredDiagnosticMessages),
components: components
)
}

func makeTranslators(
components: OpenAPI.Components = .noComponents,
Expand Down Expand Up @@ -5473,6 +5673,21 @@ extension SnippetBasedReferenceTests {
let (registerHandlersDecl, _) = try translator.translateRegisterHandlers(operations)
try XCTAssertSwiftEquivalent(registerHandlersDecl, expectedSwift, file: file, line: line)
}

func assertServerTranslation(
_ serverYAML: String,
_ expectedSwift: String,
accessModifier: AccessModifier = .public,
featureFlags: FeatureFlags = [],
file: StaticString = #filePath,
line: UInt = #line
) throws {
continueAfterFailure = false
let server = try YAMLDecoder().decode(OpenAPI.Server.self, from: serverYAML)
let translator = try makeTypesTranslator(accessModifier: accessModifier, featureFlags: featureFlags)
let translation = translator.translateServers([server])
try XCTAssertSwiftEquivalent(translation, expectedSwift, file: file, line: line)
}
}

private func XCTAssertEqualWithDiff(
Expand Down

0 comments on commit 0504c42

Please sign in to comment.