From 368d5695e56e0efefd0c3dbc2d99b91d87e822fe Mon Sep 17 00:00:00 2001 From: Kostis Stefanou Date: Thu, 7 Mar 2024 15:20:37 +0200 Subject: [PATCH] [Generator] Add support of deepObject style in query params https://github.com/apple/swift-openapi-generator/issues/259 ~Depends on https://github.com/apple/swift-openapi-runtime/pull/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) --- .../Translator/CommonTypes/Constants.swift | 3 ++ .../Parameters/TypedParameter.swift | 6 ++- .../SnippetBasedReferenceTests.swift | 48 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift index d1fbedcf..72eed786 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift @@ -319,6 +319,9 @@ enum Constants { /// The form style. static let form = "form" + + /// The deepObject style. + static let deepObject = "deepObject" } } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift b/Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift index e8eb0700..2abd3d7d 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift @@ -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 @@ -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") } } diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index ba5e5905..5e3aa25c 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -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 @@ -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 } } @@ -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) } """, @@ -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)