Skip to content

Commit

Permalink
Merge pull request #281 from mattpolzin/fix-simplified-all-of-schema-…
Browse files Browse the repository at this point in the history
…optionality

Fix simplified allOf schema optionality
  • Loading branch information
mattpolzin authored Jul 19, 2023
2 parents 70b9c43 + 1a379dd commit 7e3969e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ extension JSONSchema {
case .fragment(let context):
return .fragment(context.optionalContext())
case .all(of: let fragments, core: let core):
return .all(of: fragments, core: core.optionalContext())
return .all(of: fragments.map { $0.optionalSchemaObject() }, core: core.optionalContext())
case .one(of: let schemas, core: let core):
return .one(of: schemas, core: core.optionalContext())
case .any(of: let schemas, core: let core):
Expand Down Expand Up @@ -395,7 +395,7 @@ extension JSONSchema {
case .fragment(let context):
return .fragment(context.requiredContext())
case .all(of: let fragments, core: let core):
return .all(of: fragments, core: core.requiredContext())
return .all(of: fragments.map { $0.requiredSchemaObject() }, core: core.requiredContext())
case .one(of: let schemas, core: let core):
return .one(of: schemas, core: core.requiredContext())
case .any(of: let schemas, core: let core):
Expand Down
85 changes: 85 additions & 0 deletions Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,16 @@ final class SchemaObjectTests: XCTestCase {
XCTAssertFalse(not.required)
XCTAssertFalse(fragment.required)

// all fragments within required get flipped too:
switch(allOf) {
case .all(of: let schemas, core: _):
for schema in schemas {
XCTAssertFalse(schema.required)
}
default:
break
}

XCTAssertTrue(reference.required)
}

Expand Down Expand Up @@ -951,6 +961,16 @@ final class SchemaObjectTests: XCTestCase {
XCTAssertTrue(not.required)
XCTAssertTrue(reference.required)
XCTAssertTrue(fragment.required)

// all fragments within required get flipped too:
switch(allOf) {
case .all(of: let schemas, core: _):
for schema in schemas {
XCTAssertTrue(schema.required)
}
default:
break
}
}

func test_notNullableToNullable() {
Expand Down Expand Up @@ -4597,6 +4617,12 @@ extension SchemaObjectTests {
.reference(.component(named: "test"))
]
)
let allOfWithReferenceAndDescription = JSONSchema.all(
of: [
.fragment(description: "hello"),
.reference(.component(named: "test"))
]
)

testEncodingPropertyLines(entity: allOf, propertyLines: [
"\"allOf\" : [",
Expand Down Expand Up @@ -4660,6 +4686,17 @@ extension SchemaObjectTests {
" }",
"]"
])

testEncodingPropertyLines(entity: allOfWithReferenceAndDescription, propertyLines: [
"\"allOf\" : [",
" {",
" \"description\" : \"hello\"",
" },",
" {",
" \"$ref\" : \"#\\/components\\/schemas\\/test\"",
" }",
"]"
])
}

func test_decodeAll() throws {
Expand Down Expand Up @@ -4701,10 +4738,35 @@ extension SchemaObjectTests {
}
""".data(using: .utf8)!

let allWithReferenceAndDescriptionData = """
{
"allOf": [
{ "description": "hello" },
{ "$ref": "#/components/schemas/test" }
]
}
""".data(using: .utf8)!

let nestedOptionalAllData = """
{
"type": "object",
"properties": {
"prop1": {
"allOf": [
{ "description": "hello" },
{ "$ref": "#/components/schemas/test" }
]
}
}
}
""".data(using: .utf8)!

let all = try orderUnstableDecode(JSONSchema.self, from: allData)
let allWithTitle = try orderUnstableDecode(JSONSchema.self, from: allWithTitleData)
let allWithDiscriminator = try orderUnstableDecode(JSONSchema.self, from: allWithDiscriminatorData)
let allWithReference = try orderUnstableDecode(JSONSchema.self, from: allWithReferenceData)
let allWithReferenceAndDescription = try orderUnstableDecode(JSONSchema.self, from: allWithReferenceAndDescriptionData)
let nestedOptionalAll = try orderUnstableDecode(JSONSchema.self, from: nestedOptionalAllData)

XCTAssertEqual(
all,
Expand Down Expand Up @@ -4747,6 +4809,29 @@ extension SchemaObjectTests {
]
)
)

XCTAssertEqual(
allWithReferenceAndDescription,
JSONSchema.all(
of: [
.fragment(description: "hello"),
.reference(.component(named: "test"))
]
)
)

XCTAssertEqual(
nestedOptionalAll,
JSONSchema.object(
properties: [
"prop1": JSONSchema.all(
of: .fragment(required: false, description: "hello"),
.reference(.component(named: "test")),
required: false
)
]
)
)
}

func test_encodeOne() {
Expand Down

0 comments on commit 7e3969e

Please sign in to comment.