-
Notifications
You must be signed in to change notification settings - Fork 25
Change default optional null handling to accept null values #128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
87c3552
Add opt-in null acceptance for optional properties
ecito 250826b
Document optional null handling in Macros.md
ecito fd146e5
lint
ecito c484234
lint
ecito fdcc791
add tests
ecito bcc724b
fix the double optional issue
ecito e8801a8
Merge main into feature/optional-null-opt-in
ecito 78a5822
fix tests
ecito f440c18
Change default behavior to accept null for optional properties
ecito 3361065
remove serialized
ecito 466ae9f
Fix test expectations for default optionalNulls behavior
ecito e5b3679
fix test
ecito 6bbcfdf
fix default usage in some tests to avoid diagnostic warnings
ecito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Sources/JSONSchemaBuilder/JSONComponent/Modifier/OrNullModifier.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import JSONSchema | ||
|
|
||
| /// Style for handling null values in schemas | ||
| public enum OrNullStyle { | ||
| /// Uses type array: {"type": ["integer", "null"]} | ||
| /// Best for scalar primitives - produces clearer validation errors | ||
| case type | ||
|
|
||
| /// Uses oneOf composition: {"oneOf": [{"type": "integer"}, {"type": "null"}]} | ||
| /// Required for complex types (objects, arrays, refs) | ||
| case union | ||
| } | ||
|
|
||
| extension JSONSchemaComponent { | ||
| /// Makes this component accept null values in addition to the component's type. | ||
| /// Returns nil when null is encountered. | ||
| /// | ||
| /// - Parameter style: The style to use for null acceptance | ||
| /// - `.type`: Uses type array `["integer", "null"]` - best for primitives | ||
| /// - `.union`: Uses oneOf composition - required for complex types | ||
| /// | ||
| /// - Returns: A component that accepts either the original type or null, returning an optional value | ||
| /// | ||
| /// Example: | ||
| /// ```swift | ||
| /// JSONInteger() | ||
| /// .orNull(style: .type) // Accepts integers or null, returns Int? | ||
| /// ``` | ||
| public func orNull(style: OrNullStyle) -> JSONComponents.AnySchemaComponent<Output?> { | ||
| switch style { | ||
| case .type: | ||
| return OrNullTypeComponent<Output, Self>(wrapped: self).eraseToAnySchemaComponent() | ||
| case .union: | ||
| return OrNullUnionComponent<Output, Self>(wrapped: self).eraseToAnySchemaComponent() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Implementation using type array | ||
| private struct OrNullTypeComponent<WrappedValue, Wrapped: JSONSchemaComponent>: JSONSchemaComponent | ||
| where Wrapped.Output == WrappedValue { | ||
| typealias Output = WrappedValue? | ||
|
|
||
| var wrapped: Wrapped | ||
|
|
||
| public var schemaValue: SchemaValue { | ||
| get { | ||
| var schema = wrapped.schemaValue | ||
|
|
||
| // If there's already a type keyword, convert it to an array with null | ||
| if case .object(var obj) = schema, | ||
| let typeValue = obj[Keywords.TypeKeyword.name] | ||
| { | ||
|
|
||
| // Convert single type to array with null | ||
| switch typeValue { | ||
| case .string(let typeStr): | ||
| obj[Keywords.TypeKeyword.name] = .array([ | ||
| .string(typeStr), .string(JSONType.null.rawValue), | ||
| ]) | ||
| case .array(var types): | ||
| // Add null if not already present | ||
| let nullValue = JSONValue.string(JSONType.null.rawValue) | ||
| if !types.contains(nullValue) { | ||
| types.append(nullValue) | ||
| } | ||
| obj[Keywords.TypeKeyword.name] = .array(types) | ||
| default: | ||
| break | ||
| } | ||
|
|
||
| schema = .object(obj) | ||
| } | ||
|
|
||
| return schema | ||
| } | ||
| set { | ||
| // Not implemented - this modifier doesn't support schema value mutation | ||
| } | ||
| } | ||
|
|
||
| public func parse(_ value: JSONValue) -> Parsed<WrappedValue?, ParseIssue> { | ||
| // Accept null - return nil for the optional type | ||
| if case .null = value { | ||
| return .valid(nil) | ||
| } | ||
| return wrapped.parse(value).map(Optional.some) | ||
| } | ||
| } | ||
|
|
||
| /// Implementation using oneOf composition | ||
| private struct OrNullUnionComponent<WrappedValue, Wrapped: JSONSchemaComponent>: JSONSchemaComponent | ||
| where Wrapped.Output == WrappedValue { | ||
| typealias Output = WrappedValue? | ||
|
|
||
| var wrapped: Wrapped | ||
|
|
||
| public var schemaValue: SchemaValue { | ||
| get { | ||
| .object([ | ||
| Keywords.OneOf.name: .array([ | ||
| wrapped.schemaValue.value, | ||
| JSONNull().schemaValue.value, | ||
| ]) | ||
| ]) | ||
| } | ||
| set { | ||
| // Not implemented - this modifier doesn't support schema value mutation | ||
| } | ||
| } | ||
|
|
||
| public func parse(_ value: JSONValue) -> Parsed<WrappedValue?, ParseIssue> { | ||
| // Accept null - return nil for the optional type | ||
| if case .null = value { | ||
| return .valid(nil) | ||
| } | ||
| return wrapped.parse(value).map(Optional.some) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Sources/JSONSchemaBuilder/JSONPropertyComponent/Modifier/PropertyFlatMap.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import JSONSchema | ||
|
|
||
| extension JSONPropertyComponent { | ||
| /// Flattens a double-optional output to a single optional. | ||
| /// This is specifically useful for optional properties that use `.orNull()`, | ||
| /// which creates a double-optional (T??) that needs to be flattened to T?. | ||
| /// - Returns: A new component that flattens the double-optional output. | ||
| public func flatMapOptional<Wrapped>() | ||
| -> JSONPropertyComponents.FlatMapOptional<Self, Wrapped> | ||
| where Output == Wrapped?? { | ||
| .init(upstream: self) | ||
| } | ||
| } | ||
|
|
||
| extension JSONPropertyComponents { | ||
| public struct FlatMapOptional<Upstream: JSONPropertyComponent, Wrapped>: JSONPropertyComponent | ||
| where Upstream.Output == Wrapped?? { | ||
| let upstream: Upstream | ||
|
|
||
| public var key: String { upstream.key } | ||
|
|
||
| public var isRequired: Bool { upstream.isRequired } | ||
|
|
||
| public var value: Upstream.Value { upstream.value } | ||
|
|
||
| public func parse(_ input: [String: JSONValue]) -> Parsed<Wrapped?, ParseIssue> { | ||
| switch upstream.parse(input) { | ||
| case .valid(let output): | ||
| // Flatten T?? to T? | ||
| // If output is nil (property missing), return nil | ||
| // If output is .some(nil) (property present but null), return nil | ||
| // If output is .some(.some(value)), return value | ||
| return .valid(output.flatMap { $0 }) | ||
| case .invalid(let error): return .invalid(error) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.