From 704de33aea5001cbbce9cdb1e6c1ce9326de4634 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Mon, 20 Nov 2023 10:53:13 -0600 Subject: [PATCH] simply ensure that jsonSchemaDialect at document root and the $schema keyword within JSON Schemas do not fail to parse --- Sources/OpenAPIKit/Document/Document.swift | 6 ++++ .../Document/DocumentTests.swift | 36 +++++++++++++++++++ .../Schema Object/JSONSchemaTests.swift | 14 ++++++++ 3 files changed, 56 insertions(+) diff --git a/Sources/OpenAPIKit/Document/Document.swift b/Sources/OpenAPIKit/Document/Document.swift index 763757af7..d9c293429 100644 --- a/Sources/OpenAPIKit/Document/Document.swift +++ b/Sources/OpenAPIKit/Document/Document.swift @@ -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 @@ -473,6 +474,7 @@ extension OpenAPI.Document { return [ .openAPIVersion, .info, + .jsonSchemaDialect, .servers, .paths, .webhooks, @@ -493,6 +495,8 @@ extension OpenAPI.Document { self = .openAPIVersion case "info": self = .info + case "jsonSchemaDialect": + self = .jsonSchemaDialect case "servers": self = .servers case "paths": @@ -518,6 +522,8 @@ extension OpenAPI.Document { return "openapi" case .info: return "info" + case .jsonSchemaDialect: + return "jsonSchemaDialect" case .servers: return "servers" case .paths: diff --git a/Tests/OpenAPIKitTests/Document/DocumentTests.swift b/Tests/OpenAPIKitTests/Document/DocumentTests.swift index 013ce03dc..c68703e8d 100644 --- a/Tests/OpenAPIKitTests/Document/DocumentTests.swift +++ b/Tests/OpenAPIKitTests/Document/DocumentTests.swift @@ -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 diff --git a/Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift b/Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift index e2fca0dd0..50a83e465 100644 --- a/Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift +++ b/Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift @@ -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