api-platform / core

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

ContextBuilder called twice for normalization context #6693

Open maxhelias opened 3 weeks ago

maxhelias commented 3 weeks ago

API Platform version(s) affected: 3.4

Description
During the runtime of a request on my app, I noticed that my ContextBuilder::createFromRequest was called 2 times to create the normalization context :

How to reproduce
Create a ContextBuilder or check out ContextBuilder's built-in

Possible Solution
I was wondering if we can in the SerializeProcessor, check whether the request attribute _api_normalization_context is already computed or not, to avoid a second call. Normally the context calculation shouldn't change in the same runtime, but I may be missing something.

Additional Context
This may be intentional, in which case, another solution might be considered to leave the possibility of recomputing/caching or not the normalization context at the operation level or elsewhere to leave the choice to the user if a recomputing is necessary

soyuka commented 3 weeks ago

Sometimes read is not called (on POST operations for example). But indeed, we may add a check and use the previously computed context.

maxhelias commented 3 weeks ago

Oh yes, thanks, i had in mind the read option on the operation but had forgotten about it. The legacy system with listeners does the same thing. I'll do a PR soon for both 😉

soyuka commented 3 weeks ago

:+1: you can target 3.4