From 0504c421e1b6ae05f8f0ab0ef0a75a9c0f7fcf71 Mon Sep 17 00:00:00 2001 From: theoriginalbit <1377564+theoriginalbit@users.noreply.github.com> Date: Thu, 19 Sep 2024 21:44:33 +1000 Subject: [PATCH] Introduce snippet-based reference tests for server variable translation --- .../TypesTranslator/translateServers.swift | 1 - .../SnippetBasedReferenceTests.swift | 215 ++++++++++++++++++ 2 files changed, 215 insertions(+), 1 deletion(-) diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift index aff1be9d..eaa7efd4 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift @@ -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) diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 0196c5ee..0089ccdd 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -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 { @@ -5206,6 +5393,19 @@ extension SnippetBasedReferenceTests { components: components ) } + + func makeTypesTranslator( + accessModifier: AccessModifier = .public, + featureFlags: FeatureFlags = [], + ignoredDiagnosticMessages: Set = [], + 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, @@ -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(