@@ -103,6 +103,39 @@ public function testShouldMapBetweenResourceAndEntity(): void
103103 $ this ->assertResponseStatusCodeSame (204 );
104104 }
105105
106+ /**
107+ * When an API resource has multiple #[Map] targets (e.g. MappedEntity + AnotherMappedObject),
108+ * the ObjectMapperProcessor must resolve the correct target using stateOptions during POST.
109+ */
110+ public function testPostWithMultipleMapTargetsResolvesCorrectEntity (): void
111+ {
112+ if ($ this ->isMongoDB ()) {
113+ $ this ->markTestSkipped ('MongoDB not tested. ' );
114+ }
115+
116+ if (!$ this ->getContainer ()->has ('api_platform.object_mapper ' )) {
117+ $ this ->markTestSkipped ('ObjectMapper not installed ' );
118+ }
119+
120+ $ this ->recreateSchema ([MappedEntity::class]);
121+ $ client = self ::createClient ();
122+ $ r = $ client ->request ('POST ' , 'mapped_resources ' , ['json ' => ['username ' => 'multi target ' ]]);
123+
124+ $ this ->assertResponseStatusCodeSame (201 );
125+ $ this ->assertJsonContains (['username ' => 'multi target ' ]);
126+
127+ // Verify the mapped_data is the entity from stateOptions, not AnotherMappedObject
128+ $ mappedData = $ client ->getKernelBrowser ()->getRequest ()->attributes ->get ('mapped_data ' );
129+ $ this ->assertInstanceOf (MappedEntity::class, $ mappedData , 'ObjectMapper should resolve to the stateOptions entity class, not the first #[Map] target. ' );
130+
131+ // Verify persistence
132+ $ repo = $ this ->getManager ()->getRepository (MappedEntity::class);
133+ $ persisted = $ repo ->findOneBy (['id ' => $ r ->toArray ()['id ' ]]);
134+ $ this ->assertNotNull ($ persisted );
135+ $ this ->assertSame ('multi ' , $ persisted ->getFirstName ());
136+ $ this ->assertSame ('target ' , $ persisted ->getLastName ());
137+ }
138+
106139 public function testShouldMapToTheCorrectResource (): void
107140 {
108141 if ($ this ->isMongoDB ()) {
0 commit comments