Elao / PhpEnums

:nut_and_bolt: Extended PHP 8.1+ enums features & specific integrations with frameworks and libraries
MIT License
325 stars 27 forks source link

Nullability support for API platform swagger documentation #138

Closed rcoiffard closed 3 years ago

rcoiffard commented 3 years ago

We noticed that the ElaoEnumType-Factory does not pupose the null value for "nullable" fields in the json-schema documentation (swagger).

The swagger specifications indicate that a nullable enumeration can be defined as follows: (https://swagger.io/docs/specification/data-models/enums/)

type: string
nullable: true  # <---
enum:
  - asc
  - desc
  - null        # <--- without quotes, i.e. null not "null"

Currently, API Platform responds with this (which is a problem for some swagger validators):

type: string
nullable: true  # <---
enum:
  - asc
  - desc
rcoiffard commented 3 years ago

I could be wrong but maybe the solution is simple by adding this in \Elao\Enum\Bridge\ApiPlatform\Core\JsonSchema\Type\ElaoEnumType::getType:

    public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
    {
...
        $values = $enumClass::values();
        //Nullable enum should expose null value
        if ($type->isNullable()) {
            $values[]=null;
        }
...
    }
ogizanagi commented 3 years ago

Indeed, we should probably add null here:

https://github.com/Elao/PhpEnums/blob/0fbdccfe49ee47edecdfa91980e4e46b8584b8ff/src/Bridge/ApiPlatform/Core/JsonSchema/Type/ElaoEnumType.php#L41

in case $type->isNullable() returns true.

Up for a PR?

rcoiffard commented 3 years ago

Yes, I will try.

rcoiffard commented 3 years ago

Could you tell me how you run unit tests on the project?

ogizanagi commented 3 years ago

Just run make test (or vendor/bin/simple-phpunit il you don't have make on your machine)

rcoiffard commented 3 years ago

I can't get them to work. It may be due to my environment (windows 10 - php 7.4)...

.\vendor\bin\simple-phpunit.bat

=> Exit code 255 without trace

Maybe your CI can run them for me?: https://github.com/Elao/PhpEnums/pull/139