Skip to content

Commit 743dbb0

Browse files
authored
Merge pull request #2633 from soyuka/test-input-false
Create a resource with a falsy input
2 parents b82607b + dd9357f commit 743dbb0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

features/main/input_output.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ Feature: DTO input and output
232232
}
233233
}
234234
"""
235+
236+
@createSchema
237+
Scenario: Create a resource with no input
238+
When I add "Content-Type" header equal to "application/ld+json"
239+
And I send a "POST" request to "/dummy_dto_no_inputs" with body:
240+
"""
241+
{
242+
"foo": "test",
243+
"bar": 1
244+
}
245+
"""
246+
Then the response status code should be 201
247+
And the response should be empty

src/EventListener/SerializeListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
6161

6262
$context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
6363

64-
if (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) {
64+
if (
65+
(isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class'])
66+
||
67+
(
68+
null === $controllerResult && isset($context['input']) && \array_key_exists('class', $context['input']) &&
69+
null === $context['input']['class']
70+
)
71+
) {
6572
$event->setControllerResult('');
6673

6774
return;

src/Validator/EventListener/ValidateListener.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
5252
return;
5353
}
5454

55-
$data = $event->getControllerResult();
5655
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
57-
$validationGroups = $resourceMetadata->getOperationAttribute($attributes, 'validation_groups', null, true);
56+
$inputMetadata = $resourceMetadata->getOperationAttribute($attributes, 'input', [], true);
57+
if (\array_key_exists('class', $inputMetadata) && null === $inputMetadata['class']) {
58+
return;
59+
}
5860

59-
$this->validator->validate($data, ['groups' => $validationGroups]);
61+
$validationGroups = $resourceMetadata->getOperationAttribute($attributes, 'validation_groups', null, true);
62+
$this->validator->validate($event->getControllerResult(), ['groups' => $validationGroups]);
6063
}
6164
}

0 commit comments

Comments
 (0)