fix(serializer): report enum backing type in denormalization violations#8389
Open
soyuka wants to merge 1 commit into
Open
fix(serializer): report enum backing type in denormalization violations#8389soyuka wants to merge 1 commit into
soyuka wants to merge 1 commit into
Conversation
When a wrong-typed value is sent for a nullable backed enum property (e.g.
`?Status`), collectDenormalizationErrors produced the violation message
"This value should be of type Status|null." — the internal PHP type, which
appears nowhere in the OpenAPI schema and names none of the accepted values.
AbstractItemNormalizer delegates nullable backed enums to BackedEnumNormalizer,
which throws an actionable exception (accepted scalars + hint). The post-loop
block then rewrapped it, replacing its expectedTypes with `(string) $types`
("Status|null") and dropping the user-facing hint flag.
Preserve the hint flag when the failure comes from an object/enum normalizer,
and in DeserializeProvider render a BackedEnum expected type as its JSON-visible
backing scalar ("string"/"int") instead of the enum FQCN. The enum class stays
in expectedTypes so the 400->422 promotion (api-platform#8183) keeps working.
Now `{"status": true}` on a `?Status` property yields
"This value should be of type string|null." plus the enum hint.
Closes api-platform#8388
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.
Description
Fixes #8388.
When a wrong-typed value is sent for a nullable backed enum property (e.g.
?Status),collectDenormalizationErrorsproduced the violation message:{ "propertyPath": "status", "message": "This value should be of type Status|null." }Status|nullis the internal PHP type: it appears nowhere in the OpenAPI schema, is not something a JSON consumer can send, and names none of the accepted values. This is a regression from the TypeInfo migration (#6979); on 4.1.x the same request namedint|string+ a hint.Root cause
AbstractItemNormalizerdelegates a nullable backed enum toBackedEnumNormalizer, which throws an actionable exception (expectedTypes = ['int','string'],useMessageForUser = true, message naming the enum). The post-loop error block then rewrapped it, replacingexpectedTypeswith(string) $types(["Status","null"]) and dropping the user-facing hint flag.DeserializeProviderrendersimplode('|', expectedTypes)→Status|null, hint gone.Fix
AbstractItemNormalizer: preserve theuseMessageForUserflag when the failure comes from an object/enum normalizer, so the actionable hint survives.DeserializeProvider::normalizeExpectedTypes: render aBackedEnumexpected type as its JSON-visible backing scalar (string/int) instead of the enum FQCN. The enum class stays inexpectedTypes, so the 400→422 promotion from Validation groups are skipped when deserializing invalid PHP enums (BackedEnumNormalizer throws before validation) #8183 keeps working.Now
{"status": true}on a?Status(string-backed, nullable) property yields:{ "propertyPath": "status", "message": "This value should be of type string|null.", "hint": "The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type Status." }Tests
Added
testWrongTypeForBackedEnumReportsAcceptedScalarTypestoEnumDenormalizationValidationTest(the existing #8183 test file). Existing enum +ValidationTestmessage assertions stay green.Only validation message content changes — no HTTP status, propertyPath, constraint-code, or response-shape change; non-breaking, targeted at
4.3.