Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON Schema $defs encoding & decoding #376

Merged
merged 5 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 99 additions & 34 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,38 @@ public struct JSONSchema: JSONSchemaContext, HasWarnings {

}

// See `JSONSchemaContext`
public var defs: OrderedDictionary<String, JSONSchema> {
switch value {
case .null(let core):
return core.defs
case .boolean(let core):
return core.defs
case .number(let core, _):
return core.defs
case .integer(let core, _):
return core.defs
case .string(let core, _):
return core.defs
case .object(let core, _):
return core.defs
case .array(let core, _):
return core.defs
case .all(of: _, core: let core):
return core.defs
case .one(of: _, core: let core):
return core.defs
case .any(of: _, core: let core):
return core.defs
case .not(_, core: let core):
return core.defs
case .reference(_, let core):
return core.defs
case .fragment(let core):
return core.defs
}
}

// See `JSONSchemaContext`
public var inferred: Bool {
return coreContext.inferred
Expand Down Expand Up @@ -1086,7 +1118,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.BooleanFormat>(
format: format,
Expand All @@ -1102,7 +1135,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
return .boolean(context)
}
Expand All @@ -1123,7 +1157,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .boolean(
format: format,
Expand All @@ -1139,7 +1174,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
}

Expand All @@ -1164,7 +1200,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.AnyFormat>(
format: format,
Expand All @@ -1180,7 +1217,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
return .fragment(context)
}
Expand All @@ -1201,7 +1239,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .fragment(
format: format,
Expand All @@ -1217,7 +1256,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
}

Expand Down Expand Up @@ -1250,7 +1290,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.StringFormat>(
format: format,
Expand All @@ -1266,7 +1307,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
let stringContext = JSONSchema.StringContext(
maxLength: maxLength,
Expand Down Expand Up @@ -1299,7 +1341,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .string(
format: format,
Expand All @@ -1320,7 +1363,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
}

Expand Down Expand Up @@ -1348,7 +1392,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.NumberFormat>(
format: format,
Expand All @@ -1364,7 +1409,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
let numbericContext = JSONSchema.NumericContext(
multipleOf: multipleOf,
Expand Down Expand Up @@ -1393,7 +1439,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .number(
format: format,
Expand All @@ -1412,7 +1459,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
}

Expand Down Expand Up @@ -1440,7 +1488,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.IntegerFormat>(
format: format,
Expand All @@ -1456,7 +1505,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
let integerContext = JSONSchema.IntegerContext(
multipleOf: multipleOf,
Expand Down Expand Up @@ -1485,7 +1535,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .integer(
format: format,
Expand All @@ -1504,7 +1555,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
}

Expand Down Expand Up @@ -1533,7 +1585,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ObjectFormat>(
format: format,
Expand All @@ -1549,7 +1602,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
let objectContext = JSONSchema.ObjectContext(
properties: properties,
Expand Down Expand Up @@ -1585,7 +1639,8 @@ extension JSONSchema {
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ArrayFormat>(
format: format,
Expand All @@ -1601,7 +1656,8 @@ extension JSONSchema {
defaultValue: defaultValue,
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)

let arrayContext = JSONSchema.ArrayContext(
Expand Down Expand Up @@ -1635,7 +1691,8 @@ extension JSONSchema {
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .all(
of: schemas,
Expand All @@ -1645,7 +1702,8 @@ extension JSONSchema {
description: description,
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
)
}
Expand All @@ -1667,7 +1725,8 @@ extension JSONSchema {
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .one(
of: schemas,
Expand All @@ -1677,7 +1736,8 @@ extension JSONSchema {
description: description,
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
)
}
Expand All @@ -1699,7 +1759,8 @@ extension JSONSchema {
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .any(
of: schemas,
Expand All @@ -1709,7 +1770,8 @@ extension JSONSchema {
description: description,
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
)
}
Expand All @@ -1728,7 +1790,8 @@ extension JSONSchema {
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .not(
schema,
Expand All @@ -1738,7 +1801,8 @@ extension JSONSchema {
description: description,
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
dynamicAnchor: dynamicAnchor,
defs: defs
)
)
}
Expand All @@ -1750,11 +1814,12 @@ extension JSONSchema {
title: String? = nil,
description: String? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
dynamicAnchor: String? = nil,
defs: OrderedDictionary<String, JSONSchema> = [:]
) -> JSONSchema {
return .reference(
reference,
.init(required: required, title: title, description: description, anchor: anchor, dynamicAnchor: dynamicAnchor)
.init(required: required, title: title, description: description, anchor: anchor, dynamicAnchor: dynamicAnchor, defs: defs)
)
}
}
Expand Down
Loading
Loading