Skip to content

Commit

Permalink
Add feature flag and context entry for UUID type support
Browse files Browse the repository at this point in the history
  • Loading branch information
gentges committed Oct 7, 2024
1 parent 10598bc commit d3c3fcc
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Sources/_OpenAPIGeneratorCore/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
public enum FeatureFlag: String, Hashable, Codable, CaseIterable, Sendable {
// needs to be here for the enum to compile
case empty

/// UUID support
///
/// Enable interpretation of `type: string, format: uuid` as `Foundation.UUID` typed data.
case uuidSupport
}

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

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

/// A boolean value indicating whether the `uuid` format on schemas should be followed.
var supportUUIDFormat: Bool {
config.featureFlags.contains(.uuidSupport)
}
}
10 changes: 9 additions & 1 deletion Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ protocol FileTranslator {
extension FileTranslator {

/// A new context from the file translator.
var context: TranslatorContext { TranslatorContext(asSwiftSafeName: { $0.safeForSwiftCode }) }
var context: TranslatorContext {
TranslatorContext(
asSwiftSafeName: { $0.safeForSwiftCode },
enableUUIDSupport: supportUUIDFormat
)
}
}

/// A set of configuration values for concrete file translators.
Expand All @@ -58,4 +63,7 @@ struct TranslatorContext {
/// - Parameter string: The string to convert to be safe for Swift.
/// - Returns: A Swift-safe version of the input string.
var asSwiftSafeName: (String) -> String

/// A variable that indicates the presence of the `uuidSupport` feature flag.
var enableUUIDSupport: Bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ struct TypeMatcher {
default:
switch core.format {
case .dateTime: typeName = .date
case .uuid: typeName = .uuid
case .uuid where context.enableUUIDSupport: typeName = .uuid
default: typeName = .string
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Test_Core: XCTestCase {
func makeTranslator(
components: OpenAPI.Components = .noComponents,
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector(),
featureFlags: FeatureFlags = []
featureFlags: FeatureFlags = [.uuidSupport]
) -> TypesFileTranslator {
makeTypesTranslator(components: components, diagnostics: diagnostics, featureFlags: featureFlags)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ final class Test_OperationDescription: Test_Core {
endpoint: endpoint,
pathParameters: pathItem.parameters,
components: .init(),
context: .init(asSwiftSafeName: { $0 })
context: .init(asSwiftSafeName: { $0 }, enableUUIDSupport: true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class FileBasedReferenceTests: XCTestCase {
#endif
}

func testPetstore() throws { try _test(referenceProject: .init(name: .petstore)) }
func testPetstore() throws { try _test(referenceProject: .init(name: .petstore), featureFlags: [.uuidSupport]) }

// MARK: - Private

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

func testComponentsSchemasUUID() throws {
try self.assertSchemasTranslation(
featureFlags: [.uuidSupport],
"""
schemas:
MyUUID:
type: string
format: uuid
""",
"""
public enum Schemas {
public typealias MyUUID = Foundation.UUID
}
"""
)
// Without UUID support, the schema will be translated as a string
try self.assertSchemasTranslation(
"""
schemas:
MyUUID:
type: string
format: uuid
""",
"""
public enum Schemas {
public typealias MyUUID = Swift.String
}
"""
)
}

func testComponentsSchemasBase64() throws {
try self.assertSchemasTranslation(
Expand Down

0 comments on commit d3c3fcc

Please sign in to comment.