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
apple#259

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

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

Support nested keys on query parameters.

Adapted snippet tests (SnippetBasedReferenceTests)
  • Loading branch information
kstefanou52 committed Oct 26, 2024
1 parent 7b4dd6b commit 368d569
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,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 @@ -130,7 +130,10 @@ extension FileTranslator {
let location = parameter.location
switch location {
case .query:
guard case .form = style else {
switch style {
case .form, .deepObject:
break
default:
try diagnostics.emitUnsupported(
"Query params of style \(style.rawValue), explode: \(explode)",
foundIn: foundIn
Expand Down Expand Up @@ -243,6 +246,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 @@ -2514,6 +2514,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 @@ -2524,18 +2536,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 @@ -2572,6 +2602,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 @@ -2598,6 +2635,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 368d569

Please sign in to comment.