Skip to content

Commit d7dcae0

Browse files
committed
Add feature flag for UUID format
1 parent b4a1a4b commit d7dcae0

File tree

15 files changed

+113
-38
lines changed

15 files changed

+113
-38
lines changed

Sources/_OpenAPIGeneratorCore/FeatureFlags.swift

Lines changed: 5 additions & 0 deletions
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/CommonTranslations/translateAllAnyOneOf.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension TypesFileTranslator {
6464
parent: typeName
6565
)
6666
let associatedDeclarations: [Declaration]
67-
if TypeMatcher.isInlinable(schema) {
67+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
6868
associatedDeclarations = try translateSchema(
6969
typeName: propertyType.typeName,
7070
schema: schema,
@@ -173,7 +173,7 @@ extension TypesFileTranslator {
173173
parent: typeName
174174
)
175175
let associatedDeclarations: [Declaration]
176-
if TypeMatcher.isInlinable(schema) {
176+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
177177
associatedDeclarations = try translateSchema(
178178
typeName: childType.typeName,
179179
schema: schema,

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateObjectStruct.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension TypesFileTranslator {
8686
parent: typeName
8787
)
8888
let associatedDeclarations: [Declaration]
89-
if TypeMatcher.isInlinable(value) {
89+
if TypeMatcher.isInlinable(value, enableUUIDSupport: supportUUIDFormat) {
9090
associatedDeclarations = try translateSchema(
9191
typeName: propertyType.typeName,
9292
schema: value,
@@ -154,7 +154,7 @@ extension TypesFileTranslator {
154154
components: components,
155155
inParent: parent
156156
)
157-
if TypeMatcher.isInlinable(schema) {
157+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
158158
associatedDeclarations = try translateSchema(
159159
typeName: valueTypeUsage.typeName,
160160
schema: schema,

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator+FeatureFlags.swift

Lines changed: 5 additions & 0 deletions
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/Multipart/translateMultipart.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension TypesFileTranslator {
7676
inParent: typeName.appending(swiftComponent: nil, jsonComponent: "content")
7777
)
7878
let associatedDeclarations: [Declaration]
79-
if TypeMatcher.isInlinable(schema) {
79+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
8080
associatedDeclarations = try translateSchema(
8181
typeName: bodyTypeUsage.typeName,
8282
schema: schema,
@@ -117,7 +117,7 @@ extension TypesFileTranslator {
117117
schema: JSONSchema
118118
) throws -> [Declaration] {
119119
let associatedDeclarations: [Declaration]
120-
if TypeMatcher.isInlinable(schema) {
120+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
121121
associatedDeclarations = try translateSchema(typeName: typeName, schema: schema, overrides: .none)
122122
} else {
123123
associatedDeclarations = []

Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct TypedParameter {
3737
/// A converted function from user-provided strings to strings
3838
/// safe to be used as a Swift identifier.
3939
var asSwiftSafeName: (String) -> String
40+
41+
/// A boolean value indicating whether the `uuid` format on schemas should be followed.
42+
var supportUUIDFormat: Bool
4043
}
4144

4245
extension TypedParameter: CustomStringConvertible {
@@ -61,19 +64,19 @@ extension TypedParameter {
6164
/// A schema to be inlined.
6265
///
6366
/// - Returns: Nil when schema is referenceable.
64-
var inlineableSchema: JSONSchema? { schema.inlineableSchema }
67+
var inlineableSchema: JSONSchema? { schema.inlineableSchema(enableUUIDSupport: supportUUIDFormat) }
6568
}
6669

6770
extension UnresolvedSchema {
6871

6972
/// A schema to be inlined.
7073
///
7174
/// - Returns: Nil when schema is referenceable.
72-
var inlineableSchema: JSONSchema? {
75+
func inlineableSchema(enableUUIDSupport: Bool) -> JSONSchema? {
7376
switch self {
7477
case .a: return nil
7578
case let .b(schema):
76-
if TypeMatcher.isInlinable(schema) { return schema }
79+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: enableUUIDSupport) { return schema }
7780
return nil
7881
}
7982
}
@@ -208,7 +211,8 @@ extension FileTranslator {
208211
explode: explode,
209212
typeUsage: usage,
210213
codingStrategy: codingStrategy,
211-
asSwiftSafeName: swiftSafeName
214+
asSwiftSafeName: swiftSafeName,
215+
supportUUIDFormat: supportUUIDFormat
212216
)
213217
}
214218
}

Sources/_OpenAPIGeneratorCore/Translator/RequestBody/translateRequestBody.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension TypesFileTranslator {
4646
let contentTypeName = typeName.appending(jsonComponent: "content")
4747
let contents = requestBody.contents
4848
for content in contents {
49-
if TypeMatcher.isInlinable(content.content.schema) || content.content.isReferenceableMultipart {
49+
if TypeMatcher.isInlinable(content.content.schema, enableUUIDSupport: supportUUIDFormat) || content.content.isReferenceableMultipart {
5050
let inlineTypeDecls = try translateRequestBodyContentInTypes(content)
5151
bodyMembers.append(contentsOf: inlineTypeDecls)
5252
}

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension TypesFileTranslator {
145145
let associatedType = typedContent.resolvedTypeUsage
146146
let content = typedContent.content
147147
let schema = content.schema
148-
if TypeMatcher.isInlinable(schema) || content.isReferenceableMultipart {
148+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) || content.isReferenceableMultipart {
149149
let decls: [Declaration]
150150
if contentType.isMultipart {
151151
decls = try translateMultipartBody(typedContent)

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension TypesFileTranslator {
6666
let schema = header.schema
6767
let typeUsage = header.typeUsage
6868
let associatedDeclarations: [Declaration]
69-
if TypeMatcher.isInlinable(schema) {
69+
if TypeMatcher.isInlinable(schema, enableUUIDSupport: supportUUIDFormat) {
7070
associatedDeclarations = try translateSchema(typeName: typeUsage.typeName, schema: schema, overrides: .none)
7171
} else {
7272
associatedDeclarations = []

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeAssigner.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct TypeAssigner {
4444
/// A converted function from user-provided strings to strings
4545
/// safe to be used as a Swift identifier.
4646
var asSwiftSafeName: (String) -> String
47+
48+
/// A boolean value indicating whether the `uuid` format on schemas should be followed.
49+
var enableUUIDSupport: Bool
4750

4851
/// Returns a type name for an OpenAPI-named component type.
4952
///
@@ -328,7 +331,7 @@ struct TypeAssigner {
328331
inParent parent: TypeName,
329332
subtype: SubtypeNamingMethod
330333
) throws -> TypeUsage {
331-
let typeMatcher = TypeMatcher(asSwiftSafeName: asSwiftSafeName)
334+
let typeMatcher = TypeMatcher(asSwiftSafeName: asSwiftSafeName, enableUUIDSupport: enableUUIDSupport)
332335
// Check if this type can be simply referenced without
333336
// creating a new inline type.
334337
if let referenceableType = try typeMatcher.tryMatchReferenceableType(for: schema, components: components) {
@@ -545,10 +548,14 @@ struct TypeAssigner {
545548
extension FileTranslator {
546549
547550
/// A configured type assigner.
548-
var typeAssigner: TypeAssigner { TypeAssigner(asSwiftSafeName: swiftSafeName) }
551+
var typeAssigner: TypeAssigner {
552+
TypeAssigner(asSwiftSafeName: swiftSafeName, enableUUIDSupport: supportUUIDFormat)
553+
}
549554
550555
/// A configured type matcher.
551-
var typeMatcher: TypeMatcher { TypeMatcher(asSwiftSafeName: swiftSafeName) }
556+
var typeMatcher: TypeMatcher {
557+
TypeMatcher(asSwiftSafeName: swiftSafeName, enableUUIDSupport: supportUUIDFormat)
558+
}
552559
}
553560
554561
/// An error used during the parsing of JSON references specified in an

0 commit comments

Comments
 (0)