Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ fileprivate extension Array where Element == String {

extension ExistingTypeDescription {

/// The name in the `Components.Schemas.` namespace, if the type can appear
/// there. Nil otherwise.
/// The name in the `Components.Schemas.` namespace this type references
/// without an intervening array or dictionary, if any. Nil otherwise.
var referencedSchemaComponentName: String? {
switch self {
case .member(let components): return components.nameIfTopLevelSchemaComponent
case .array(let desc), .dictionaryValue(let desc), .any(let desc), .optional(let desc):
return desc.referencedSchemaComponentName
case .any(let desc), .optional(let desc): return desc.referencedSchemaComponentName
// Arrays and dictionaries provide heap indirection, so a reference reached
// only through them cannot form a value-type cycle that needs boxing.
case .array, .dictionaryValue: return nil
case .generic: return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,74 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testComponentsSchemasRecursive_throughArray() throws {
try self.assertSchemasTranslation(
"""
schemas:
Node:
type: object
properties:
children:
type: array
items:
$ref: '#/components/schemas/Node'
""",
"""
public enum Schemas {
public struct Node: Codable, Hashable, Sendable {
public var children: [Components.Schemas.Node]?
public init(children: [Components.Schemas.Node]? = nil) {
self.children = children
}
public enum CodingKeys: String, CodingKey {
case children
}
}
}
"""
)
}

func testComponentsSchemasRecursive_throughDictionary() throws {
try self.assertSchemasTranslation(
"""
schemas:
Node:
type: object
properties:
children:
type: object
additionalProperties:
$ref: '#/components/schemas/Node'
""",
"""
public enum Schemas {
public struct Node: Codable, Hashable, Sendable {
public struct childrenPayload: Codable, Hashable, Sendable {
public var additionalProperties: [String: Components.Schemas.Node]
public init(additionalProperties: [String: Components.Schemas.Node] = .init()) {
self.additionalProperties = additionalProperties
}
public init(from decoder: any Swift.Decoder) throws {
additionalProperties = try decoder.decodeAdditionalProperties(knownKeys: [])
}
public func encode(to encoder: any Swift.Encoder) throws {
try encoder.encodeAdditionalProperties(additionalProperties)
}
}
public var children: Components.Schemas.Node.childrenPayload?
public init(children: Components.Schemas.Node.childrenPayload? = nil) {
self.children = children
}
public enum CodingKeys: String, CodingKey {
case children
}
}
}
"""
)
}

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