Skip to content
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

Support type: 'null' as OpenAPIValueContainer #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let package = Package(

// Read OpenAPI documents
.package(url: "https://github.com/mattpolzin/OpenAPIKit", from: "3.1.2"),
.package(url: "https://github.com/jpsim/Yams", "4.0.0"..<"6.0.0"),
.package(url: "https://github.com/jpsim/Yams", "5.1.0"..<"6.0.0"),

// CLI Tool
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ struct TypeMatcher {
private static func _tryMatchBuiltinNonRecursive(for schema: JSONSchema.Schema) -> TypeUsage? {
let typeName: TypeName
switch schema {
case .null(_): typeName = TypeName.valueContainer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm still unsure whether we want to promote null to a JSON value here. Let's discuss in the other PR first, then we can come back to this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered the singleton instance of NSNull, or some new enum type with one case, or () or something. It wasn't obvious if there was a more appropriate type for this in Swift.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe we should introduce an typealias OpenAPINull = Void in the runtime library, and use that? What do you think? cc @simonjbeaumont

OpenAPIValueContainer isn't appropriate, I think, as it erases our knowledge of it being null, forcing the adopter to check again later. We should be able to avoid that.

case .boolean(_): typeName = .swift("Bool")
case .number(let core, _):
switch core.format {
Expand Down Expand Up @@ -331,7 +332,7 @@ struct TypeMatcher {
// arrays are already recursed-into by _tryMatchTypeRecursive
// so just return nil here
return nil
case .reference, .not, .all, .any, .one, .null:
case .reference, .not, .all, .any, .one:
// never built-in
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ extension FileTranslator {
func isSchemaSupported(_ schema: JSONSchema, referenceStack: inout ReferenceStack) throws -> IsSchemaSupportedResult
{
switch schema.value {
case .string, .integer, .number, .boolean,
case .null, .string, .integer, .number, .boolean,
// We mark any object as supported, even if it
// has unsupported properties.
// The code responsible for emitting an object is
Expand Down Expand Up @@ -173,7 +173,7 @@ extension FileTranslator {
schemas.filter(\.isReference),
referenceStack: &referenceStack
)
case .not, .null: return .unsupported(reason: .schemaType, schema: schema)
case .not: return .unsupported(reason: .schemaType, schema: schema)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testComponentsSchemasNull() throws {
try self.assertSchemasTranslation(
"""
schemas:
Null:
type: "null"
NullArray:
type: array
items:
$ref: "#/components/schemas/Null"
""",
"""
public enum Schemas {
public typealias Null = OpenAPIRuntime.OpenAPIValueContainer
public typealias NullArray = [Components.Schemas.Null]
}
""")
}

func testComponentsSchemasNullableStringProperty() throws {
try self.assertSchemasTranslation(
"""
Expand Down