Skip to content

Commit

Permalink
Merge pull request #246 from kean/kean/properties-hint-2
Browse files Browse the repository at this point in the history
Add a hint for allowed values types
  • Loading branch information
mattpolzin authored Dec 22, 2021
2 parents 79ca11e + f2f8af0 commit 6316f4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ extension JSONSchema.CoreContext: Decodable {

format = try container.decodeIfPresent(Format.self, forKey: .format) ?? .unspecified

let nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable)
_nullable = nullable

// default to `true` at decoding site.
// It is the responsibility of decoders farther upstream
// to mark this as _not_ required if needed using
Expand All @@ -699,9 +702,16 @@ extension JSONSchema.CoreContext: Decodable {
description = try container.decodeIfPresent(String.self, forKey: .description)
discriminator = try container.decodeIfPresent(OpenAPI.Discriminator.self, forKey: .discriminator)
externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs)
allowedValues = try container.decodeIfPresent([AnyCodable].self, forKey: .allowedValues)
if Format.self == JSONTypeFormat.StringFormat.self {
if (nullable ?? false) {
allowedValues = try container.decodeIfPresent([String?].self, forKey: .allowedValues)?.map(AnyCodable.init)
} else {
allowedValues = try container.decodeIfPresent([String].self, forKey: .allowedValues)?.map(AnyCodable.init)
}
} else {
allowedValues = try container.decodeIfPresent([AnyCodable].self, forKey: .allowedValues)
}
defaultValue = try container.decodeIfPresent(AnyCodable.self, forKey: .defaultValue)
_nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable)

let readOnly = try container.decodeIfPresent(Bool.self, forKey: .readOnly)
let writeOnly = try container.decodeIfPresent(Bool.self, forKey: .writeOnly)
Expand Down
3 changes: 3 additions & 0 deletions Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4146,6 +4146,7 @@ extension SchemaObjectTests {
let deprecatedStringData = #"{"type": "string", "deprecated": true}"#.data(using: .utf8)!
let allowedValueStringData = #"{"type": "string", "enum": ["hello"]}"#.data(using: .utf8)!
let discriminatorStringData = #"{"type": "string", "discriminator": {"propertyName": "hello"}}"#.data(using: .utf8)!
let nullableStringWithAllowedValuesData = #"{"type": "string", "nullable": true, "enum": ["hello", null]}"#.data(using: .utf8)!

let string = try orderUnstableDecode(JSONSchema.self, from: stringData)
let nullableString = try orderUnstableDecode(JSONSchema.self, from: nullableStringData)
Expand All @@ -4154,6 +4155,7 @@ extension SchemaObjectTests {
let deprecatedString = try orderUnstableDecode(JSONSchema.self, from: deprecatedStringData)
let allowedValueString = try orderUnstableDecode(JSONSchema.self, from: allowedValueStringData)
let discriminatorString = try orderUnstableDecode(JSONSchema.self, from: discriminatorStringData)
let nullableStringWithAllowedValues = try orderUnstableDecode(JSONSchema.self, from: nullableStringWithAllowedValuesData)

XCTAssertEqual(string, JSONSchema.string(.init(format: .generic), .init()))
XCTAssertEqual(nullableString, JSONSchema.string(.init(format: .generic, nullable: true), .init()))
Expand All @@ -4162,6 +4164,7 @@ extension SchemaObjectTests {
XCTAssertEqual(deprecatedString, JSONSchema.string(.init(format: .generic, deprecated: true), .init()))
XCTAssertEqual(allowedValueString, JSONSchema.string(.init(format: .generic, allowedValues: ["hello"]), .init()))
XCTAssertEqual(discriminatorString, JSONSchema.string(discriminator: .init(propertyName: "hello")))
XCTAssertEqual(nullableStringWithAllowedValues, JSONSchema.string(nullable: true, allowedValues: ["hello", nil]))
}

func test_decodeStringWithTypeInferred() throws {
Expand Down

0 comments on commit 6316f4f

Please sign in to comment.