Skip to content

Support NSNull in OpenAPIContainer types #109

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 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions Sources/OpenAPIRuntime/Base/OpenAPIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
//
//===----------------------------------------------------------------------===//

#if canImport(Foundation)
import class Foundation.NSNull
#endif

/// A container for a value represented by JSON Schema.
///
/// Contains an untyped JSON value. In some cases, the structure of the data
Expand Down Expand Up @@ -62,6 +66,9 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
/// - Throws: When the value is not supported.
static func tryCast(_ value: (any Sendable)?) throws -> (any Sendable)? {
guard let value = value else { return nil }
#if canImport(Foundation)
if value is NSNull { return value }
#endif
if let array = value as? [(any Sendable)?] { return try array.map(tryCast(_:)) }
if let dictionary = value as? [String: (any Sendable)?] { return try dictionary.mapValues(tryCast(_:)) }
if let value = tryCastPrimitiveType(value) { return value }
Expand Down Expand Up @@ -123,6 +130,12 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
try container.encodeNil()
return
}
#if canImport(Foundation)
if value is NSNull {
try container.encodeNil()
return
}
#endif
switch value {
case let value as Bool: try container.encode(value)
case let value as Int: try container.encode(value)
Expand Down
17 changes: 17 additions & 0 deletions Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
//
//===----------------------------------------------------------------------===//
import XCTest
#if canImport(Foundation)
import Foundation
#endif
@_spi(Generated) @testable import OpenAPIRuntime

final class Test_OpenAPIValue: Test_Runtime {
Expand All @@ -22,6 +25,10 @@ final class Test_OpenAPIValue: Test_Runtime {
_ = OpenAPIValueContainer(1)
_ = OpenAPIValueContainer(4.5)

#if canImport(Foundation)
XCTAssertEqual(try OpenAPIValueContainer(unvalidatedValue: NSNull()).value as? NSNull, NSNull())
#endif

_ = try OpenAPIValueContainer(unvalidatedValue: ["hello"])
_ = try OpenAPIValueContainer(unvalidatedValue: ["hello": "world"])

Expand Down Expand Up @@ -60,6 +67,16 @@ final class Test_OpenAPIValue: Test_Runtime {
"""#
try _testPrettyEncoded(container, expectedJSON: expectedString)
}
#if canImport(Foundation)
func testEncodingNSNull() throws {
let value = NSNull()
let container = try OpenAPIValueContainer(unvalidatedValue: value)
let expectedString = #"""
null
"""#
try _testPrettyEncoded(container, expectedJSON: expectedString)
}
#endif

func testEncoding_container_failure() throws {
struct Foobar: Equatable {}
Expand Down