From f10ae3ddf6fd0d7c5af303f26e5283b7ae4e0f21 Mon Sep 17 00:00:00 2001 From: Shannon Young Date: Tue, 18 Apr 2023 13:18:44 -0700 Subject: [PATCH] =?UTF-8?q?Only=20include=20=E2=80=9CadditionalProperties?= =?UTF-8?q?=E2=80=9D=20if=20explicitly=20defined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/JsonModel/Documentable.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/JsonModel/Documentable.swift b/Sources/JsonModel/Documentable.swift index b03c648..a3449b0 100644 --- a/Sources/JsonModel/Documentable.swift +++ b/Sources/JsonModel/Documentable.swift @@ -112,6 +112,14 @@ public protocol DocumentableObject : DocumentableBase { static func jsonExamples() throws -> [[String : JsonSerializable]] } +/// A protocol that allows a JsonSchema to define "additionalProperties" explicitly. +/// Any serializable object that may be serialized using other languages that have +/// a different set of required properties (for example, Kotlin serialization) should +/// not set `additionalProperties == false`. +public protocol FinalDocumentableObject : DocumentableStruct { + static var additionalProperties: Bool { get } +} + /// Structs that implement the Codable protocol. public protocol DocumentableStruct : DocumentableObject, Codable { static func examples() -> [Self] @@ -509,11 +517,10 @@ public class JsonDocumentBuilder { let examples = try (docType as? DocumentableObject.Type).map { try $0.jsonExamples() } - let isOpen = (docType as? DocumentableObject.Type)?.isOpen() ?? !rootPointer.isSealed return JsonSchema(id: URL(string: rootPointer.refId.classPath)!, description: rootPointer.documentDescription ?? "", isArray: rootPointer.isArray, - additionalProperties: (isOpen || interfaces.count > 0) ? nil : false, + additionalProperties: (docType as? FinalDocumentableObject.Type)?.additionalProperties, codingKeys: docType.codingKeys(), interfaces: interfaces.count > 0 ? interfaces : nil, definitions: definitions, @@ -776,7 +783,7 @@ public class JsonDocumentBuilder { return JsonSchemaObjectRef(ref: refId) } return .object(JsonSchemaObject(id: ref, - additionalProperties: (docType.isOpen() || interfaces.count > 0) ? nil : false, + additionalProperties: (docType as? FinalDocumentableObject.Type)?.additionalProperties, description: "", codingKeys: docType.codingKeys(), properties: properties,