Repository navigation
2.0.0 - Much Love for Schemas
Features & Additions
New schema validation
New optional schema validation (not included in validation by default): .schemaComponentsAreDefined. Validates that all schemas are at least minimally defined (i.e. none of the JSON Schema definitions are just an empty object).
LocallyDereferenceable
Most OpenAPI types are LocallyDereferenceable now, which means they offer a dereferenced(in:) method that takes an OpenAPI.Components and results in a new type that is guaranteed to not contain any JSONReferences. This is the same behavior exposed on OpenAPI.Document via its locallyDereferenced() method.
Schema simplification
DereferencedJSONSchema has gained the simplified() method which will attempt to simplify compound schemas. The same is exposed via JSONSchema's simplified(given:) method which takes OpenAPI.Components.
Simplification is in its early stages right now and does not support all possible schemas but it works well on all(of:) schemas already.
Bug Fixes
OpenAPI.Server'surlused to not be able to handle server URLs with variables in them. This has been fixed in the newurlTemplateproperty (see breaking changes and the migration guide for more information).JSONSchemacan express a number of valid schemas it previously could not -- all of its cases support the core set of properties (see breaking changes and the migratio guide for more information).OpenAPI.Content'sschemaproperty has become optional, as indicated by the OpenAPI Specification (under Media Type Object).
Breaking Changes
The following list enumerates the breaking changes but the migration guide serves as a better resource for identifying the changes needed in your codebase (if any).
OpenAPI.Serverloses itsurlproperty in favor of a more correcturlTemplateproperty that can in turn be asked for a FoundationURL(but it will only produce one if the URL in question has no variables in it).OpenAPI.Content'sschemaproperty has become optional to support valid OpenAPI documentation where the Media Type Object has no schema.JSONSchemaFragmenthas been removed -- you can now useJSONSchemato represent all of the same schema fragments. Relatedly,JSONSchema'sundefinedcase has been renamedfragmentand it has gained a fullCoreContext.DereferencedJSONSchema'sunderlyingJSONSchemaproperty has been renamed tojsonSchema.- The
JSONSchemageneralContextproperty andContexttype have been renamed tocoreContextandCoreContext.DereferencedJSONSchemahas also renamed itsContexttoCoreContext. JSONSchema'sall(of:),any(of:), andone(of:)cases had theirdiscriminatorassociated value replaced with a fullCoreContext(which in turn has adiscriminator).JSONSchema'snotcase gained aCoreContext.- Some additional properties of the
JSONSchema.CoreContext(and its other contexts) have become optional to support a broader range of valid schemas. In practice, this is mostly non-breaking because the accessors remain non-optional even where the underlying type has changed but the initializers now take optional input to support the new expressivity. JSONSchema'sdereferencedSchemaObject()anddereferencedSchemaObject(resolvingIn:)renamed todereferenced()anddereferenced(in:).OpenAPI.Componentsdereferencing and lookup methods have been renamed to better express the differences between "looking a resource up" and "dereferencing a resource." See the migration guide for more information.
Improvements in-depth
Dereferencing
In OpenAPIKit v1, the OpenAPI.Components type offered the methods dereference(_:) and forceDereference(_:) to perform lookup of components by their references. These were overloaded to allow looking up Either types representing either a reference to a component or the component itself.
In OpenAPIKit v1.4, true dereferencing was introduced. True dereferencing does not just turn a reference into the value it refers to, it removes references iteratively for all properties of the given value. That made the use of the word "dereference" in the OpenAPI.Components type's methods misleading -- these methods "looked up" values but did not "dereference" them.
OpenAPIKit v2 fixes this confusing naming by supporting component lookup via subscript (non-throwing) and lookup(_:) (throwing) methods and not offering any methods that truly dereference types. At the same time, OpenAPIKit v2 adds the dereferenced(in:) method to most OpenAPI types. This new method takes an OpenAPI.Components value and returns a fully dereferenced version of self. The dereferenced(in:) method offers the same iterative dereferencing behavior exposed by the OpenAPI.Document locallyDereferenced() method that was added in OpenAPIKit v1.4.
Simplifying all(of:) schemas
The JSONSchema all(of:) case is unique amongst the combination cases because all of the schema fragments under it can often effectively be merged into a new schema. This is what "simplifying" that schema means in OpenAPIKit. The result of simplifying an all(of:) schema is a DereferencedJSONSchema.
Simplification is performed by JSONSchema's simplified(given:) method (which takes an OpenAPI.Components value) or the DereferencedJSONSchema simplified() method.
let schemaData = """
{
"allOf": [
{
"type": "object",
"description": "A person",
"required": [ "name" ],
"properties": {
"name": { "type": "string" }
}
},
{
"type": "object",
"properties": {
"favoriteColor": {
"type": "string",
"enum": [ "red", "green", "blue" ]
}
}
}
]
}
""".data(using: .utf8)!
let personSchema = try JSONDecoder().decode(JSONSchema .self, from: schemaData)
.simplified(given: .noComponents)
// results in simplified schema equivalent to:
let personSchemaInCode = JSONSchema.object(
description: "A person",
properties: [
"name": .string,
"favoriteColor": try JSONSchema.string(required: false, allowedValues: "red", "green", "blue")
]
)Default schema optionality
In OpenAPIKit v1, schemas created in-code defaulted to required: true. While decoding, however, they would default to required: false and then if a JSON Schema .object had a "required" array containing a certain property, that property's required boolean would get flipped to true. This is somewhat intuitive at face value, but it has the unintuitive side effect of all root schema components (i.e. a JSON Schema that does not live within another .object) having required: false because there is no parent "required" array to cause OpenAPIKit to flip its required boolean.
Again, this has no effect on the accuracy of encoding/decoding because the required boolean of a JSONSchema value is only encoded as part of a parent schema's "required" array.
OpenAPIKit v2 swaps this decoding default to required: true and instead flips the boolean to false for all properties not found in a parent object's "required" array. This approach has the same effect upon encoding/decoding except for root schemas having required: true which both aligns better with the default in-code required boolean and also makes more intuitive sense.
let schemaData = """
{
"type": "object",
"required": [
"test"
],
"properties": {
"test": {
"type": "string"
}
}
}
""".data(using: .utf8)!
//
// OpenAPIKit v1
//
let schema1 = try JSONDecoder().decode(JSONSchema.self, from: schemaData)
// results in:
let schema1InCode = JSONSchema.object(
required: false, // <-- note that this is unintuitive even though it has no effect on the correctness of the schema when encoded.
properties: [
"test": .string(required: true) // <-- note that this `required: true` could be omitted, it is the default for in-code construction.
]
)
//
// OpenAPIKit v2
//
let schema2 = try JSONDecoder().decode(JSONSchema.self, from: schemaData)
// results in:
let schema2InCode = JSONSchema.object(
required: true, // <-- note that this `required: true` could be omitted, it is the default for in-code construction.
properties: [
"test": .string(required: true) // <-- note that this `required: true` could be omitted, it is the default for in-code construction.
]
)