[PHP-SYMFONY] Align invalid BackedEnum query deserialization with SerializerRuntimeException (fixes #23552)#23553
Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom Apr 15, 2026
Conversation
…num tryFrom in JmsSerializer
wing328
reviewed
Apr 15, 2026
| $enum = $type::tryFrom($data); | ||
| if (!$enum) { | ||
| throw new RuntimeException(sprintf("Unknown %s value in %s enum", $data, $type)); | ||
| throw new SerializerRuntimeException(sprintf("Unknown %s value in %s enum", $data, $type)); |
Member
There was a problem hiding this comment.
if there's a demand to fallback to runtime exception for whatever reasons, we will add an option for the fallback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #23552
What changed
The generated
php-symfonyJmsSerializeruseduse RuntimeExceptionand threwnew RuntimeException(...)when aBackedEnum::tryFrom($data)failed fordeserialize(..., 'string')(typical forin: queryenums). The generatedDefaultControlleronly catchesJMS\Serializer\Exception\RuntimeExceptionasSerializerRuntimeException, so PHP’scatchdid not match a global\RuntimeException, often surfacing as HTTP 500 instead of the bad-request path.Template change (
modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache):use RuntimeException;and existingthrow new RuntimeException(...)for unsupported-type branches.use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException;.tryFromfailure branch now throwsnew SerializerRuntimeException(sprintf('Unknown %s value in %s enum', ...)), matching the controller’scatch.Tests / fixture
modules/openapi-generator/src/test/resources/3_1/php-symfony/jms-enum-query-invalid-deserialization.yaml.PhpSymfonyServerCodegenTest#testJmsSerializerUsesJmsRuntimeExceptionForBackedEnumStringDeserializationErrors: asserts imports, theSerializerRuntimeExceptionthrow for unknown enum values, and thatDefaultControllerstill declaresSerializerRuntimeException; runsphp -lwhenphpis onPATH.How to validate
php-symfonyfrom the new YAML fixture (or any spec with queryBackedEnum).Service/JmsSerializer.phpcontains bothuselines andthrow new SerializerRuntimeExceptiononly in the unknown-enum branch.GET ...?status=<invalid>should hitcreateBadRequestResponse(typically 400), not an unhandled 500 fromdeserialize../mvnw -pl modules/openapi-generator -Dtest=PhpSymfonyServerCodegenTest testPR checklist
Read the contribution guidelines.
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Pre-submit (local fork):
./mvnw -pl modules/openapi-generator -Dtest=PhpSymfonyServerCodegenTest testwas run successfully with Java 11. Fullclean package,generate-samples.sh, andexport_docs_generators.shwere not run in this workspace; run the full sequence before opening / updating the upstream PR so CI sample expectations stay in sync.File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) — intended target:master.If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) —fixes #23552above.If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
cc PHP technical committee: @jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon · PHP Symfony generator: @ksm2
Summary by cubic
Makes invalid query
BackedEnumvalues inphp-symfonydeserialize toSerializerRuntimeException, soDefaultControllercatches them and returns 400 instead of 500. Fixes #23552.JmsSerializer.mustache, addeduse JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException;and throw it only whenBackedEnum::tryFrom($data)fails; keptuse RuntimeException;for other unsupported types.catch; validated generated PHP syntax.Service/JmsSerializer.phpto match the new behavior.Written for commit 410c384. Summary will update on new commits.