Skip to content

Commit 9985908

Browse files
authored
Correctly handle path params with a hyphen in the name (#602)
### Motivation Fixes #601, check out the issue for details. ### Modifications Adds hyphen to the regular expression that parses out params from the URL template. ### Result Correctly handle path params with hyphens in the name. ### Test Plan Added a unit test.
1 parent db5d1ea commit 9985908

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ extension OperationDescription {
248248
/// The regular expression for parsing subcomponents of path components.
249249
///
250250
/// Either a parameter `{foo}` or a constant value `foo`.
251-
private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_]+\})|([^{}]+)"#)
251+
private static let pathParameterRegex = try! NSRegularExpression(pattern: #"(\{[a-zA-Z0-9_\-\.]+\})|([^{}]+)"#)
252252

253253
/// Returns a string that contains the template to be generated for
254254
/// the client that fills in path parameters, and an array expression

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

+60
Original file line numberDiff line numberDiff line change
@@ -2605,6 +2605,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
26052605
"""
26062606
)
26072607
}
2608+
26082609
func testRequestWithPathParams() throws {
26092610
try self.assertRequestInTypesClientServerTranslation(
26102611
"""
@@ -2696,6 +2697,65 @@ final class SnippetBasedReferenceTests: XCTestCase {
26962697
)
26972698
}
26982699

2700+
func testRequestWithPathParamWithHyphenAndPeriod() throws {
2701+
try self.assertRequestInTypesClientServerTranslation(
2702+
"""
2703+
/foo/{p.a-b}:
2704+
get:
2705+
parameters:
2706+
- name: p.a-b
2707+
in: path
2708+
required: true
2709+
schema:
2710+
type: string
2711+
operationId: getFoo
2712+
responses:
2713+
default:
2714+
description: Response
2715+
""",
2716+
types: """
2717+
public struct Input: Sendable, Hashable {
2718+
public struct Path: Sendable, Hashable {
2719+
public var p_period_a_hyphen_b: Swift.String
2720+
public init(p_period_a_hyphen_b: Swift.String) {
2721+
self.p_period_a_hyphen_b = p_period_a_hyphen_b
2722+
}
2723+
}
2724+
public var path: Operations.getFoo.Input.Path
2725+
public init(path: Operations.getFoo.Input.Path) {
2726+
self.path = path
2727+
}
2728+
}
2729+
""",
2730+
client: """
2731+
{ input in
2732+
let path = try converter.renderedPath(
2733+
template: "/foo/{}",
2734+
parameters: [
2735+
input.path.p_period_a_hyphen_b
2736+
]
2737+
)
2738+
var request: HTTPTypes.HTTPRequest = .init(
2739+
soar_path: path,
2740+
method: .get
2741+
)
2742+
suppressMutabilityWarning(&request)
2743+
return (request, nil)
2744+
}
2745+
""",
2746+
server: """
2747+
{ request, requestBody, metadata in
2748+
let path: Operations.getFoo.Input.Path = .init(p_period_a_hyphen_b: try converter.getPathParameterAsURI(
2749+
in: metadata.pathParameters,
2750+
name: "p.a-b",
2751+
as: Swift.String.self
2752+
))
2753+
return Operations.getFoo.Input(path: path)
2754+
}
2755+
"""
2756+
)
2757+
}
2758+
26992759
func testRequestRequiredBodyPrimitiveSchema() throws {
27002760
try self.assertRequestInTypesClientServerTranslation(
27012761
"""

0 commit comments

Comments
 (0)