From 8531e2c68967e3f6120e23823f79d3b1bccb3b53 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 23 Jul 2024 11:38:43 +0200 Subject: [PATCH 1/2] Correctly handle path params with a hyphen in the name --- .../Operations/OperationDescription.swift | 2 +- .../SnippetBasedReferenceTests.swift | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift index 42ff9989..c56b94a7 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift @@ -248,7 +248,7 @@ extension OperationDescription { /// The regular expression for parsing subcomponents of path components. /// /// Either a parameter `{foo}` or a constant value `foo`. - private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_]+\})|([^{}]+)"#) + private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_-]+\})|([^{}]+)"#) /// Returns a string that contains the template to be generated for /// the client that fills in path parameters, and an array expression diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index a362df37..76be230a 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -2605,6 +2605,7 @@ final class SnippetBasedReferenceTests: XCTestCase { """ ) } + func testRequestWithPathParams() throws { try self.assertRequestInTypesClientServerTranslation( """ @@ -2696,6 +2697,65 @@ final class SnippetBasedReferenceTests: XCTestCase { ) } + func testRequestWithPathParamWithHyphen() throws { + try self.assertRequestInTypesClientServerTranslation( + """ + /foo/{a-b}: + get: + parameters: + - name: a-b + in: path + required: true + schema: + type: string + operationId: getFoo + responses: + default: + description: Response + """, + types: """ + public struct Input: Sendable, Hashable { + public struct Path: Sendable, Hashable { + public var a_hyphen_b: Swift.String + public init(a_hyphen_b: Swift.String) { + self.a_hyphen_b = a_hyphen_b + } + } + public var path: Operations.getFoo.Input.Path + public init(path: Operations.getFoo.Input.Path) { + self.path = path + } + } + """, + client: """ + { input in + let path = try converter.renderedPath( + template: "/foo/{}", + parameters: [ + input.path.a_hyphen_b + ] + ) + var request: HTTPTypes.HTTPRequest = .init( + soar_path: path, + method: .get + ) + suppressMutabilityWarning(&request) + return (request, nil) + } + """, + server: """ + { request, requestBody, metadata in + let path: Operations.getFoo.Input.Path = .init(a_hyphen_b: try converter.getPathParameterAsURI( + in: metadata.pathParameters, + name: "a-b", + as: Swift.String.self + )) + return Operations.getFoo.Input(path: path) + } + """ + ) + } + func testRequestRequiredBodyPrimitiveSchema() throws { try self.assertRequestInTypesClientServerTranslation( """ From 413ac87f76ddb1509ae9a15aff4fcfa7ebd966b0 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 23 Jul 2024 11:43:18 +0200 Subject: [PATCH 2/2] Also add support for a period --- .../Operations/OperationDescription.swift | 2 +- .../SnippetBasedReferenceTests.swift | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift index c56b94a7..aea727dc 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift @@ -248,7 +248,7 @@ extension OperationDescription { /// The regular expression for parsing subcomponents of path components. /// /// Either a parameter `{foo}` or a constant value `foo`. - private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_-]+\})|([^{}]+)"#) + private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_\-\.]+\})|([^{}]+)"#) /// Returns a string that contains the template to be generated for /// the client that fills in path parameters, and an array expression diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 76be230a..0196c5ee 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -2697,13 +2697,13 @@ final class SnippetBasedReferenceTests: XCTestCase { ) } - func testRequestWithPathParamWithHyphen() throws { + func testRequestWithPathParamWithHyphenAndPeriod() throws { try self.assertRequestInTypesClientServerTranslation( """ - /foo/{a-b}: + /foo/{p.a-b}: get: parameters: - - name: a-b + - name: p.a-b in: path required: true schema: @@ -2716,9 +2716,9 @@ final class SnippetBasedReferenceTests: XCTestCase { types: """ public struct Input: Sendable, Hashable { public struct Path: Sendable, Hashable { - public var a_hyphen_b: Swift.String - public init(a_hyphen_b: Swift.String) { - self.a_hyphen_b = a_hyphen_b + public var p_period_a_hyphen_b: Swift.String + public init(p_period_a_hyphen_b: Swift.String) { + self.p_period_a_hyphen_b = p_period_a_hyphen_b } } public var path: Operations.getFoo.Input.Path @@ -2732,7 +2732,7 @@ final class SnippetBasedReferenceTests: XCTestCase { let path = try converter.renderedPath( template: "/foo/{}", parameters: [ - input.path.a_hyphen_b + input.path.p_period_a_hyphen_b ] ) var request: HTTPTypes.HTTPRequest = .init( @@ -2745,9 +2745,9 @@ final class SnippetBasedReferenceTests: XCTestCase { """, server: """ { request, requestBody, metadata in - let path: Operations.getFoo.Input.Path = .init(a_hyphen_b: try converter.getPathParameterAsURI( + let path: Operations.getFoo.Input.Path = .init(p_period_a_hyphen_b: try converter.getPathParameterAsURI( in: metadata.pathParameters, - name: "a-b", + name: "p.a-b", as: Swift.String.self )) return Operations.getFoo.Input(path: path)