Skip to content

[REQ] [Swift] Add conformance to Identifiable protocol to models when possible #20143

@x-sheep

Description

@x-sheep

Is your feature request related to a problem? Please describe.

SwiftUI makes heavy use of the Identifiable protocol, which is used to track the identity of entities across data changes. If an object is replaced with a different object with the same identity, SwiftUI will animate the item changing instead of animating an insertion.

Objects that do not conform to the Identifiable protocol cannot be used for displaying lists of items, unless a keypath is supplied that references a field that can be used as a identity.

The OpenAPI generator currently doesn't emit any type that uses the Identifiable protocol.

Describe the solution you'd like

The generator should check if a model has a field id of type string, integer or UUID, and emit a conformance to Identifiable if it does. Other data types could be supported if they conform to Hashable.

Describe alternatives you've considered

I'm currently adding conformance retroactively by adding an extra file containing extensions to the project, with lines such as:

extension Pet : @retroactive Identifiable {}

This must be done for every single model that is used in a ForEach or List object.

Additional context

From the Swift documentation:

Identifiable leaves the duration and scope of the identity unspecified.

This means that adding Identifiable conformance does not come with undue assumptions regarding the API being contacted, e.g. if IDs can be reused between resource types or scopes.

The protocol was introduced with iOS 13. If lower versions of iOS should be supported, it's possible to emit a conditional conformance with an @available attribute instead of adding it directly to the type declaration.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions