Skip to content
Merged
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
8 changes: 5 additions & 3 deletions Sources/_OpenAPIGeneratorCore/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
/// 1.0 is released.)
public enum FeatureFlag: String, Hashable, Codable, CaseIterable {

/// Has to be here until we add more feature flags, otherwise the enum
/// doesn't compile.
case empty
/// Support for `nullable` schemas.
///
/// A dedicated field in OpenAPI 3.0, a `null` value present in
/// the `types` array in OpenAPI 3.1.
case nullableSchemas
}

/// A set of enabled feature flags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ import OpenAPIKit

extension FileTranslator {
// Add helpers for reading feature flags below.

/// A Boolean value indicating whether the `nullable` field on schemas
/// should be taken into account.
var supportNullableSchemas: Bool {
config.featureFlags.contains(.nullableSchemas)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ struct TypeAssigner {
/// safe to be used as a Swift identifier.
var asSwiftSafeName: (String) -> String

/// A Boolean value indicating whether the `nullable` field on schemas
/// should be taken into account.
var supportNullableSchemas: Bool

/// Returns a type name for an OpenAPI-named component type.
///
/// A component type is any type in `#/components` in the OpenAPI document.
Expand Down Expand Up @@ -256,7 +260,10 @@ struct TypeAssigner {
// Check if this type can be simply referenced without
// creating a new inline type.
if let referenceableType =
try TypeMatcher(asSwiftSafeName: asSwiftSafeName)
try TypeMatcher(
asSwiftSafeName: asSwiftSafeName,
supportNullableSchemas: supportNullableSchemas
)
.tryMatchReferenceableType(for: schema)
{
return referenceableType
Expand Down Expand Up @@ -452,12 +459,18 @@ extension FileTranslator {

/// A configured type assigner.
var typeAssigner: TypeAssigner {
TypeAssigner(asSwiftSafeName: swiftSafeName)
TypeAssigner(
asSwiftSafeName: swiftSafeName,
supportNullableSchemas: supportNullableSchemas
)
}

/// A configured type matcher.
var typeMatcher: TypeMatcher {
TypeMatcher(asSwiftSafeName: swiftSafeName)
TypeMatcher(
asSwiftSafeName: swiftSafeName,
supportNullableSchemas: supportNullableSchemas
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct TypeMatcher {
/// safe to be used as a Swift identifier.
var asSwiftSafeName: (String) -> String

/// A Boolean value indicating whether the `nullable` field on schemas
/// should be taken into account.
var supportNullableSchemas: Bool

/// Returns the type name of a built-in type that matches the specified
/// schema.
///
Expand Down Expand Up @@ -78,8 +82,11 @@ struct TypeMatcher {
guard case let .reference(ref, _) = schema else {
return nil
}
return try TypeAssigner(asSwiftSafeName: asSwiftSafeName)
.typeName(for: ref).asUsage
return try TypeAssigner(
asSwiftSafeName: asSwiftSafeName,
supportNullableSchemas: supportNullableSchemas
)
.typeName(for: ref).asUsage
},
matchedArrayHandler: { elementType in
elementType.asArray
Expand All @@ -88,7 +95,7 @@ struct TypeMatcher {
TypeName.arrayContainer.asUsage
}
)?
.withOptional(!schema.required)
.withOptional(!schema.required || (supportNullableSchemas && schema.nullable))
}

/// Returns a Boolean value that indicates whether the schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Supported features are always provided on _both_ client and server.
- [x] description
- [x] format
- [ ] default
- [ ] nullable (only in 3.0, removed in 3.1)
- [x] nullable (only in 3.0, removed in 3.1, add `null` in `types` instead)
- [x] discriminator
- [ ] readOnly
- [ ] writeOnly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,102 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testComponentsSchemasNullableStringProperty() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyObj:
type: object
properties:
fooOptional:
type: string
fooRequired:
type: string
fooOptionalNullable:
type: [string, null]
fooRequiredNullable:
type: [string, null]
required:
- fooRequired
- fooRequiredNullable
""",
"""
public enum Schemas {
public struct MyObj: Codable, Hashable, Sendable {
public var fooOptional: Swift.String?
public var fooRequired: Swift.String
public var fooOptionalNullable: Swift.String?
public var fooRequiredNullable: Swift.String
public init(
fooOptional: Swift.String? = nil,
fooRequired: Swift.String,
fooOptionalNullable: Swift.String? = nil,
fooRequiredNullable: Swift.String
) {
self.fooOptional = fooOptional
self.fooRequired = fooRequired
self.fooOptionalNullable = fooOptionalNullable
self.fooRequiredNullable = fooRequiredNullable
}
public enum CodingKeys: String, CodingKey {
case fooOptional
case fooRequired
case fooOptionalNullable
case fooRequiredNullable
}
}
}
"""
)
try self.assertSchemasTranslation(
featureFlags: [.nullableSchemas],
"""
schemas:
MyObj:
type: object
properties:
fooOptional:
type: string
fooRequired:
type: string
fooOptionalNullable:
type: [string, null]
fooRequiredNullable:
type: [string, null]
required:
- fooRequired
- fooRequiredNullable
""",
"""
public enum Schemas {
public struct MyObj: Codable, Hashable, Sendable {
public var fooOptional: Swift.String?
public var fooRequired: Swift.String
public var fooOptionalNullable: Swift.String?
public var fooRequiredNullable: Swift.String?
public init(
fooOptional: Swift.String? = nil,
fooRequired: Swift.String,
fooOptionalNullable: Swift.String? = nil,
fooRequiredNullable: Swift.String? = nil
) {
self.fooOptional = fooOptional
self.fooRequired = fooRequired
self.fooOptionalNullable = fooOptionalNullable
self.fooRequiredNullable = fooRequiredNullable
}
public enum CodingKeys: String, CodingKey {
case fooOptional
case fooRequired
case fooOptionalNullable
case fooRequiredNullable
}
}
}
"""
)
}

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