Open JercSi opened 2 weeks ago
Hi, have you checked the documentation? https://api-platform.com/docs/core/filters/#parameter-validation
You can declare parameters on a Resource or an Operation through the parameters property.
In your example:
new GetCollection(
provider: Provider::class,
parameters: ['prop' => new QueryParameter(required: true)],
),
A GET /simple_resources
gives you:
{
"@context": "/api/contexts/ConstraintViolationList",
"@id": "/api/validation_errors/ad32d13f-c3d4-423b-909a-857b961eb720",
"@type": "ConstraintViolationList",
"status": 422,
"violations": [
{
"propertyPath": "prop",
"message": "The parameter \"prop\" is required.",
"code": "ad32d13f-c3d4-423b-909a-857b961eb720"
}
],
"detail": "prop: The parameter \"prop\" is required.",
"description": "prop: The parameter \"prop\" is required.",
"type": "/validation_errors/ad32d13f-c3d4-423b-909a-857b961eb720",
"title": "An error occurred"
}
Your suggestion is almost perfect. If i do any changes on the filter (e.g. property is no longer required) I would need to apply this change on all resources where my filter is used.
Current solution is, to set alias to the filter and use it parameters list. Downside of this solution is that I need to set on all resources a parameters and link them with my filter.
#[ApiResource(
operations: [
new GetCollection(
provider: Provider::class,
parameters: [
'prop' => new QueryParameter(filter: 'MyFilterClass')
]
),
],
paginationEnabled: false,
)]
#[ApiFilter(
filterClass: Filter::class,
properties: [
'prop' => 'filter_a',
],
alias: 'MyFilterClass',
)]
API Platform version(s) affected: 4.0.3, 4.0.4 (Symfony)
Description
API Filters required parameters seems to be lost with the upgrade from v3.2.26 to 4.0.4 (Symfony). In previous version http reponse code was 400 with a description that query parameter is required. In 4.0.4 response is 200 with the data. OpenAPI specification (api/docs) shows the query parameter field as required in both versions.
How to reproduce
Have a resource with getCollection (with custom data provider) and ApiFilter which defines one property as required.
Expected behavior is to get the http reponse 400 with an description that property is required instead of http 200. (Response examples are below)
Additional Context
Resource example:
Collection provider example:
Filter:
Response from 3.2.26:
Response from 4.0.4: