Skip to content

fix(state): use exception message for user-facing violation when available#7894

Merged
soyuka merged 5 commits into
4.3from
fix/symfony-8.1-backend-enum-denormalizer
Apr 27, 2026
Merged

fix(state): use exception message for user-facing violation when available#7894
soyuka merged 5 commits into
4.3from
fix/symfony-8.1-backend-enum-denormalizer

Conversation

@vincentchalamon

Copy link
Copy Markdown
Contributor

When a NotNormalizableValueException has canUseMessageForUser() = true, use its message directly as the constraint violation message instead of the generic Type constraint message. This fixes issues with Symfony 8.x where expectedTypes can be null/empty, producing broken messages.

Q A
Branch? 4.3
Tickets Closes #7893
License MIT
Doc PR N/A

…lable

When a NotNormalizableValueException has canUseMessageForUser() = true,
use its message directly as the constraint violation message instead of
the generic Type constraint message. This fixes issues with Symfony 8.x
where expectedTypes can be null/empty, producing broken messages.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vincentchalamon vincentchalamon self-assigned this Mar 29, 2026
@vincentchalamon vincentchalamon changed the title fix(state): use exception message for user-facing violation when avai… fix(state): use exception message for user-facing violation when available Mar 29, 2026
vincentchalamon and others added 4 commits March 29, 2026 21:25
…expectedTypes

Simulates the behavior from symfony/serializer PR #62574 where an
invalid enum value produces an exception with expectedTypes=null and
a user-friendly message listing valid values.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ymfony compat

- Sort ValidationException import alphabetically in DeserializeProviderTest
- Update ValidationTest to expect exception message when canUseMessageForUser() is true
- Use positional params for NotNormalizableValueException ctor (lowest Symfony compat)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… violations

These violations have canUseMessageForUser()=true, so the new behavior
uses the exception message directly instead of the generic type message.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The extended NotNormalizableValueException constructor (with path and
useMessageForUser params) doesn't exist on lowest Symfony versions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vincentchalamon vincentchalamon requested a review from soyuka April 14, 2026 15:53
@soyuka soyuka merged commit 37f248a into 4.3 Apr 27, 2026
438 of 591 checks passed
@soyuka soyuka deleted the fix/symfony-8.1-backend-enum-denormalizer branch April 27, 2026 12:31
@soyuka

soyuka commented Apr 27, 2026

Copy link
Copy Markdown
Member

thanks!

@norkunas

Copy link
Copy Markdown
Contributor

The whole point of this IIRC was to use explicitly the same message that the validator would return. Now it's a BC break and will need to fix our testsuite

@vincentchalamon vincentchalamon linked an issue Apr 28, 2026 that may be closed by this pull request
@soyuka

soyuka commented Apr 28, 2026

Copy link
Copy Markdown
Member

its not released yet we can rollback, I thought that the $exception->canUseMessageForUser() would be a good guard for the BC but maybe that we should discuss this more?

WDYT @vincentchalamon ?

It looks like this is fixing a bug for 8.1 and shouldn't impact actual behavior no @norkunas can you give more details? maybe we're missing a regression test?

$violationBaz = $findViolation('baz');
$this->assertNotNull($violationBaz, 'Violation for "baz" not found.');
$this->assertSame('This value should be of type string.', $violationBaz['message']);
$this->assertSame('Failed to create object because the class misses the "baz" property.', $violationBaz['message']);

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.

these should not change to preserve BC I guess

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yup, this is what i was refering to.

@soyuka soyuka Apr 28, 2026

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.

though the new messages are way clearer... in 8.1 this breaks and we get "This value should be of type ." not sure how to fix this properly. Isn't that not covered by BC anyways (https://symfony.com/doc/current/contributing/code/bc.html#using-our-translations) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Symfony 8.1 BackendEnumDenormalizer new error

3 participants