File tree 6 files changed +28
-9
lines changed
Sources/_OpenAPIGeneratorCore/Translator
Tests/OpenAPIGeneratorReferenceTests
6 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ extension FileTranslator {
28
28
let typealiasDescription = TypealiasDescription (
29
29
accessModifier: config. access,
30
30
name: typeName. shortSwiftName,
31
- existingType: . init( existingTypeUsage. withOptional ( false ) )
31
+ existingType: . init( existingTypeUsage)
32
32
)
33
33
let typealiasComment : Comment ? = typeName. docCommentWithUserDescription ( userDescription)
34
34
return . commentable( typealiasComment, . typealias( typealiasDescription) )
Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ extension TypesFileTranslator {
70
70
let decl = try translateSchema (
71
71
typeName: typeName,
72
72
schema: parameter. schema,
73
- overrides: . init( isOptional : !parameter . required , userDescription: parameter. parameter. description)
73
+ overrides: . init( userDescription: parameter. parameter. description)
74
74
)
75
75
return decl
76
76
}
Original file line number Diff line number Diff line change @@ -114,7 +114,7 @@ extension TypesFileTranslator {
114
114
let decl = try translateSchema (
115
115
typeName: typeName,
116
116
schema: header. schema,
117
- overrides: . init( isOptional : header . isOptional , userDescription: header. header. description)
117
+ overrides: . init( userDescription: header. header. description)
118
118
)
119
119
return decl
120
120
}
Original file line number Diff line number Diff line change @@ -345,7 +345,7 @@ struct TypeAssigner {
345
345
swiftComponent: asSwiftSafeName ( originalName) + suffix,
346
346
jsonComponent: jsonReferenceComponentOverride ?? originalName
347
347
)
348
- . asUsage. withOptional ( try typeMatcher. isOptional ( schema, components: components) )
348
+ . asUsage. withOptional ( try typeMatcher. isOptionalRoot ( schema, components: components) )
349
349
}
350
350
351
351
/// Returns a type name for a reusable component.
Original file line number Diff line number Diff line change @@ -78,7 +78,7 @@ struct TypeMatcher {
78
78
} ,
79
79
genericArrayHandler: { TypeName . arrayContainer. asUsage }
80
80
) ?
81
- . withOptional ( isOptional ( schema, components: components) )
81
+ . withOptional ( isOptionalRoot ( schema, components: components) )
82
82
}
83
83
84
84
/// Returns a Boolean value that indicates whether the schema
@@ -331,6 +331,25 @@ struct TypeMatcher {
331
331
return result
332
332
}
333
333
334
+ /// Returns a Boolean value indicating whether the schema is optional at the root of any references.
335
+ /// - Parameters:
336
+ /// - schema: The reference to check.
337
+ /// - components: The OpenAPI components for looking up references.
338
+ /// - Throws: An error if there's an issue while checking the schema.
339
+ /// - Returns: `true` if the schema is an optional root, `false` otherwise.
340
+ func isOptionalRoot( _ schema: JSONSchema , components: OpenAPI . Components ) throws -> Bool {
341
+ let directlyOptional = schema. nullable || !schema. required
342
+ switch schema. value {
343
+ case . null( _) :
344
+ return true
345
+ case . reference( let ref, _) :
346
+ let indirectlyOptional = try isOptional ( ref, components: components)
347
+ return directlyOptional && !indirectlyOptional
348
+ default :
349
+ return directlyOptional
350
+ }
351
+ }
352
+
334
353
// MARK: - Private
335
354
336
355
/// Returns the type name of a built-in type that matches the specified
Original file line number Diff line number Diff line change @@ -237,7 +237,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
237
237
""" ,
238
238
"""
239
239
public enum Schemas {
240
- public typealias Null = OpenAPIRuntime.OpenAPIValueContainer
240
+ public typealias Null = OpenAPIRuntime.OpenAPIValueContainer?
241
241
public typealias NullArray = [Components.Schemas.Null]
242
242
}
243
243
""" )
@@ -520,17 +520,17 @@ final class SnippetBasedReferenceTests: XCTestCase {
520
520
"""
521
521
public enum Schemas {
522
522
public typealias MyRequiredString = Swift.String
523
- public typealias MyNullableString = Swift.String
523
+ public typealias MyNullableString = Swift.String?
524
524
public struct MyObject: Codable, Hashable, Sendable {
525
525
public var id: Swift.Int64
526
526
public var alias: Swift.String?
527
527
public var requiredString: Components.Schemas.MyRequiredString
528
- public var nullableString: Components.Schemas.MyNullableString?
528
+ public var nullableString: Components.Schemas.MyNullableString
529
529
public init(
530
530
id: Swift.Int64,
531
531
alias: Swift.String? = nil,
532
532
requiredString: Components.Schemas.MyRequiredString,
533
- nullableString: Components.Schemas.MyNullableString? = nil
533
+ nullableString: Components.Schemas.MyNullableString
534
534
) {
535
535
self.id = id
536
536
self.alias = alias
You can’t perform that action at this time.
0 commit comments