Skip to content

Commit d3c3fcc

Browse files
committed
Add feature flag and context entry for UUID type support
1 parent 10598bc commit d3c3fcc

File tree

8 files changed

+54
-5
lines changed

8 files changed

+54
-5
lines changed

Sources/_OpenAPIGeneratorCore/FeatureFlags.swift

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
public enum FeatureFlag: String, Hashable, Codable, CaseIterable, Sendable {
2929
// needs to be here for the enum to compile
3030
case empty
31+
32+
/// UUID support
33+
///
34+
/// Enable interpretation of `type: string, format: uuid` as `Foundation.UUID` typed data.
35+
case uuidSupport
3136
}
3237

3338
/// A set of enabled feature flags.

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator+FeatureFlags.swift

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ import OpenAPIKit
1515

1616
extension FileTranslator {
1717
// Add helpers for reading feature flags below.
18+
19+
/// A boolean value indicating whether the `uuid` format on schemas should be followed.
20+
var supportUUIDFormat: Bool {
21+
config.featureFlags.contains(.uuidSupport)
22+
}
1823
}

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ protocol FileTranslator {
4747
extension FileTranslator {
4848

4949
/// A new context from the file translator.
50-
var context: TranslatorContext { TranslatorContext(asSwiftSafeName: { $0.safeForSwiftCode }) }
50+
var context: TranslatorContext {
51+
TranslatorContext(
52+
asSwiftSafeName: { $0.safeForSwiftCode },
53+
enableUUIDSupport: supportUUIDFormat
54+
)
55+
}
5156
}
5257

5358
/// A set of configuration values for concrete file translators.
@@ -58,4 +63,7 @@ struct TranslatorContext {
5863
/// - Parameter string: The string to convert to be safe for Swift.
5964
/// - Returns: A Swift-safe version of the input string.
6065
var asSwiftSafeName: (String) -> String
66+
67+
/// A variable that indicates the presence of the `uuidSupport` feature flag.
68+
var enableUUIDSupport: Bool
6169
}

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ struct TypeMatcher {
313313
default:
314314
switch core.format {
315315
case .dateTime: typeName = .date
316-
case .uuid: typeName = .uuid
316+
case .uuid where context.enableUUIDSupport: typeName = .uuid
317317
default: typeName = .string
318318
}
319319
}

Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Test_Core: XCTestCase {
2828
func makeTranslator(
2929
components: OpenAPI.Components = .noComponents,
3030
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector(),
31-
featureFlags: FeatureFlags = []
31+
featureFlags: FeatureFlags = [.uuidSupport]
3232
) -> TypesFileTranslator {
3333
makeTypesTranslator(components: components, diagnostics: diagnostics, featureFlags: featureFlags)
3434
}

Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ final class Test_OperationDescription: Test_Core {
144144
endpoint: endpoint,
145145
pathParameters: pathItem.parameters,
146146
components: .init(),
147-
context: .init(asSwiftSafeName: { $0 })
147+
context: .init(asSwiftSafeName: { $0 }, enableUUIDSupport: true)
148148
)
149149
}
150150
}

Tests/OpenAPIGeneratorReferenceTests/FileBasedReferenceTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class FileBasedReferenceTests: XCTestCase {
4242
#endif
4343
}
4444

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

4747
// MARK: - Private
4848

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

+31
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,37 @@ final class SnippetBasedReferenceTests: XCTestCase {
14621462
"""
14631463
)
14641464
}
1465+
1466+
func testComponentsSchemasUUID() throws {
1467+
try self.assertSchemasTranslation(
1468+
featureFlags: [.uuidSupport],
1469+
"""
1470+
schemas:
1471+
MyUUID:
1472+
type: string
1473+
format: uuid
1474+
""",
1475+
"""
1476+
public enum Schemas {
1477+
public typealias MyUUID = Foundation.UUID
1478+
}
1479+
"""
1480+
)
1481+
// Without UUID support, the schema will be translated as a string
1482+
try self.assertSchemasTranslation(
1483+
"""
1484+
schemas:
1485+
MyUUID:
1486+
type: string
1487+
format: uuid
1488+
""",
1489+
"""
1490+
public enum Schemas {
1491+
public typealias MyUUID = Swift.String
1492+
}
1493+
"""
1494+
)
1495+
}
14651496

14661497
func testComponentsSchemasBase64() throws {
14671498
try self.assertSchemasTranslation(

0 commit comments

Comments
 (0)