Skip to content

Commit

Permalink
Merge pull request #165 from mattpolzin/feature/164/default-schema-pr…
Browse files Browse the repository at this point in the history
…operty

Add default property to schema object
  • Loading branch information
mattpolzin authored Dec 20, 2020
2 parents 0216f74 + ece5d49 commit 96e85ad
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Sources/OpenAPIKit/Schema Object/DereferencedJSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public enum DereferencedJSONSchema: Equatable, JSONSchemaContext {
// See `JSONSchemaContext`
public var allowedValues: [AnyCodable]? { jsonSchema.allowedValues }

// See `JSONSchemaContext`
public var defaultValue: AnyCodable? { jsonSchema.defaultValue }

// See `JSONSchemaContext`
public var example: AnyCodable? { jsonSchema.example }

Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenAPIKit/Schema Object/JSONSchema+Combining.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ extension JSONSchema.CoreContext where Format == JSONTypeFormat.AnyFormat {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
return try transformedContext.combined(with: other)
Expand Down Expand Up @@ -331,6 +332,7 @@ extension JSONSchema.CoreContext {
)
}
let newAllowedValues = allowedValues ?? other.allowedValues
let newDefaultValue = defaultValue ?? other.defaultValue

if let conflict = conflicting(example, other.example) {
throw JSONSchemaResolutionError(.attributeConflict(jsonType: nil, name: "example", original: String(describing: conflict.0), new: String(describing: conflict.1)))
Expand All @@ -349,6 +351,7 @@ extension JSONSchema.CoreContext {
discriminator: newDiscriminator,
externalDocs: newExternalDocs,
allowedValues: newAllowedValues,
defaultValue: newDefaultValue,
example: newExample
)
}
Expand Down Expand Up @@ -557,6 +560,7 @@ extension JSONSchema.CoreContext {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand Down
59 changes: 59 additions & 0 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public enum JSONSchema: Equatable, JSONSchemaContext {
return coreContext?.allowedValues
}

// See `JSONSchemaContext`
public var defaultValue: AnyCodable? {
return coreContext?.defaultValue
}

// See `JSONSchemaContext`
public var example: AnyCodable? {
return coreContext?.example
Expand Down Expand Up @@ -423,6 +428,36 @@ extension JSONSchema {
}
}

/// Return a version of this `JSONSchema` that has the given default value.
public func with(defaultValue: AnyCodable) -> JSONSchema {
switch self {
case .boolean(let context):
return .boolean(context.with(defaultValue: defaultValue))
case .object(let contextA, let contextB):
return .object(contextA.with(defaultValue: defaultValue), contextB)
case .array(let contextA, let contextB):
return .array(contextA.with(defaultValue: defaultValue), contextB)
case .number(let context, let contextB):
return .number(context.with(defaultValue: defaultValue), contextB)
case .integer(let context, let contextB):
return .integer(context.with(defaultValue: defaultValue), contextB)
case .string(let context, let contextB):
return .string(context.with(defaultValue: defaultValue), contextB)
case .fragment(let context):
return .fragment(context.with(defaultValue: defaultValue))
case .all(of: let fragments, core: let core):
return .all(of: fragments, core: core.with(defaultValue: defaultValue))
case .one(of: let schemas, core: let core):
return .one(of: schemas, core: core.with(defaultValue: defaultValue))
case .any(of: let schemas, core: let core):
return .any(of: schemas, core: core.with(defaultValue: defaultValue))
case .not(let schema, core: let core):
return .not(schema, core: core.with(defaultValue: defaultValue))
case .reference:
return self
}
}

/// Returns a version of this `JSONSchema` that has the given example
/// attached.
public func with(example: AnyCodable) throws -> JSONSchema {
Expand Down Expand Up @@ -536,6 +571,7 @@ extension JSONSchema {
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.BooleanFormat>(
Expand All @@ -549,6 +585,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
return .boolean(context)
Expand All @@ -567,6 +604,7 @@ extension JSONSchema {
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
return .boolean(
Expand All @@ -580,6 +618,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand All @@ -602,6 +641,7 @@ extension JSONSchema {
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let context = JSONSchema.CoreContext<JSONTypeFormat.AnyFormat>(
Expand All @@ -615,6 +655,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
return .fragment(context)
Expand All @@ -633,6 +674,7 @@ extension JSONSchema {
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
return .fragment(
Expand All @@ -646,6 +688,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand Down Expand Up @@ -674,6 +717,7 @@ extension JSONSchema {
maxLength: Int? = nil,
pattern: String? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.StringFormat>(
Expand All @@ -687,6 +731,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
let stringContext = JSONSchema.StringContext(
Expand All @@ -713,6 +758,7 @@ extension JSONSchema {
maxLength: Int? = nil,
pattern: String? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
return .string(
Expand All @@ -729,6 +775,7 @@ extension JSONSchema {
maxLength: maxLength,
pattern: pattern,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand All @@ -754,6 +801,7 @@ extension JSONSchema {
maximum: (Double, exclusive: Bool)? = nil,
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.NumberFormat>(
Expand All @@ -767,6 +815,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
let numbericContext = JSONSchema.NumericContext(
Expand All @@ -793,6 +842,7 @@ extension JSONSchema {
maximum: (Double, exclusive: Bool)? = nil,
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
return .number(
Expand All @@ -809,6 +859,7 @@ extension JSONSchema {
maximum: maximum,
minimum: minimum,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand All @@ -834,6 +885,7 @@ extension JSONSchema {
maximum: (Int, exclusive: Bool)? = nil,
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let genericContext = JSONSchema.CoreContext<JSONTypeFormat.IntegerFormat>(
Expand All @@ -847,6 +899,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
let integerContext = JSONSchema.IntegerContext(
Expand All @@ -873,6 +926,7 @@ extension JSONSchema {
maximum: (Int, exclusive: Bool)? = nil,
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
return .integer(
Expand All @@ -889,6 +943,7 @@ extension JSONSchema {
maximum: maximum,
minimum: minimum,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
}
Expand All @@ -915,6 +970,7 @@ extension JSONSchema {
properties: [String: JSONSchema] = [:],
additionalProperties: Either<Bool, JSONSchema>? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ObjectFormat>(
Expand All @@ -928,6 +984,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)
let objectContext = JSONSchema.ObjectContext(
Expand Down Expand Up @@ -961,6 +1018,7 @@ extension JSONSchema {
uniqueItems: Bool? = nil,
items: JSONSchema? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema {
let coreContext = JSONSchema.CoreContext<JSONTypeFormat.ArrayFormat>(
Expand All @@ -974,6 +1032,7 @@ extension JSONSchema {
discriminator: discriminator,
externalDocs: externalDocs,
allowedValues: allowedValues,
defaultValue: defaultValue,
example: example
)

Expand Down
Loading

0 comments on commit 96e85ad

Please sign in to comment.