Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from Sage-Bionetworks/syoung/additional-proper…
Browse files Browse the repository at this point in the history
…ties

Only include “additionalProperties” if explicitly defined
  • Loading branch information
syoung-smallwisdom authored Apr 18, 2023
2 parents 2f8ebd4 + f10ae3d commit 78a329a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Sources/JsonModel/Documentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 78a329a

Please sign in to comment.