The public process method on the classes extending the ResourceBase can have different definitions depending on the request method. When trying to handle GET and POST on the same route with:
methods: ['GET', 'POST']
I need:
GET:
public function process(Request $request): ResourceResponse
POST:
public function process(Request $request, JsonApiDocumentTopLevel $document): ResourceResponse
Because the arguments depend on the process definition:
$args = $this->argumentResolver->getArguments($request, [$resource, 'process']);
$response = $resource->process(...$args);I can't make both methods work with an unique definition of the process function. Adding a default NULL value on the $document doesn't work on POST either.
I could create a custom _controller and handle the logic depending on the request method or have one separated route per method. But I think this could be a common enough use case to be supported within the module.
Is there a different approach? Am I missing something?
What do you think?
Thanks.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3094907-9.patch | 1.06 KB | sam711 |
| #3 | resolve_document_before_default_value-3094907-3.patch | 1.1 KB | sam711 |
Comments
Comment #2
sam711 commentedLooking further into this I found that this code (default NULL value for $document) ...
public function process(Request $request, JsonApiDocumentTopLevel $document = NULL): ResourceResponsedoesn't work because of the order of the arguments in jsonapi_resources.controller.argument_resolver. The default resolver is being processed before than the document resolver.
It looks like it's working if I change the order to:
Are you aware of any side effects if we change the order?
This would solve the problem of supporting several methods with the same route/controller.
Comment #3
sam711 commentedPatch changing the order of the arguments.
Comment #4
sam711 commentedNeeds the defaultTheme on tests fixed. #3094974: Add defaultTheme to JsonapiResourceTest
Comment #5
sam711 commentedComment #6
sam711 commentedTests fixed, passing now.
Comment #7
mglamanI think this makes sense, as default is supposed to be last. I'll take a look at this before we fix #3096424: The service argument_resolver.raw_parameter has been removed
Comment #8
sam711 commentedThanks!
Comment #9
sam711 commentedSee if this works for both issues since both are changing the same line.
Comment #10
sam711 commentedComment #11
sam711 commentedThe reason for changing the argument order is that when the default value is specified, the
argument_resolver.defaultreturns NULL and ends the chain, sojsonapi_resources.argument_resolver.documentnever runs.Changing the order affects the chain resolver flow.
Sorry for not being able to make it clear before. Thanks @mglaman and @gabesullice!
Comment #12
mglamanWorks for me to fix #3096424: The service argument_resolver.raw_parameter has been removed here as well.
Comment #14
mglaman