From 86cff2f16189488ba84f4b3d7dcb7000183a016e Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Sun, 31 Mar 2024 16:45:34 -0500 Subject: [PATCH] Support `type: 'null'` as OpenAPIValueContainer --- Package.swift | 2 +- .../TypeAssignment/TypeMatcher.swift | 3 ++- .../TypeAssignment/isSchemaSupported.swift | 4 ++-- .../SnippetBasedReferenceTests.swift | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 7b90240a..47166359 100644 --- a/Package.swift +++ b/Package.swift @@ -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"), diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift b/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift index 08ac4a1e..5395c501 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift @@ -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 case .boolean(_): typeName = .swift("Bool") case .number(let core, _): switch core.format { @@ -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 } diff --git a/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/isSchemaSupported.swift b/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/isSchemaSupported.swift index e4735b0b..e1fb6cc0 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/isSchemaSupported.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/isSchemaSupported.swift @@ -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 @@ -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) } } diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 52ad3b82..dbe67a16 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -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( """