Skip to content

Add support for Backed Enum as model in Symfony Forms #2104

@nikola-jovanovic-php

Description

@nikola-jovanovic-php

Currently, there is support for Backed Enum in Entity that is creating component and referencing it in in open api specification.

But when adding EnumType in symfony form, it's specified as simple enum and not a reusable component, same as ChoiceType, and this is creating a lot of duplicate enum types when generating api clients, one per each form where it's used.

I guess the update should be happening here (but i'm not sure).

Current State:

Enum:

enum ExampleEnum: string
{
    case ONE = 'one';
    case TWO = 'two';
    case THREE = 'three';
}

Form:

->add('formField', EnumType::class, ['class' => ExampleEnum::class])

generated specs:

formField:
    type: string
    enum: ['one', 'two', 'three']

Requested state and wanted changes:

components:
  schemas:
    ExampleEnum:
      type: string
      enum:
        - one
        - two
        - three
formField:
    $ref: '#/components/schemas/ExampleEnum'

or if form field is array of enums (multiple = true):

formField:
    type: array
    items:
        $ref: '#/components/schemas/ExampleEnum'

Metadata

Metadata

Assignees

No one assigned

    Labels

    stalestale issue/PR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions