Skip to content

[PHP-SYMFONY] Align invalid BackedEnum query deserialization with SerializerRuntimeException (fixes #23552)#23553

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
JerrySLau:fix_jms_enum_ex
Apr 15, 2026
Merged

[PHP-SYMFONY] Align invalid BackedEnum query deserialization with SerializerRuntimeException (fixes #23552)#23553
wing328 merged 2 commits intoOpenAPITools:masterfrom
JerrySLau:fix_jms_enum_ex

Conversation

@JerrySLau
Copy link
Copy Markdown
Contributor

@JerrySLau JerrySLau commented Apr 15, 2026

fixes #23552

What changed

The generated php-symfony JmsSerializer used use RuntimeException and threw new RuntimeException(...) when a BackedEnum::tryFrom($data) failed for deserialize(..., 'string') (typical for in: query enums). The generated DefaultController only catches JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException, so PHP’s catch did 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):

  • Keep use RuntimeException; and existing throw new RuntimeException(...) for unsupported-type branches.
  • Add use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException;.
  • Only the tryFrom failure branch now throws new SerializerRuntimeException(sprintf('Unknown %s value in %s enum', ...)), matching the controller’s catch.

Tests / fixture

  • New minimal OpenAPI 3.1 fixture: modules/openapi-generator/src/test/resources/3_1/php-symfony/jms-enum-query-invalid-deserialization.yaml.
  • New PhpSymfonyServerCodegenTest#testJmsSerializerUsesJmsRuntimeExceptionForBackedEnumStringDeserializationErrors: asserts imports, the SerializerRuntimeException throw for unknown enum values, and that DefaultController still declares SerializerRuntimeException; runs php -l when php is on PATH.

How to validate

  1. Build the generator (Java 11 per project docs), generate php-symfony from the new YAML fixture (or any spec with query BackedEnum).
  2. Confirm Service/JmsSerializer.php contains both use lines and throw new SerializerRuntimeException only in the unknown-enum branch.
  3. In a Symfony app wired to the bundle, GET ...?status=<invalid> should hit createBadRequestResponse (typically 400), not an unhandled 500 from deserialize.
./mvnw -pl modules/openapi-generator -Dtest=PhpSymfonyServerCodegenTest test

PR 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:

    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    

    (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 test was run successfully with Java 11. Full clean package, generate-samples.sh, and export_docs_generators.sh were 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 (upcoming 7.x.0 minor 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 #23552 above.

  • 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 BackedEnum values in php-symfony deserialize to SerializerRuntimeException, so DefaultController catches them and returns 400 instead of 500. Fixes #23552.

  • Bug Fixes
    • In JmsSerializer.mustache, added use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException; and throw it only when BackedEnum::tryFrom($data) fails; kept use RuntimeException; for other unsupported types.
    • Added a minimal OpenAPI 3.1 fixture and a test to assert the import, thrown exception, and controller catch; validated generated PHP syntax.
    • Updated sample Service/JmsSerializer.php to match the new behavior.

Written for commit 410c384. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

$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));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's a demand to fallback to runtime exception for whatever reasons, we will add an option for the fallback.

@wing328 wing328 merged commit 74b72ee into OpenAPITools:master Apr 15, 2026
52 checks passed
@wing328 wing328 added this to the 7.22.0 milestone Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][PHP-SYMFONY] Invalid query BackedEnum values throw \RuntimeException in JmsSerializer, bypassing DefaultController catch (500 instead of 400)

2 participants