core icon indicating copy to clipboard operation
core copied to clipboard

ContextBuilder called twice for normalization context

Open maxhelias opened this issue 1 year ago • 3 comments

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 :

  • First call here : https://github.com/api-platform/core/blob/fcd9a0725fb9fab942b306e5d3cb0e96226ee63e/src/State/Provider/ReadProvider.php#L71
  • Seconde here : https://github.com/api-platform/core/blob/fcd9a0725fb9fab942b306e5d3cb0e96226ee63e/src/State/Processor/SerializeProcessor.php#L55

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

maxhelias avatar Oct 05 '24 20:10 maxhelias

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

soyuka avatar Oct 07 '24 07:10 soyuka

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 😉

maxhelias avatar Oct 07 '24 07:10 maxhelias

:+1: you can target 3.4

soyuka avatar Oct 07 '24 09:10 soyuka

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 07 '24 04:12 stale[bot]