[QueryParameter] Support PHP backed enums in OpenAPI schemas
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with how QueryParameter schemas are turned into OpenAPI output and how query values are validated, since the payload names neither file nor test. Check whether the existing enum handling for resource properties can be reused for a QueryParameter schema that names an enum class. The syntax is still open (a 'enumClass' key inside schema, or a dedicated option), so agree on it with maintainers first. Done means the generated schema lists the backed enum's values and incoming values are validated against them.
Written by the indexing model from the issue text.
Description
Description
In API Platform 4.4 / 5.0, defining a QueryParameter with allowed values from a PHP backed enum requires manually duplicating its values in the OpenAPI schema.
For example:
enum FooEnum: string
{
case A = 'a';
case B = 'b';
case C = 'c';
}
Currently, the values must be explicitly listed:
new QueryParameter(
schema: [
'type' => 'string',
'enum' => ['a', 'b', 'c'],
],
)
This duplicates the enum definition and requires updating the schema whenever enum cases change.
PHP does not allow calling FooEnum::cases() directly in attribute arguments.
Proposed solution
Allow referencing a PHP backed enum class directly in a QueryParameter schema.
For example:
new QueryParameter(
schema: [
'type' => 'string',
'enumClass' => FooEnum::class,
],
)
Alternatively, a dedicated enumClass option on QueryParameter could be introduced.
The syntax is illustrative; the goal is to avoid manually duplicating enum values.
Expected behavior
API Platform should automatically:
- Extract backed enum values using
FooEnum::cases(). - Include these values in the generated OpenAPI schema.
- Validate incoming parameter values against the enum.
Generated OpenAPI schema:
type: string
enum:
- a
- b
- c
Automatic conversion to PHP enum instances is not required as part of this feature.
Additional context
API Platform already supports PHP backed enums in resource properties and OpenAPI schema generation.
It would be useful to provide equivalent support for explicitly declared query parameters, keeping the enum as the single source of truth.
- Dominant language
- PHP
- Stars
- 2.6k
- Forks
- 989
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 102
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 1 day ago. Open
Difficulty 2/5 Under an hour Newbie friendliness 80/100
api-platform/core#8701 ·
Maintainers usually reply within 1 day
-
JSON:API: `JsonApiCollectionBaseSchemaNoPagination` declares `data` as `type: array` without `items`Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
api-platform/core#8699 ·
Maintainers usually reply within 1 day
-
api:openapi:export --spec-version=3.0.0 outputs `webhooks`, which is invalid in OpenAPI 3.0Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
api-platform/core#8690 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
api-platform/core#8680 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
api-platform/core#8725 ·
Maintainers usually reply within 1 day
All issues in api-platform/core
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
opnsense/core#11052 · 1 comment ·
Maintainers usually reply within 1 day
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
grokability/snipe-it#19857 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
Maintainers usually reply within 1 day