Skip to content
Merged
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
234 changes: 234 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,240 @@ 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_directAndThroughArray() throws {
try self.assertSchemasTranslation(
"""
schemas:
Node:
type: object
properties:
parent:
$ref: '#/components/schemas/Node'
children:
type: array
items:
$ref: '#/components/schemas/Node'
""",
"""
public enum Schemas {
public struct Node: Codable, Hashable, Sendable {
public var parent: Components.Schemas.Node? {
get {
self.storage.value.parent
}
_modify {
yield &self.storage.value.parent
}
}
public var children: [Components.Schemas.Node]? {
get {
self.storage.value.children
}
_modify {
yield &self.storage.value.children
}
}
public init(
parent: Components.Schemas.Node? = nil,
children: [Components.Schemas.Node]? = nil
) {
self.storage = .init(value: .init(
parent: parent,
children: children
))
}
public enum CodingKeys: String, CodingKey {
case parent
case children
}
public init(from decoder: any Swift.Decoder) throws {
self.storage = try .init(from: decoder)
}
public func encode(to encoder: any Swift.Encoder) throws {
try self.storage.encode(to: encoder)
}
private var storage: OpenAPIRuntime.CopyOnWriteBox<Storage>
private struct Storage: Codable, Hashable, Sendable {
var parent: Components.Schemas.Node?
var children: [Components.Schemas.Node]?
init(
parent: Components.Schemas.Node? = nil,
children: [Components.Schemas.Node]? = nil
) {
self.parent = parent
self.children = children
}
typealias CodingKeys = Components.Schemas.Node.CodingKeys
}
}
}
"""
)
}

func testComponentsSchemasRecursive_directAndThroughDictionary() throws {
try self.assertSchemasTranslation(
"""
schemas:
Node:
type: object
properties:
parent:
$ref: '#/components/schemas/Node'
children:
type: object
additionalProperties:
$ref: '#/components/schemas/Node'
""",
"""
public enum Schemas {
public struct Node: Codable, Hashable, Sendable {
public var parent: Components.Schemas.Node? {
get {
self.storage.value.parent
}
_modify {
yield &self.storage.value.parent
}
}
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? {
get {
self.storage.value.children
}
_modify {
yield &self.storage.value.children
}
}
public init(
parent: Components.Schemas.Node? = nil,
children: Components.Schemas.Node.childrenPayload? = nil
) {
self.storage = .init(value: .init(
parent: parent,
children: children
))
}
public enum CodingKeys: String, CodingKey {
case parent
case children
}
public init(from decoder: any Swift.Decoder) throws {
self.storage = try .init(from: decoder)
}
public func encode(to encoder: any Swift.Encoder) throws {
try self.storage.encode(to: encoder)
}
private var storage: OpenAPIRuntime.CopyOnWriteBox<Storage>
private struct Storage: Codable, Hashable, Sendable {
var parent: Components.Schemas.Node?
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)
}
}
var children: Components.Schemas.Node.childrenPayload?
init(
parent: Components.Schemas.Node? = nil,
children: Components.Schemas.Node.childrenPayload? = nil
) {
self.parent = parent
self.children = children
}
typealias CodingKeys = Components.Schemas.Node.CodingKeys
}
}
}
"""
)
}

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