contributte / apitte

:wrench: An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes, annotations and loving openapi/swagger.

Home Page:https://contributte.org/packages/contributte/apitte/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dispatcher / transformer supporting SerializerInterface out of the box

northys opened this issue · comments

I noticed that https://github.com/contributte/apitte-skeleton/ implements it's own JsonDispatcher to use symfony/serializer. Also it somehow allows to return any value Object (DTO?) in controller that will be passed directly into serializer.

I did the same thing in my project based on nette/sandbox by extending DecoratedDispatcher::negotiate() which allows only array, scalar and IResponseEntity so I added is_object condition and returned new ObjectEntity wrapping my DTO which is already present in this repository but unsused. Then I created my own SerializerTransformer that transformed this TDO / value object into json exactly the same way you do in JsonDispatcher of apitte-skeleton.

My question is why is this part of apitte-skeleton and not supported out of the box by this library? Am I missing something? My point is that I migrated (basically I copied few folders into apitte-skeleton) my API I started to work at 2 weeks ago to contributte/api-skeleton and without changing much it magically started to work. So where's the difference between soluton in skeleton and my tinker solution?

If I get your point correctly, do you ask why some parts of apitte-skeleton, such as https://github.com/contributte/apitte-skeleton/blob/master/app/model/Api/Dispatcher/JsonDispatcher.php, is not part of apitte/core?

Yes

Basically what apitte/core allows me out of the box is piece of array *** while in some random sandbox which I didn't know about there is full featured serializer that allows me to return any DTO as response object from controller method. Those response objects can be nested and everything is typed and lovely. This option with serializer should be recommended default available in apitte/core <3

Before I discovered apitte sandbox I did it myself by extending DecoratedDispatcher's negotiate method with if (is_object) { return ObjectEntity::from($result); } and implementing SerializerTransformer that calls the symfony/serializer.

I don't know why you decided to put everything to dispatcher I don't think it is it's work to serialize responses. Aren't there transformers to do this job? I'm not nitpicking btw even if it may sound like so. It just confused me when I saw your solution for the first time. I thought my solution was bad usage of apitte and that I should do it in different way.

Closing here. Feel free to open it.