Home > Software design >  Using Native Enum in Api-Platform / OpenApi
Using Native Enum in Api-Platform / OpenApi

Time:03-25

I am learning OpenApi/Swagger API with Api-Platform. I created a new endpoint, that accepts values of an enum as a parameter:

#[ApiResource(
itemOperations: [
    'get_by_name' => [
        'openapi_context' => [
            ....
            'parameters' => [
                [
                    'in' => 'header',
                    'name' => 'X-Server-Region',
                    'schema' => [
                    'type' => 'string',
                    'enum' => ['server1', 'server2'],
                    'example' => 'server1',
                 ],
                 'description' => 'Server to select',
                 'required' => true
             ],
...
)]

However, this is a rather common param and values can be updated frequently (as more servers are added), I'd like to use some kind of template.

So I tried:

<?php

namespace App\Enum;

enum Server: string
{
    case SERVER1 = 'server1';
    case SERVER2 = 'server2';
    ...
}

with

'enum' => [...Server::cases()],

or

'enum' => [Server::class],

and many other forms of that, to no avail.

I tried to understand the concept of components, but could not find a way to use them in Symfony/Api Platform.

How could I reuse an enum at different endpoints?

CodePudding user response:

Enums being fairly new to PHP, they are not yet directly supported by Api-Platform.

Support will come, but for the time being you'll have to explicitly list each of the cases manually on the configuration.

  • Related