api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 876 forks source link

Making Elao Enums work out of the box #2936

Closed jacraade closed 3 years ago

jacraade commented 5 years ago

Hello.

Using Elao Enums as properties gets the openapi/swagger generator all confused. It always says that the type is string instead of the actual enum type or the type behind the enum (integer in my case). functionally it seems to work fine (as in you pass an integer not a string to the api and it saves it correctly) so that means the enum normalizer does its job.

is there a way to at least make it detect the correct type? (enum support in general seems to be WIP)

currently i'm overwriting it by hand using

     * @ApiProperty(
     *     attributes={
     *         "swagger_context"={"type"="integer"},
     *         "openapi_context"={"type"="integer"}
     *     }
     * )

but thats not optimal.

i suppose this is just how it is right now with enums not supported out of the box but is there a a better way doing it till they are supported?

teohhanhui commented 5 years ago

I don't see anything in the EnumInterface that allows us to know the type of values. So it's not possible at this time. You should perhaps raise this issue with them.

jacraade commented 5 years ago

hmm. what would be needed to make it clear so i could create a pull request for it? a function that returns a php or json type name?

also i guess the backend binding doesn't help because its not a thing that always exists.

teohhanhui commented 5 years ago

Hmm... It might help actually. They could create a PropertyInfo extractor that uses the Doctrine metadata. But I don't lnow how good the PropertyInfo component is at combining / merging type information from multiple sources. @dunglas?

soyuka commented 5 years ago

IMO propertyinfo can be easily decorated

teohhanhui commented 5 years ago

Fair enough. But I don't think it should be done that way. Decorating the correct extractor sounds better.

jacraade commented 5 years ago

@teohhanhui , ok after some more work i found out that only the get works but the put/post (denormalization) of enum values causes an "This value should be of type MyEnum" error.

i found out that api platform only passes the class name to the normalizer without the namespace (so "MyEnum" not "App\Enum\MyEnum"). This is likely the root issue as even with a custom Normalizer that adds the namespace back to it, it then fails at validation with the same error

teohhanhui commented 5 years ago

@jacraade That doesn't sound like an API Platform issue. You probably forgot a use statement?

jacraade commented 5 years ago

i think i just solved it. the problem was not the use but the autogenerated @var \MyEnum annotation. never paid attention to it because i didn't imagine it being used. changed it to an absolute path seems to fix it (have to do more tests)

hope php gets native attributes some day and doesn't have to misuse comments 😞

sorry for the hassle

Edit: well fuck

teohhanhui commented 5 years ago

Typed properties will be in PHP 7.4 :smile:

soyuka commented 3 years ago

Closing as duplicate of https://github.com/api-platform/core/issues/2254