Skip to content

Commit

Permalink
Correctly handle path params with a hyphen in the name (#602)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
czechboy0 authored Jul 23, 2024
1 parent db5d1ea commit 9985908
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
"""
)
}

func testRequestWithPathParams() throws {
try self.assertRequestInTypesClientServerTranslation(
"""
Expand Down Expand Up @@ -2696,6 +2697,65 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testRequestWithPathParamWithHyphenAndPeriod() throws {
try self.assertRequestInTypesClientServerTranslation(
"""
/foo/{p.a-b}:
get:
parameters:
- name: p.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 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
public init(path: Operations.getFoo.Input.Path) {
self.path = path
}
}
""",
client: """
{ input in
let path = try converter.renderedPath(
template: "/foo/{}",
parameters: [
input.path.p_period_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(p_period_a_hyphen_b: try converter.getPathParameterAsURI(
in: metadata.pathParameters,
name: "p.a-b",
as: Swift.String.self
))
return Operations.getFoo.Input(path: path)
}
"""
)
}

func testRequestRequiredBodyPrimitiveSchema() throws {
try self.assertRequestInTypesClientServerTranslation(
"""
Expand Down

0 comments on commit 9985908

Please sign in to comment.