Skip to content

Commit dd9357f

Browse files
committed
Create a resource with a falsy input
1 parent f6e4c59 commit dd9357f

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
@@ -242,3 +242,16 @@ Feature: DTO input and output
242242
}
243243
}
244244
"""
245+
246+
@createSchema
247+
Scenario: Create a resource with no input
248+
When I add "Content-Type" header equal to "application/ld+json"
249+
And I send a "POST" request to "/dummy_dto_no_inputs" with body:
250+
"""
251+
{
252+
"foo": "test",
253+
"bar": 1
254+
}
255+
"""
256+
Then the response status code should be 201
257+
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)