Skip to content

Commit b1b60ab

Browse files
committed
Fix #7599 Allow override of CsvEncoder::AS_COLLECTION_KEY in context
1 parent c099892 commit b1b60ab

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Serializer/SerializerContextBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function createFromRequest(Request $request, bool $normalization, ?array
107107
}
108108

109109
if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
110-
$context[CsvEncoder::AS_COLLECTION_KEY] = false;
110+
$context[CsvEncoder::AS_COLLECTION_KEY] = $context[CsvEncoder::AS_COLLECTION_KEY] ?? false;
111111
}
112112
}
113113
if ($operation->getCollectDenormalizationErrors() ?? false) {

src/Serializer/Tests/SerializerContextBuilderTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPUnit\Framework\TestCase;
2626
use Prophecy\PhpUnit\ProphecyTrait;
2727
use Symfony\Component\HttpFoundation\Request;
28+
use Symfony\Component\Serializer\Encoder\CsvEncoder;
2829

2930
/**
3031
* @author Kévin Dunglas <[email protected]>
@@ -36,6 +37,7 @@ class SerializerContextBuilderTest extends TestCase
3637
private SerializerContextBuilder $builder;
3738
private HttpOperation $operation;
3839
private HttpOperation $patchOperation;
40+
private HttpOperation $patchCollectionOperation;
3941

4042
protected function setUp(): void
4143
{
@@ -49,10 +51,14 @@ protected function setUp(): void
4951
]),
5052
]);
5153

52-
$this->patchOperation = new Patch(inputFormats: ['json' => ['application/merge-patch+json']], name: 'patch');
54+
$this->patchOperation = new Patch(inputFormats: ['json' => ['application/merge-patch+json'], 'csv' => ['text/csv']], name: 'patch');
55+
$this->patchCollectionOperation = $this->patchOperation
56+
->withDenormalizationContext([CsvEncoder::AS_COLLECTION_KEY => true])
57+
->withName('patch_collection');
5358
$resourceMetadataWithPatch = new ResourceMetadataCollection('Foo', [
5459
new ApiResource(operations: [
5560
'patch' => $this->patchOperation,
61+
'patch_collection' => $this->patchCollectionOperation,
5662
]),
5763
]);
5864

@@ -104,6 +110,18 @@ public function testCreateFromRequest(): void
104110
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml', 'id' => '1']);
105111
$expected = ['bar' => 'baz', 'operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/bars/1/foos', 'api_allow_update' => false, 'uri' => 'http://localhost/bars/1/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'operation' => $this->operation, 'skip_null_values' => true, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id'], 'skip_null_to_one_relations' => true];
106112
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
113+
114+
$request = Request::create('/foowithpatch/1', 'PATCH', server: ['CONTENT_TYPE' => 'text/csv']);
115+
$request->attributes->replace(['_api_resource_class' => 'FooWithPatch', '_api_operation_name' => 'patch', '_api_format' => 'csv', '_api_mime_type' => 'text/csv']);
116+
$expected = ['operation_name' => 'patch', 'resource_class' => 'FooWithPatch', 'request_uri' => '/foowithpatch/1', 'api_allow_update' => true, 'uri' => 'http://localhost/foowithpatch/1', 'output' => null, 'input' => null, 'deep_object_to_populate' => true, 'skip_null_values' => true, 'iri_only' => false, 'operation' => $this->patchOperation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id'], 'skip_null_to_one_relations' => true];
117+
$expected[CsvEncoder::AS_COLLECTION_KEY] = false;
118+
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
119+
120+
$request = Request::create('/foowithpatch/1', 'PATCH', server: ['CONTENT_TYPE' => 'text/csv']);
121+
$request->attributes->replace(['_api_resource_class' => 'FooWithPatch', '_api_operation_name' => 'patch_collection', '_api_format' => 'csv', '_api_mime_type' => 'text/csv']);
122+
$expected = ['operation_name' => 'patch_collection', 'resource_class' => 'FooWithPatch', 'request_uri' => '/foowithpatch/1', 'api_allow_update' => true, 'uri' => 'http://localhost/foowithpatch/1', 'output' => null, 'input' => null, 'deep_object_to_populate' => true, 'skip_null_values' => true, 'iri_only' => false, 'operation' => $this->patchCollectionOperation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id'], 'skip_null_to_one_relations' => true];
123+
$expected[CsvEncoder::AS_COLLECTION_KEY] = true;
124+
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
107125
}
108126

109127
public function testThrowExceptionOnInvalidRequest(): void

0 commit comments

Comments
 (0)