Skip to content

The generator is boxing properties of recursive types that do not need boxing #682

Open
@acecilia

Description

@acecilia

Description

👋 I am observing that the generator is boxing properties that do not need boxing

Reproduction

This OpenAPI spec:

components:
  schemas:
    MyStruct:
      type: object
      properties:
        myProperty:
          type: array
          items:
            $ref: '#/components/schemas/MyStruct'

Generates boxed code:

internal struct MyStruct: Codable, Hashable, Sendable {
    /// - Remark: Generated from `#/components/schemas/MyStruct/myProperty`.
    internal var myProperty: [Components.Schemas.MyStruct]? {
        get  {
            storage.value.myProperty
        }
        _modify {
            yield &storage.value.myProperty
        }
    }
    /// Creates a new `MyStruct`.
    ///
    /// - Parameters:
    ///   - myProperty:
    internal init(myProperty: [Components.Schemas.MyStruct]? = nil) {
        storage = .init(value: .init(myProperty: myProperty))
    }
    internal enum CodingKeys: String, CodingKey {
        case myProperty
    }
    internal init(from decoder: any Decoder) throws {
        storage = try .init(from: decoder)
    }
    internal func encode(to encoder: any Encoder) throws {
        try storage.encode(to: encoder)
    }
    /// Internal reference storage to allow type recursion.
    private var storage: OpenAPIRuntime.CopyOnWriteBox<Storage>
    private struct Storage: Codable, Hashable, Sendable {
        /// - Remark: Generated from `#/components/schemas/MyStruct/myProperty`.
        var myProperty: [Components.Schemas.MyStruct]?
        init(myProperty: [Components.Schemas.MyStruct]? = nil) {
            self.myProperty = myProperty
        }
        typealias CodingKeys = Components.Schemas.MyStruct.CodingKeys
    }
}

Package version(s)

├── swift-openapi-generator<https://github.com/apple/swift-openapi-generator@1.5.0>
│   ├── swift-algorithms<https://github.com/apple/swift-algorithms@1.2.0>
│   │   └── swift-numerics<https://github.com/apple/swift-numerics.git@1.0.2>
│   ├── swift-collections<https://github.com/apple/swift-collections@1.1.4>
│   ├── openapikit<https://github.com/mattpolzin/OpenAPIKit@3.3.0>
│   │   └── yams<https://github.com/jpsim/Yams@5.1.3>
│   ├── yams<https://github.com/jpsim/Yams@5.1.3>
│   └── swift-argument-parser<https://github.com/apple/swift-argument-parser@1.5.0>
├── swift-openapi-runtime<https://github.com/apple/swift-openapi-runtime@1.7.0>
│   └── swift-http-types<https://github.com/apple/swift-http-types@1.3.1>
├── swift-openapi-urlsession<https://github.com/apple/swift-openapi-urlsession@1.0.2>
│   ├── swift-openapi-runtime<https://github.com/apple/swift-openapi-runtime@1.7.0>
│   │   └── swift-http-types<https://github.com/apple/swift-http-types@1.3.1>
│   ├── swift-http-types<https://github.com/apple/swift-http-types@1.3.1>
│   └── swift-collections<https://github.com/apple/swift-collections@1.1.4>
└── swift-argument-parser<https://github.com/apple/swift-argument-parser@1.5.0>

Expected behavior

This swift code is valid and compiles:

struct MYStruct {
    let myProperty: [MYStruct]
}

So I would expect that the generated code does not have any boxing.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

Originally raised in #70 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/generatorAffects: plugin, CLI, config file.kind/bugFeature doesn't work as expected.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions