Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/3_0' into feature/193/op…
Browse files Browse the repository at this point in the history
…tional-responses
  • Loading branch information
mattpolzin committed Sep 4, 2023
2 parents 805bf46 + ccdcb64 commit 41d94a2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
13 changes: 10 additions & 3 deletions Sources/OpenAPIKitCompat/Compat30To31.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,16 @@ extension OpenAPIKit30.OpenAPI.Server: To31 {
/// to facilitate incremental migration within your codebase from OpenAPIKit30 to OpenAPIKit.
public func to31() -> OpenAPIKit.OpenAPI.Server {

let newVariables = variables.mapValues { variable in
OpenAPIKit.OpenAPI.Server.Variable(
enum: variable.enum,
let newVariables: OrderedDictionary<String, OpenAPIKit.OpenAPI.Server.Variable> = variables.mapValues { variable in
let enumValue: [String]?
if !variable.enum.isEmpty {
enumValue = variable.enum
} else {
enumValue = nil
}

return OpenAPIKit.OpenAPI.Server.Variable(
enum: enumValue,
default: variable.default,
description: variable.description,
vendorExtensions: variable.vendorExtensions
Expand Down
93 changes: 65 additions & 28 deletions Tests/OpenAPIKitCompatTests/DocumentConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ final class DocumentConversionTests: XCTestCase {

try assertEqualNewToOld(newDoc, oldDoc)
XCTAssertEqual(newDoc.openAPIVersion, .v3_1_0)

try newDoc.validate()
}

func test_vendorExtensionsOnDoc() throws {
Expand All @@ -39,6 +41,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

func test_fullInfo() throws {
Expand All @@ -60,6 +64,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

func test_servers() throws {
Expand All @@ -77,6 +83,13 @@ final class DocumentConversionTests: XCTestCase {
description: "hi",
variables: ["hello": .init(enum: ["1"], default: "1", description: "described", vendorExtensions: ["x-hi": "hello"])],
vendorExtensions: ["x-test": 2]
),
.init(
urlTemplate: try .init(templateString: "{protocol}://{hostname}/api/v3"),
variables: [
"protocol": .init(default: "http", description: "protocol"),
"hostname": .init(default: "HOSTNAME", description: "host name")
]
)
],
paths: [:],
Expand All @@ -86,6 +99,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

func test_paths() throws {
Expand Down Expand Up @@ -132,23 +147,25 @@ final class DocumentConversionTests: XCTestCase {
.component(named: "security"): ["hello"]
]

let operation = OpenAPIKit30.OpenAPI.Operation(
tags: ["hello"],
summary: "sum",
description: "described",
externalDocs: externalDocs,
operationId: "ident",
parameters: params,
requestBody: .request(request),
responses: [200: .b(response)],
callbacks: [
"callback": .b(callbacks),
"other_callback": .a(.component(named: "other_callback"))],
deprecated: true,
security: [securityRequirement],
servers: [server],
vendorExtensions: ["x-hello": 101]
)
let operation = (0...7).map { idx in
OpenAPIKit30.OpenAPI.Operation(
tags: ["hello"],
summary: "sum",
description: "described",
externalDocs: externalDocs,
operationId: "ident\(idx)",
parameters: params,
requestBody: .request(request),
responses: [200: .b(response)],
callbacks: [
"callback": .b(callbacks),
"other_callback": .a(.component(named: "other_callback"))],
deprecated: true,
security: [securityRequirement],
servers: [server],
vendorExtensions: ["x-hello": 101]
)
}

let oldDoc = OpenAPIKit30.OpenAPI.Document(
info: .init(title: "Hello", version: "1.0.0"),
Expand All @@ -170,23 +187,29 @@ final class DocumentConversionTests: XCTestCase {
.a(.internal(.component(name: "test"))),
.parameter(.init(name: "test", context: .query, schema: .string))
],
get: operation,
put: operation,
post: operation,
delete: operation,
options: operation,
head: operation,
patch: operation,
trace: operation,
get: operation[0],
put: operation[1],
post: operation[2],
delete: operation[3],
options: operation[4],
head: operation[5],
patch: operation[6],
trace: operation[7],
vendorExtensions: ["x-test": 123]
)
],
components: .noComponents
components: .init(
parameters: [
"test": .init(name: "referencedParam", context: .query, schema: .string)
]
)
)

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

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

func testJSONSchemas() throws {
Expand Down Expand Up @@ -227,7 +250,8 @@ final class DocumentConversionTests: XCTestCase {

let components = OpenAPIKit30.OpenAPI.Components(
schemas: [
"schema1": .string
"schema1": .string,
"test3_param": .integer(format: .int32, required: false)
],
responses: [
"response1": response
Expand Down Expand Up @@ -293,6 +317,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

func testSecurity() throws {
Expand Down Expand Up @@ -323,6 +349,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc2 = oldDoc2.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc2, oldDoc2)

try newDoc.validate()
}

func testTags() throws {
Expand Down Expand Up @@ -364,6 +392,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc3 = oldDoc3.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc3, oldDoc3)

try newDoc.validate()
}

func testExternalDocs() throws {
Expand Down Expand Up @@ -398,6 +428,8 @@ final class DocumentConversionTests: XCTestCase {
let newDoc = oldDoc.convert(to: .v3_1_0)

try assertEqualNewToOld(newDoc, oldDoc)

try newDoc.validate()
}

// TODO: more tests
Expand Down Expand Up @@ -479,10 +511,15 @@ fileprivate func assertEqualNewToOld(_ newServer: OpenAPIKit.OpenAPI.Server?, _
XCTAssertEqual(newServer.urlTemplate, oldServer.urlTemplate)
XCTAssertEqual(newServer.description, oldServer.description)
XCTAssertEqual(newServer.vendorExtensions, oldServer.vendorExtensions)
XCTAssertEqual(newServer.variables.count, oldServer.variables.count)
for (key, newVariable) in newServer.variables {
let oldVariable = oldServer.variables[key]
XCTAssertEqual(newVariable.description, oldVariable?.description)
XCTAssertEqual(newVariable.`enum`, oldVariable?.`enum`)
if (oldVariable?.enum ?? []).isEmpty {
XCTAssertNil(newVariable.`enum`)
} else {
XCTAssertEqual(newVariable.`enum`, oldVariable?.`enum`)
}
XCTAssertEqual(newVariable.`default`, oldVariable?.`default`)
XCTAssertEqual(newVariable.vendorExtensions, oldVariable?.vendorExtensions)
}
Expand Down

0 comments on commit 41d94a2

Please sign in to comment.