Repository navigation
fix(state): map to the output class on mapped write operations - #8497
Open
dylan-rumble wants to merge 1 commit into
Open
dylan-rumble wants to merge 1 commit into
dylan-rumble wants to merge 1 commit into
Conversation
dylan-rumble
marked this pull request as ready for review
September 2, 2026 13:49
3 tasks done
dylan-rumble
marked this pull request as draft
September 3, 2026 14:43
dylan-rumble
marked this pull request as ready for review
September 3, 2026 14:45
dylan-rumble
force-pushed
the
feat/object-mapper-output-dto-write-operation-support
branch
from
September 21, 2026 13:01
3743ba6 to
1fb847a
Compare
This was referenced Nov 13, 2026
dylan-rumble
force-pushed
the
feat/object-mapper-output-dto-write-operation-support
branch
from
November 17, 2026 12:10
1fb847a to
f25a7ed
Compare
With the Object Mapper, `ObjectMapperOutputProcessor` mapped the persisted entity to the resource class even when the operation declares an `output`: the body had the resource shape while OpenAPI and Hydra documented the output class. The processor now maps to the output class, as the provider does on reads. `Location`, `Content-Location` and the status are about the resource the operation acted on, they are computed from the entity mapped to the resource class. Data that already is an instance of the output class is not mapped again. Refs api-platform#8849 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dylan-rumble
force-pushed
the
feat/object-mapper-output-dto-write-operation-support
branch
from
November 17, 2026 12:16
f25a7ed to
7ba99e9
Compare
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.
Rebased on 5.0 and reduced to what #8849 keeps from the original patch. The provider change (map to the resource class on unsafe methods) was merged on its own in #8848 and is no longer part of this PR.
Problem
On a resource mapped to an entity (
stateOptions+#[Map]),ObjectMapperOutputProcessormaps the persisted entity to the resource class, also when the operation declares anoutput. APOSTorPATCHanswers with the resource shape while OpenAPI and Hydra document the output class.What this PR changes
ObjectMapperOutputProcessormaps to the output class (getOutput()['class'] ?? getClass()), asObjectMapperProviderdoes on reads.LocationandContent-Locationare computed from the resource the operation acted on. When the output class is not the resource class, the processor also maps the entity to the resource class and stores it in themapped_resourcerequest attribute;HttpResponseHeadersTraitbuilds the IRI from it. No item template is needed for the headers.HttpResponseStatusTraitreads the same attribute, so aPUTwithallowCreatethat creates keeps answering 201 when anoutputis declared.outputis its own resource #8849).Operations without an
output, or whoseoutputis the resource class, are untouched: one mapping, same headers.What was dropped
The original patch set
item_uri_templateimplicitly from the operation's ownuriTemplateonPATCH/PUT(SerializerContextBuilderand the headers trait). #8849 refuses that, andPost('/books/{id}/discount', output: DiscountResult::class)shows why. Both changes are gone,SerializerContextBuilderis not touched anymore.Where this stands against #8849
ObjectMapperOutputProcessormaps to the output classLocationandContent-Locationare computed from the resourcemapped_resourcerequest attributeitemUriTemplateon everyHttpOperation, cascading from the resource@type, anonymous code paths)inputPoints that need a decision or differ from the RFC:
output(to the documented one). Happy to retarget tomain.mapped_resource. The request attribute is how the resource reachesRespondProcessor, the RFC does not say how. It is only set when the output class is not the resource class. Another mechanism is fine with me.PATCH/PUTis not possible yet. OnlyPosthasitemUriTemplatetoday, so until feat(metadata): add canonicalUriTemplate to resources and HTTP operations #8479 aPATCH/PUToutput is always "another thing": own type, generated@id. The headers still point at the resource.@typeof a view.Post(output: ..., itemUriTemplate: ...)already gives the@typeof the resource in JSON-LD. JSON:API gives the output short name astype(MappedOutputDto), that is left to the 6.0 step.genid(_links.self,data.id), also left to the 6.0 step and to the open question on JSON:API.mapped_resource: headers and status then follow the existing logic, they are not computed from the resource.Behavior to be aware of
map()call per write when anoutputdifferent from the resource class is declared, so#[Map]transforms run twice for that entity. The entity → resource mapping is the one every mapped write already performs today, no new mapping has to be declared.Unable to generate an IRI, this branch answers 201 with the output body and noLocation/Content-Location.Verified against 5.0
Same requests run against 5.0 and this branch, on
POST,PATCHandPUT(update and create):LocationandContent-Locationare identical to 5.0, only the body changes to the output class.USE_SYMFONY_LISTENERS=1): no regression. 5.0 sends noLocation/Content-Locationon mapped writes in that mode (with or withoutoutput), operations with anoutputnow get them. Operations without anoutputare left as they are, that looks like a separate bug.Not run: Laravel (same processor and
RespondProcessorchain, but the workbench has no mapped write with anoutput) and MongoDB ODM. GraphQL mutations have their own processor chain and are not affected, as noted in #8849.Tests
tests/Functional/MappedResourceOutputTest.php(new fixtures only):POSTwithoutput+itemUriTemplate: body is the output DTO,@id,LocationandContent-Locationare the item IRI.PATCHwithoutput: body is the output DTO and is not given the IRI of the resource,Content-Locationis the item IRI, unsent fields are preserved in the database.PUTwithoutputandallowCreate: 201 with the output DTO as body.ObjectMapperOutputProcessorTest: maps to the declared output class, setsmapped_resourcewhen the output differs, does not map data that already is the output class.