Skip to content

Commit

Permalink
Merge pull request #346 from mattpolzin/explode-query-param-to31
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin authored Nov 13, 2023
2 parents 7dbb3ce + cf0a971 commit b1d7ed3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Sources/OpenAPIKitCompat/Compat30To31.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ extension OpenAPIKit30.OpenAPI.Parameter.SchemaContext: To31 {
return OpenAPIKit.OpenAPI.Parameter.SchemaContext(
schemaReference: .init(ref.to31()),
style: style,
explode: explode,
allowReserved: allowReserved,
examples: newExamples
)
} else {
return OpenAPIKit.OpenAPI.Parameter.SchemaContext(
schemaReference: .init(ref.to31()),
style: style,
explode: explode,
allowReserved: allowReserved,
example: example
)
Expand All @@ -192,13 +194,15 @@ extension OpenAPIKit30.OpenAPI.Parameter.SchemaContext: To31 {
return OpenAPIKit.OpenAPI.Parameter.SchemaContext(
schema.to31(),
style: style,
explode: explode,
allowReserved: allowReserved,
examples: newExamples
)
} else {
return OpenAPIKit.OpenAPI.Parameter.SchemaContext(
schema.to31(),
style: style,
explode: explode,
allowReserved: allowReserved,
example: example
)
Expand Down
66 changes: 59 additions & 7 deletions Tests/OpenAPIKitCompatTests/DocumentConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,51 @@ final class DocumentConversionTests: XCTestCase {
}

func test_Parameter() throws {
// TODO: write test
let parameter1 = OpenAPIKit30.OpenAPI.Parameter(
name: "hello",
context: .query,
schemaOrContent: .a(OpenAPIKit30.OpenAPI.Parameter.SchemaContext(
.object,
style: .simple
))
)

let parameter2 = OpenAPIKit30.OpenAPI.Parameter(
name: "hello",
context: .query,
schemaOrContent: .a(OpenAPIKit30.OpenAPI.Parameter.SchemaContext(
.object,
style: .form,
explode: false,
allowReserved: true,
example: "hi=hello"
)),
description: "described",
deprecated: true,
vendorExtensions: [ "x-hello": "hi" ]
)

let oldDoc = OpenAPIKit30.OpenAPI.Document(
info: .init(title: "hello", version: "1.0.0"),
servers: [],
paths: [:],
components: .init(
parameters: [
"param1": parameter1,
"param2": parameter2
]
)
)

let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc,oldDoc)

let newParameter1 = try XCTUnwrap(newDoc.components.parameters["param1"])
let newParameter2 = try XCTUnwrap(newDoc.components.parameters["param2"])

try assertEqualNewToOld(newParameter1, parameter1)
try assertEqualNewToOld(newParameter2, parameter2)
}

// TODO: more tests
Expand Down Expand Up @@ -866,15 +910,15 @@ fileprivate func assertEqualNewToOld(_ newServer: OpenAPIKit.OpenAPI.Server?, _
}
}

fileprivate func assertEqualNewToOld(_ newParamArray: OpenAPIKit.OpenAPI.Parameter.Array, _ oldParamArray: OpenAPIKit30.OpenAPI.Parameter.Array) {
fileprivate func assertEqualNewToOld(_ newParamArray: OpenAPIKit.OpenAPI.Parameter.Array, _ oldParamArray: OpenAPIKit30.OpenAPI.Parameter.Array) throws {
for (newParameter, oldParameter) in zip(newParamArray, oldParamArray) {
switch (newParameter, oldParameter) {
case (.a(let ref), .a(let ref2)):
XCTAssertNil(ref.summary)
XCTAssertNil(ref.description)
XCTAssertEqual(ref.jsonReference.absoluteString, ref2.absoluteString)
case (.b(let param), .b(let param2)):
assertEqualNewToOld(param, param2)
try assertEqualNewToOld(param, param2)
default:
XCTFail("Parameters are not equal because one is a reference and the other is not: \(newParameter) / \(oldParameter)")
}
Expand All @@ -896,7 +940,7 @@ fileprivate func assertEqualNewToOld(_ newPathItem: OpenAPIKit.OpenAPI.PathItem?
try assertEqualNewToOld(newServer, oldServer)
}
}
assertEqualNewToOld(newPathItem.parameters, oldPathItem.parameters)
try assertEqualNewToOld(newPathItem.parameters, oldPathItem.parameters)
try assertEqualNewToOld(newPathItem.get, oldPathItem.get)
try assertEqualNewToOld(newPathItem.put, oldPathItem.put)
try assertEqualNewToOld(newPathItem.post, oldPathItem.post)
Expand All @@ -909,9 +953,17 @@ fileprivate func assertEqualNewToOld(_ newPathItem: OpenAPIKit.OpenAPI.PathItem?
XCTAssertEqual(newPathItem.vendorExtensions, oldPathItem.vendorExtensions)
}

fileprivate func assertEqualNewToOld(_ newParam: OpenAPIKit.OpenAPI.Parameter, _ oldParam: OpenAPIKit30.OpenAPI.Parameter) {
fileprivate func assertEqualNewToOld(_ newParam: OpenAPIKit.OpenAPI.Parameter, _ oldParam: OpenAPIKit30.OpenAPI.Parameter) throws {
XCTAssertEqual(newParam.name, oldParam.name)
assertEqualNewToOld(newParam.context, oldParam.context)
switch (newParam.schemaOrContent, oldParam.schemaOrContent) {
case (.a(let newSchema), .a(let oldSchema)):
try assertEqualNewToOld(newSchema, oldSchema)
case (.b(let newContent), .b(let oldContent)):
try assertEqualNewToOld(newContent, oldContent)
default:
XCTFail("Parameter schemaOrContent are not equal. \(newParam.schemaOrContent) / \(oldParam.schemaOrContent)")
}
XCTAssertEqual(newParam.description, oldParam.description)
XCTAssertEqual(newParam.deprecated, oldParam.deprecated)
XCTAssertEqual(newParam.vendorExtensions, oldParam.vendorExtensions)
Expand Down Expand Up @@ -943,7 +995,7 @@ fileprivate func assertEqualNewToOld(_ newOperation: OpenAPIKit.OpenAPI.Operatio
XCTAssertEqual(newOp.description, oldOp.description)
try assertEqualNewToOld(newOp.externalDocs, oldOp.externalDocs)
XCTAssertEqual(newOp.operationId, oldOp.operationId)
assertEqualNewToOld(newOp.parameters, oldOp.parameters)
try assertEqualNewToOld(newOp.parameters, oldOp.parameters)
if let newRequest = newOp.requestBody {
let oldRequest = try XCTUnwrap(oldOp.requestBody)
switch (newRequest, oldRequest) {
Expand Down Expand Up @@ -1407,7 +1459,7 @@ fileprivate func assertEqualNewToOld(_ newComponents: OpenAPIKit.OpenAPI.Compone
}
for (key, newParameter) in newComponents.parameters {
let oldParameter = try XCTUnwrap(oldComponents.parameters[key])
assertEqualNewToOld(newParameter, oldParameter)
try assertEqualNewToOld(newParameter, oldParameter)
}
for (key, newExample) in newComponents.examples {
let oldExample = try XCTUnwrap(oldComponents.examples[key])
Expand Down

0 comments on commit b1d7ed3

Please sign in to comment.