Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Generator] Add support of deepObject style in query params #538

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let package = Package(
// Tests-only: Runtime library linked by generated code, and also
// helps keep the runtime library new enough to work with the generated
// code.
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.3.2"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.2"),
],
targets: [
Expand Down
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,12 @@ extension FileTranslator {
let location = parameter.location
switch location {
case .query:
guard case .form = style else {
switch style {
case .form:
break
case .deepObject where explode:
break
default:
try diagnostics.emitUnsupported(
"Query params of style \(style.rawValue), explode: \(explode)",
foundIn: foundIn
Expand Down Expand Up @@ -243,6 +248,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
12 changes: 12 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ paths:
schema:
format: uuid
type: string
- name: sort
in: query
required: false
style: deepObject
explode: true
schema:
type: object
properties:
id:
type: string
name:
type: string
- $ref: '#/components/parameters/query.born-since'
responses:
'200':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public struct Client: APIProtocol {
name: "My-Request-UUID",
value: input.headers.My_hyphen_Request_hyphen_UUID
)
try converter.setQueryItemAsURI(
in: &request,
style: .deepObject,
explode: true,
name: "sort",
value: input.query.sort
)
try converter.setQueryItemAsURI(
in: &request,
style: .form,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
name: "feeds",
as: Operations.listPets.Input.Query.feedsPayload.self
),
sort: try converter.getOptionalQueryItemAsURI(
in: request.soar_query,
style: .deepObject,
explode: true,
name: "sort",
as: Operations.listPets.Input.Query.sortPayload.self
),
since: try converter.getOptionalQueryItemAsURI(
in: request.soar_query,
style: .form,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,31 @@ public enum Operations {
public typealias feedsPayload = [Operations.listPets.Input.Query.feedsPayloadPayload]
/// - Remark: Generated from `#/paths/pets/GET/query/feeds`.
public var feeds: Operations.listPets.Input.Query.feedsPayload?
/// - Remark: Generated from `#/paths/pets/GET/query/sort`.
public struct sortPayload: Codable, Hashable, Sendable {
/// - Remark: Generated from `#/paths/pets/GET/query/sort/id`.
public var id: Swift.String?
/// - Remark: Generated from `#/paths/pets/GET/query/sort/name`.
public var name: Swift.String?
/// Creates a new `sortPayload`.
///
/// - Parameters:
/// - id:
/// - name:
public init(
id: Swift.String? = nil,
name: Swift.String? = nil
) {
self.id = id
self.name = name
}
public enum CodingKeys: String, CodingKey {
case id
case name
}
}
/// - Remark: Generated from `#/paths/pets/GET/query/sort`.
public var sort: Operations.listPets.Input.Query.sortPayload?
/// Supply this parameter to filter pets born since the provided date.
///
/// - Remark: Generated from `#/paths/pets/GET/query/since`.
Expand All @@ -1865,16 +1890,19 @@ public enum Operations {
/// - limit: How many items to return at one time (max 100)
/// - habitat:
/// - feeds:
/// - sort:
/// - since: Supply this parameter to filter pets born since the provided date.
public init(
limit: Swift.Int32? = nil,
habitat: Operations.listPets.Input.Query.habitatPayload? = nil,
feeds: Operations.listPets.Input.Query.feedsPayload? = nil,
sort: Operations.listPets.Input.Query.sortPayload? = nil,
since: Components.Parameters.query_period_born_hyphen_since? = nil
) {
self.limit = limit
self.habitat = habitat
self.feeds = feeds
self.sort = sort
self.since = since
}
}
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
kstefanou52 marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions Tests/PetstoreConsumerTests/Test_Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Test_Client: XCTestCase {
XCTAssertEqual(operationID, "listPets")
XCTAssertEqual(
request.path,
"/pets?limit=24&habitat=water&feeds=herbivore&feeds=carnivore&since=2023-01-18T10%3A04%3A11Z"
"/pets?limit=24&habitat=water&feeds=herbivore&feeds=carnivore&sort%5Bid%5D=ascending&sort%5Bname%5D=descending&since=2023-01-18T10%3A04%3A11Z"
)
XCTAssertEqual(baseURL.absoluteString, "/api")
XCTAssertEqual(request.method, .get)
Expand All @@ -66,7 +66,7 @@ final class Test_Client: XCTestCase {
}
let response = try await client.listPets(
.init(
query: .init(limit: 24, habitat: .water, feeds: [.herbivore, .carnivore], since: .test),
query: .init(limit: 24, habitat: .water, feeds: [.herbivore, .carnivore], sort: .init(id: "ascending", name: "descending"), since: .test),
headers: .init(My_hyphen_Request_hyphen_UUID: "abcd-1234")
)
)
Expand Down
3 changes: 2 additions & 1 deletion Tests/PetstoreConsumerTests/Test_Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class Test_Server: XCTestCase {
XCTAssertEqual(input.query.habitat, .water)
XCTAssertEqual(input.query.since, .test)
XCTAssertEqual(input.query.feeds, [.carnivore, .herbivore])
XCTAssertEqual(input.query.sort, .init(id: "ascending", name: "descending"))
kstefanou52 marked this conversation as resolved.
Show resolved Hide resolved
XCTAssertEqual(input.headers.My_hyphen_Request_hyphen_UUID, "abcd-1234")
return .ok(
.init(
Expand All @@ -43,7 +44,7 @@ final class Test_Server: XCTestCase {
})
let (response, responseBody) = try await server.listPets(
.init(
soar_path: "/api/pets?limit=24&habitat=water&feeds=carnivore&feeds=herbivore&since=\(Date.testString)",
soar_path: "/api/pets?sort%5Bid%5D=ascending&sort%5Bname%5D=descending",
method: .get,
headerFields: [.init("My-Request-UUID")!: "abcd-1234"]
),
Expand Down