Skip to content

Commit

Permalink
Merge pull request #349 from mattpolzin/dont-fail-for-schema-dialects
Browse files Browse the repository at this point in the history
simply ensure that jsonSchemaDialect at document root and the $schema keyword within JSON Schemas do not fail to parse
  • Loading branch information
mattpolzin authored Nov 20, 2023
2 parents b1d7ed3 + 704de33 commit b00b08d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/OpenAPIKit/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ extension OpenAPI.Document {
internal enum CodingKeys: ExtendableCodingKey {
case openAPIVersion
case info
case jsonSchemaDialect // TODO: implement parsing (https://github.com/mattpolzin/OpenAPIKit/issues/202)
case servers
case paths
case webhooks
Expand All @@ -473,6 +474,7 @@ extension OpenAPI.Document {
return [
.openAPIVersion,
.info,
.jsonSchemaDialect,
.servers,
.paths,
.webhooks,
Expand All @@ -493,6 +495,8 @@ extension OpenAPI.Document {
self = .openAPIVersion
case "info":
self = .info
case "jsonSchemaDialect":
self = .jsonSchemaDialect
case "servers":
self = .servers
case "paths":
Expand All @@ -518,6 +522,8 @@ extension OpenAPI.Document {
return "openapi"
case .info:
return "info"
case .jsonSchemaDialect:
return "jsonSchemaDialect"
case .servers:
return "servers"
case .paths:
Expand Down
36 changes: 36 additions & 0 deletions Tests/OpenAPIKitTests/Document/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,42 @@ extension DocumentTests {
)
)
}

func test_jsonSchemaDialect_encode() throws {
// TODO: once implemented (https://github.com/mattpolzin/OpenAPIKit/issues/202)
}

func test_jsonSchemaDialect_decode() throws {
let documentData =
"""
{
"externalDocs" : {
"url" : "http:\\/\\/google.com"
},
"info" : {
"title" : "API",
"version" : "1.0"
},
"openapi" : "3.1.0",
"paths" : {
},
"jsonSchemaDialect" : "http://json-schema.org/draft/2020-12/schema"
}
""".data(using: .utf8)!
let document = try orderUnstableDecode(OpenAPI.Document.self, from: documentData)

XCTAssertEqual(
document,
OpenAPI.Document(
info: .init(title: "API", version: "1.0"),
servers: [],
paths: [:],
components: .noComponents,
externalDocs: .init(url: URL(string: "http://google.com")!)
)
)
}
}

// MARK: - Webhooks
Expand Down
14 changes: 14 additions & 0 deletions Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,20 @@ extension SchemaObjectTests {
XCTAssertEqual(warnResult.value, .object(.init(), .init(properties: [:])))
}

func test_decodeAllowsSchemaKeyword() {
// a weak check that things at least don't fail when a valid $schema property exists.
let schemaSchema = """
{
"$schema" : "http://json-schema.org/draft/2020-12/schema"
}
""".data(using: .utf8)!

XCTAssertEqual(
try orderUnstableDecode(JSONSchema.self, from: schemaSchema),
.fragment(.init())
)
}

func test_decodeExampleFragment() throws {
// This way of specifying an example is deprecated in favor of
// the examples property (so the encoding of this does not turn
Expand Down

0 comments on commit b00b08d

Please sign in to comment.