Skip to content

Commit f933fb9

Browse files
kstefanou52arthurcro
authored andcommitted
[Generator] Add support of deepObject style in query params
1 parent 9727261 commit f933fb9

File tree

8 files changed

+109
-4
lines changed

8 files changed

+109
-4
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let package = Package(
6060
// Tests-only: Runtime library linked by generated code, and also
6161
// helps keep the runtime library new enough to work with the generated
6262
// code.
63-
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.3.2"),
63+
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.4.0"),
6464
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.2"),
6565
],
6666
targets: [

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ enum Constants {
319319

320320
/// The form style.
321321
static let form = "form"
322+
323+
/// The deepObject style.
324+
static let deepObject = "deepObject"
322325
}
323326
}
324327

Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ extension FileTranslator {
130130
let location = parameter.location
131131
switch location {
132132
case .query:
133-
guard case .form = style else {
133+
switch style {
134+
case .form, .deepObject:
135+
break
136+
default:
134137
try diagnostics.emitUnsupported(
135138
"Query params of style \(style.rawValue), explode: \(explode)",
136139
foundIn: foundIn
@@ -243,6 +246,7 @@ extension OpenAPI.Parameter.SchemaContext.Style {
243246
var runtimeName: String {
244247
switch self {
245248
case .form: return Constants.Components.Parameters.Style.form
249+
case .deepObject: return Constants.Components.Parameters.Style.deepObject
246250
default: preconditionFailure("Unsupported style")
247251
}
248252
}

Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ paths:
6969
schema:
7070
format: uuid
7171
type: string
72+
- name: sort
73+
in: query
74+
required: false
75+
style: deepObject
76+
explode: true
77+
schema:
78+
type: object
79+
properties:
80+
id:
81+
type: string
82+
name:
83+
type: string
7284
- $ref: '#/components/parameters/query.born-since'
7385
responses:
7486
'200':

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public struct Client: APIProtocol {
8787
name: "My-Request-UUID",
8888
value: input.headers.My_hyphen_Request_hyphen_UUID
8989
)
90+
try converter.setQueryItemAsURI(
91+
in: &request,
92+
style: .deepObject,
93+
explode: true,
94+
name: "sort",
95+
value: input.query.sort
96+
)
9097
try converter.setQueryItemAsURI(
9198
in: &request,
9299
style: .form,

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
187187
name: "feeds",
188188
as: Operations.listPets.Input.Query.feedsPayload.self
189189
),
190+
sort: try converter.getOptionalQueryItemAsURI(
191+
in: request.soar_query,
192+
style: .deepObject,
193+
explode: true,
194+
name: "sort",
195+
as: Operations.listPets.Input.Query.sortPayload.self
196+
),
190197
since: try converter.getOptionalQueryItemAsURI(
191198
in: request.soar_query,
192199
style: .form,

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,31 @@ public enum Operations {
18551855
public typealias feedsPayload = [Operations.listPets.Input.Query.feedsPayloadPayload]
18561856
/// - Remark: Generated from `#/paths/pets/GET/query/feeds`.
18571857
public var feeds: Operations.listPets.Input.Query.feedsPayload?
1858+
/// - Remark: Generated from `#/paths/pets/GET/query/sort`.
1859+
public struct sortPayload: Codable, Hashable, Sendable {
1860+
/// - Remark: Generated from `#/paths/pets/GET/query/sort/id`.
1861+
public var id: Swift.String?
1862+
/// - Remark: Generated from `#/paths/pets/GET/query/sort/name`.
1863+
public var name: Swift.String?
1864+
/// Creates a new `sortPayload`.
1865+
///
1866+
/// - Parameters:
1867+
/// - id:
1868+
/// - name:
1869+
public init(
1870+
id: Swift.String? = nil,
1871+
name: Swift.String? = nil
1872+
) {
1873+
self.id = id
1874+
self.name = name
1875+
}
1876+
public enum CodingKeys: String, CodingKey {
1877+
case id
1878+
case name
1879+
}
1880+
}
1881+
/// - Remark: Generated from `#/paths/pets/GET/query/sort`.
1882+
public var sort: Operations.listPets.Input.Query.sortPayload?
18581883
/// Supply this parameter to filter pets born since the provided date.
18591884
///
18601885
/// - Remark: Generated from `#/paths/pets/GET/query/since`.
@@ -1865,16 +1890,19 @@ public enum Operations {
18651890
/// - limit: How many items to return at one time (max 100)
18661891
/// - habitat:
18671892
/// - feeds:
1893+
/// - sort:
18681894
/// - since: Supply this parameter to filter pets born since the provided date.
18691895
public init(
18701896
limit: Swift.Int32? = nil,
18711897
habitat: Operations.listPets.Input.Query.habitatPayload? = nil,
18721898
feeds: Operations.listPets.Input.Query.feedsPayload? = nil,
1899+
sort: Operations.listPets.Input.Query.sortPayload? = nil,
18731900
since: Components.Parameters.query_period_born_hyphen_since? = nil
18741901
) {
18751902
self.limit = limit
18761903
self.habitat = habitat
18771904
self.feeds = feeds
1905+
self.sort = sort
18781906
self.since = since
18791907
}
18801908
}

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,18 @@ final class SnippetBasedReferenceTests: XCTestCase {
25142514
type: array
25152515
items:
25162516
type: string
2517+
- name: sort
2518+
in: query
2519+
required: true
2520+
style: deepObject
2521+
explode: true
2522+
schema:
2523+
type: object
2524+
properties:
2525+
option1:
2526+
type: string
2527+
option2:
2528+
type: string
25172529
responses:
25182530
default:
25192531
description: Response
@@ -2524,18 +2536,36 @@ final class SnippetBasedReferenceTests: XCTestCase {
25242536
public var single: Swift.String?
25252537
public var manyExploded: [Swift.String]?
25262538
public var manyUnexploded: [Swift.String]?
2539+
public struct sortPayload: Codable, Hashable, Sendable {
2540+
public var option1: Swift.String?
2541+
public var option2: Swift.String?
2542+
public init(
2543+
option1: Swift.String? = nil,
2544+
option2: Swift.String? = nil
2545+
) {
2546+
self.option1 = option1
2547+
self.option2 = option2
2548+
}
2549+
public enum CodingKeys: String, CodingKey {
2550+
case option1
2551+
case option2
2552+
}
2553+
}
2554+
public var sort: Operations.get_sol_foo.Input.Query.sortPayload
25272555
public init(
25282556
single: Swift.String? = nil,
25292557
manyExploded: [Swift.String]? = nil,
2530-
manyUnexploded: [Swift.String]? = nil
2558+
manyUnexploded: [Swift.String]? = nil,
2559+
sort: Operations.get_sol_foo.Input.Query.sortPayload
25312560
) {
25322561
self.single = single
25332562
self.manyExploded = manyExploded
25342563
self.manyUnexploded = manyUnexploded
2564+
self.sort = sort
25352565
}
25362566
}
25372567
public var query: Operations.get_sol_foo.Input.Query
2538-
public init(query: Operations.get_sol_foo.Input.Query = .init()) {
2568+
public init(query: Operations.get_sol_foo.Input.Query) {
25392569
self.query = query
25402570
}
25412571
}
@@ -2572,6 +2602,13 @@ final class SnippetBasedReferenceTests: XCTestCase {
25722602
name: "manyUnexploded",
25732603
value: input.query.manyUnexploded
25742604
)
2605+
try converter.setQueryItemAsURI(
2606+
in: &request,
2607+
style: .deepObject,
2608+
explode: true,
2609+
name: "sort",
2610+
value: input.query.sort
2611+
)
25752612
return (request, nil)
25762613
}
25772614
""",
@@ -2598,6 +2635,13 @@ final class SnippetBasedReferenceTests: XCTestCase {
25982635
explode: false,
25992636
name: "manyUnexploded",
26002637
as: [Swift.String].self
2638+
),
2639+
sort: try converter.getRequiredQueryItemAsURI(
2640+
in: request.soar_query,
2641+
style: .deepObject,
2642+
explode: true,
2643+
name: "sort",
2644+
as: Operations.get_sol_foo.Input.Query.sortPayload.self
26012645
)
26022646
)
26032647
return Operations.get_sol_foo.Input(query: query)

0 commit comments

Comments
 (0)