1717use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \FirstResource ;
1818use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResource ;
1919use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResourceOdm ;
20+ use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResourceSourceOnly ;
2021use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResourceWithInput ;
2122use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResourceWithRelation ;
2223use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \MappedResourceWithRelationRelated ;
2324use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \SecondResource ;
2425use ApiPlatform \Tests \Fixtures \TestBundle \Document \MappedDocument ;
2526use ApiPlatform \Tests \Fixtures \TestBundle \Entity \MappedEntity ;
27+ use ApiPlatform \Tests \Fixtures \TestBundle \Entity \MappedEntitySourceOnly ;
2628use ApiPlatform \Tests \Fixtures \TestBundle \Entity \MappedResourceWithRelationEntity ;
2729use ApiPlatform \Tests \Fixtures \TestBundle \Entity \MappedResourceWithRelationRelatedEntity ;
2830use ApiPlatform \Tests \Fixtures \TestBundle \Entity \SameEntity ;
@@ -49,6 +51,7 @@ public static function getResources(): array
4951 MappedResourceWithRelation::class,
5052 MappedResourceWithRelationRelated::class,
5153 MappedResourceWithInput::class,
54+ MappedResourceSourceOnly::class,
5255 ];
5356 }
5457
@@ -167,6 +170,52 @@ public function testShouldNotMapWhenInput(): void
167170 $ this ->assertJsonContains (['username ' => 'test ' ]);
168171 }
169172
173+ public function testShouldMapWithSourceOnly (): void
174+ {
175+ if ($ this ->isMongoDB ()) {
176+ $ this ->markTestSkipped ('MongoDB not tested ' );
177+ }
178+
179+ if (!$ this ->getContainer ()->has ('api_platform.object_mapper ' )) {
180+ $ this ->markTestSkipped ('ObjectMapper not installed ' );
181+ }
182+
183+ $ this ->recreateSchema ([MappedEntitySourceOnly::class]);
184+ $ manager = $ this ->getManager ();
185+
186+ for ($ i = 0 ; $ i < 10 ; ++$ i ) {
187+ $ e = new MappedEntitySourceOnly ();
188+ $ e ->setLastName ('A ' .$ i );
189+ $ e ->setFirstName ('B ' .$ i );
190+ $ manager ->persist ($ e );
191+ }
192+
193+ $ manager ->flush ();
194+
195+ $ r = self ::createClient ()->request ('GET ' , '/mapped_resource_source_onlies ' );
196+ $ this ->assertJsonContains (['member ' => [
197+ ['username ' => 'B0 A0 ' ],
198+ ['username ' => 'B1 A1 ' ],
199+ ['username ' => 'B2 A2 ' ],
200+ ]]);
201+
202+ $ r = self ::createClient ()->request ('POST ' , 'mapped_resource_source_onlies ' , ['json ' => ['username ' => 'so yuka ' ]]);
203+ $ this ->assertJsonContains (['username ' => 'so yuka ' ]);
204+
205+ $ manager = $ this ->getManager ();
206+ $ repo = $ manager ->getRepository (MappedEntitySourceOnly::class);
207+ $ persisted = $ repo ->findOneBy (['id ' => $ r ->toArray ()['id ' ]]);
208+ $ this ->assertSame ('so ' , $ persisted ->getFirstName ());
209+ $ this ->assertSame ('yuka ' , $ persisted ->getLastName ());
210+
211+ $ uri = $ r ->toArray ()['@id ' ];
212+ self ::createClient ()->request ('GET ' , $ uri );
213+ $ this ->assertJsonContains (['username ' => 'so yuka ' ]);
214+
215+ $ r = self ::createClient ()->request ('PATCH ' , $ uri , ['json ' => ['username ' => 'ba zar ' ], 'headers ' => ['content-type ' => 'application/merge-patch+json ' ]]);
216+ $ this ->assertJsonContains (['username ' => 'ba zar ' ]);
217+ }
218+
170219 private function loadFixtures (): void
171220 {
172221 $ manager = $ this ->getManager ();
0 commit comments