SortFilter generates unsupported array form for parameters
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- php
- Domain
- api, backend-api-design
Research direction
Start by reading SortFilter and OpenApiFilterTrait::getOpenApiParameters(), then check the tests for generated OpenAPI query parameters. The report identifies castToArray as the likely source of the extra array-form parameter. Done means the docs no longer advertise orderDate[] when the filter rejects that input, with relevant tests passing.
Written by the indexing model from the issue text.
Description
API Platform version(s) affected:
I have tested this on both api-platform/core 4.4.4 and api-platform/core 5.0.3
Description
When you add a SortFilter to an endpoint it generates 2 forms of the sortFilter for the given property. For example:
Let's create the following Collection endpoint:
#[ApiResource(
shortName: 'Party',
operations: [
new GetCollection(
'/activities/parties{._format}',
parameters: [
'orderDate' => new QueryParameter(
filter: new SortFilter(),
property: 'date',
),
]
),
],
openapi: new Operation(
tags: ['Activities'],
summary: 'Parties',
)
)]
This would result in the docs.jsonopenapi generating these two parameters for it:
{
"name": "orderDate",
"in": "query",
"description": "Party orderDate",
"required": false,
"deprecated": false,
"schema": {
"type": "string",
"enum": [
"asc",
"desc",
"ASC",
"DESC"
]
},
"style": "form",
"explode": true
},
{
"name": "orderDate[]",
"in": "query",
"description": "Party orderDate",
"required": false,
"deprecated": false,
"schema": {
"type": "array",
"items": {
"type": "string",
"enum": [
"asc",
"desc",
"ASC",
"DESC"
]
}
},
The first works fine, but if you were to use ?orderDate[]= the endpoint will throw a constrain violation with code 422 saying the selected value is not valid. It seems Order filter creates the documentation for it but does not actually support this form of the filter (which makes sense to me as I cannot come up with a situation where you would want to be able to sort the same property in multiple directions).
How to reproduce
I think the previous header explains it adequately.
Possible Solution
One solution would be to overwrite the castToArray property of the parameter before passing it to OpenApiFilterTrait's getOpenApiParameters() method by altering the use statement in SortFilter to something like:
use OpenApiFilterTrait {
getOpenApiParameters as private traitGetOpenApiParameters;
}
and then setting castToArray to false if it was NULL by adding the following to SortFilter:
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
{
return $this->traitGetOpenApiParameters(
$parameter->withCastToArray(
$parameter->getCastToArray() ?? false,
)
);
}
- Dominant language
- PHP
- Stars
- 2.6k
- Forks
- 988
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 105
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from api-platform/core
-
JSON:API: resource schemas (`*.jsonapi`) have `properties` but no `type: object`Possibly taken @ousamabenyounes claimed this 5 days ago. Open
Difficulty 2/5 Under an hour Newbie friendliness 80/100
api-platform/core#8701 ·
Maintainers usually reply within 1 day
-
[Json-Schema] Nested resources lost externalDoc and description dataPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
api-platform/core#8721 ·
Maintainers usually reply within 1 day
-
JSON:API: 404/405 router errors are empty, and error objects have a non-spec `type` memberPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 50/100
api-platform/core#8704 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
api-platform/core#8703 ·
Maintainers usually reply within 1 day
-
JSON:API: relations set through the constructor reject spec resource linkage (`relationships.<name>.data`)Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
api-platform/core#8702 ·
Maintainers usually reply within 1 day
All issues in api-platform/core
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
MagnaCapax/PMSS#1142 · 1 comment ·
Maintainers usually reply within 3 days
-
bug 🐛
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
netalertx/NetAlertX#1856 · 1 reaction ·
Maintainers usually reply within 1 day
-
Bug Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
feature good first issue html mcv php
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
lorisleiva/laravel-docker#166 ·