1414namespace ApiPlatform \Core \EventListener ;
1515
1616use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
17+ use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
1718use ApiPlatform \Core \Util \RequestAttributesExtractor ;
1819use Symfony \Component \HttpFoundation \Response ;
1920use Symfony \Component \HttpKernel \Event \GetResponseForControllerResultEvent ;
@@ -51,7 +52,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
5152
5253 return ;
5354 }
54- if ($ controllerResult instanceof Response || !($ attributes ['respond ' ] ?? $ request ->attributes ->getBoolean ('_api_respond ' , false ))) {
55+ if ($ controllerResult instanceof Response || !($ attributes ['respond ' ] ?? $ request ->attributes ->getBoolean ('_api_respond ' ))) {
5556 return ;
5657 }
5758
@@ -78,6 +79,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
7879 $ headers ['Sunset ' ] = (new \DateTimeImmutable ($ sunset ))->format (\DateTime::RFC1123 );
7980 }
8081
82+ $ headers = $ this ->addAcceptPatchHeader ($ headers , $ attributes , $ resourceMetadata );
8183 $ status = $ resourceMetadata ->getOperationAttribute ($ attributes , 'status ' );
8284 }
8385
@@ -87,4 +89,29 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
8789 $ headers
8890 ));
8991 }
92+
93+ private function addAcceptPatchHeader (array $ headers , array $ attributes , ResourceMetadata $ resourceMetadata ): array
94+ {
95+ if (!isset ($ attributes ['item_operation_name ' ])) {
96+ return $ headers ;
97+ }
98+
99+ $ patchMimeTypes = [];
100+ foreach ($ resourceMetadata ->getItemOperations () as $ operation ) {
101+ if ('PATCH ' !== ($ operation ['method ' ] ?? '' ) || !isset ($ operation ['input_formats ' ])) {
102+ continue ;
103+ }
104+
105+ foreach ($ operation ['input_formats ' ] as $ mimeTypes ) {
106+ foreach ($ mimeTypes as $ mimeType ) {
107+ $ patchMimeTypes [] = $ mimeType ;
108+ }
109+ }
110+ $ headers ['Accept-Patch ' ] = implode (', ' , $ patchMimeTypes );
111+
112+ return $ headers ;
113+ }
114+
115+ return $ headers ;
116+ }
90117}
0 commit comments