Skip to content

Commit

Permalink
Merge pull request #360 from mattpolzin/anchors
Browse files Browse the repository at this point in the history
Add JSONSchema support for anchors
  • Loading branch information
mattpolzin authored Mar 27, 2024
2 parents 6beeaa1 + 2caa771 commit 4a3d992
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 54 deletions.
6 changes: 6 additions & 0 deletions Sources/OpenAPIKit/Schema Object/DereferencedJSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public enum DereferencedJSONSchema: Equatable, JSONSchemaContext {
// See `JSONSchemaContext`
public var examples: [AnyCodable] { jsonSchema.examples }

// See `JSONSchemaContext`
public var anchor: String? { jsonSchema.anchor }

// See `JSONSchemaContext`
public var dynamicAnchor: String? { jsonSchema.dynamicAnchor }

// See `JSONSchemaContext`
public var inferred: Bool { jsonSchema.inferred }

Expand Down
148 changes: 114 additions & 34 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ public struct JSONSchema: JSONSchemaContext, HasWarnings {
return coreContext.examples
}

// See `JSONSchemaContext`
public var anchor: String? {
return coreContext.anchor
}

// See `JSONSchemaContext`
public var dynamicAnchor: String? {
return coreContext.dynamicAnchor

}

// See `JSONSchemaContext`
public var inferred: Bool {
return coreContext.inferred
Expand Down Expand Up @@ -1073,7 +1084,9 @@ extension JSONSchema {
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.BooleanFormat>(
format: format,
Expand All @@ -1087,7 +1100,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
return .boolean(context)
}
Expand All @@ -1106,7 +1121,9 @@ extension JSONSchema {
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .boolean(
format: format,
Expand All @@ -1120,7 +1137,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
}

Expand All @@ -1143,7 +1162,9 @@ extension JSONSchema {
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.AnyFormat>(
format: format,
Expand All @@ -1157,7 +1178,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
return .fragment(context)
}
Expand All @@ -1176,7 +1199,9 @@ extension JSONSchema {
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .fragment(
format: format,
Expand All @@ -1190,7 +1215,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
}

Expand Down Expand Up @@ -1221,7 +1248,9 @@ extension JSONSchema {
contentEncoding: OpenAPI.ContentEncoding? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.StringFormat>(
format: format,
Expand All @@ -1235,7 +1264,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
let stringContext = JSONSchema.StringContext(
maxLength: maxLength,
Expand Down Expand Up @@ -1266,7 +1297,9 @@ extension JSONSchema {
contentEncoding: OpenAPI.ContentEncoding? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .string(
format: format,
Expand All @@ -1285,7 +1318,9 @@ extension JSONSchema {
contentEncoding: contentEncoding,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
}

Expand All @@ -1311,7 +1346,9 @@ extension JSONSchema {
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.NumberFormat>(
format: format,
Expand All @@ -1325,7 +1362,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
let numbericContext = JSONSchema.NumericContext(
multipleOf: multipleOf,
Expand All @@ -1352,7 +1391,9 @@ extension JSONSchema {
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .number(
format: format,
Expand All @@ -1369,7 +1410,9 @@ extension JSONSchema {
minimum: minimum,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
}

Expand All @@ -1395,7 +1438,9 @@ extension JSONSchema {
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.IntegerFormat>(
format: format,
Expand All @@ -1409,7 +1454,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
let integerContext = JSONSchema.IntegerContext(
multipleOf: multipleOf,
Expand All @@ -1436,7 +1483,9 @@ extension JSONSchema {
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .integer(
format: format,
Expand All @@ -1453,7 +1502,9 @@ extension JSONSchema {
minimum: minimum,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
}

Expand All @@ -1480,7 +1531,9 @@ extension JSONSchema {
additionalProperties: Either<Bool, JSONSchema>? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ObjectFormat>(
format: format,
Expand All @@ -1494,7 +1547,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
let objectContext = JSONSchema.ObjectContext(
properties: properties,
Expand Down Expand Up @@ -1528,7 +1583,9 @@ extension JSONSchema {
items: JSONSchema? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
examples: [AnyCodable] = []
examples: [AnyCodable] = [],
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ArrayFormat>(
format: format,
Expand All @@ -1542,7 +1599,9 @@ extension JSONSchema {
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
examples: examples
examples: examples,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)

let arrayContext = JSONSchema.ArrayContext(
Expand Down Expand Up @@ -1574,15 +1633,19 @@ extension JSONSchema {
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .all(
of: schemas,
core: .init(
required: required,
title: title,
description: description,
discriminator: discriminator
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
)
}
Expand All @@ -1602,15 +1665,19 @@ extension JSONSchema {
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .one(
of: schemas,
core: .init(
required: required,
title: title,
description: description,
discriminator: discriminator
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
)
}
Expand All @@ -1630,15 +1697,19 @@ extension JSONSchema {
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .any(
of: schemas,
core: .init(
required: required,
title: title,
description: description,
discriminator: discriminator
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
)
}
Expand All @@ -1655,15 +1726,19 @@ extension JSONSchema {
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
discriminator: OpenAPI.Discriminator? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .not(
schema,
core: .init(
required: required,
title: title,
description: description,
discriminator: discriminator
discriminator: discriminator,
anchor: anchor,
dynamicAnchor: dynamicAnchor
)
)
}
Expand All @@ -1673,9 +1748,14 @@ extension JSONSchema {
_ reference: JSONReference<JSONSchema>,
required: Bool = true,
title: String? = nil,
description: String? = nil
description: String? = nil,
anchor: String? = nil,
dynamicAnchor: String? = nil
) -> JSONSchema {
return .reference(reference, .init(required: required, title: title, description: description))
return .reference(
reference,
.init(required: required, title: title, description: description, anchor: anchor, dynamicAnchor: dynamicAnchor)
)
}
}

Expand Down
Loading

0 comments on commit 4a3d992

Please sign in to comment.