Skip to content

Commit

Permalink
[Generator] Add support of deepObject style in query params
Browse files Browse the repository at this point in the history
### Motivation

apple#259

~Depends on apple/swift-openapi-runtime#100
landing first and getting released, and the version dependency being
bumped here.~

### Modifications

Added `deepObject` style to serializer & parser in order to support nested keys on query parameters.

### Result

Support nested keys on query parameters.

### Test Plan

Adapted snippet tests (SnippetBasedReferenceTests)
  • Loading branch information
kstefanou52 committed Mar 7, 2024
1 parent 7992d77 commit 82556e6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ enum Constants {

/// The form style.
static let form = "form"

/// The deepObject style.
static let deepObject = "deepObject"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ extension FileTranslator {
let location = parameter.location
switch location {
case .query:
guard case .form = style else {
switch style {
case .form, .deepObject:
break
default:
diagnostics.emitUnsupported(
"Query params of style \(style.rawValue), explode: \(explode)",
foundIn: foundIn
)
return nil
}
case .header, .path:
guard case .simple = style else {
Expand Down Expand Up @@ -252,6 +254,7 @@ extension OpenAPI.Parameter.SchemaContext.Style {
var runtimeName: String {
switch self {
case .form: return Constants.Components.Parameters.Style.form
case .deepObject: return Constants.Components.Parameters.Style.deepObject
default: preconditionFailure("Unsupported style")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,18 @@ final class SnippetBasedReferenceTests: XCTestCase {
type: array
items:
type: string
- name: sort
in: query
required: true
style: deepObject
explode: true
schema:
type: object
properties:
option1:
type: string
option2:
type: string
responses:
default:
description: Response
Expand All @@ -2414,18 +2426,36 @@ final class SnippetBasedReferenceTests: XCTestCase {
public var single: Swift.String?
public var manyExploded: [Swift.String]?
public var manyUnexploded: [Swift.String]?
public struct sortPayload: Codable, Hashable, Sendable {
public var option1: Swift.String?
public var option2: Swift.String?
public init(
option1: Swift.String? = nil,
option2: Swift.String? = nil
) {
self.option1 = option1
self.option2 = option2
}
public enum CodingKeys: String, CodingKey {
case option1
case option2
}
}
public var sort: Operations.get_sol_foo.Input.Query.sortPayload
public init(
single: Swift.String? = nil,
manyExploded: [Swift.String]? = nil,
manyUnexploded: [Swift.String]? = nil
manyUnexploded: [Swift.String]? = nil,
sort: Operations.get_sol_foo.Input.Query.sortPayload
) {
self.single = single
self.manyExploded = manyExploded
self.manyUnexploded = manyUnexploded
self.sort = sort
}
}
public var query: Operations.get_sol_foo.Input.Query
public init(query: Operations.get_sol_foo.Input.Query = .init()) {
public init(query: Operations.get_sol_foo.Input.Query) {
self.query = query
}
}
Expand Down Expand Up @@ -2462,6 +2492,13 @@ final class SnippetBasedReferenceTests: XCTestCase {
name: "manyUnexploded",
value: input.query.manyUnexploded
)
try converter.setQueryItemAsURI(
in: &request,
style: .deepObject,
explode: true,
name: "sort",
value: input.query.sort
)
return (request, nil)
}
""",
Expand All @@ -2488,6 +2525,13 @@ final class SnippetBasedReferenceTests: XCTestCase {
explode: false,
name: "manyUnexploded",
as: [Swift.String].self
),
sort: try converter.getRequiredQueryItemAsURI(
in: request.soar_query,
style: .deepObject,
explode: true,
name: "sort",
as: Operations.get_sol_foo.Input.Query.sortPayload.self
)
)
return Operations.get_sol_foo.Input(query: query)
Expand Down

0 comments on commit 82556e6

Please sign in to comment.