Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 8b15569

Browse files
committed
Allow also simple lists as result
Don't enforce the result to be a json only having objects, also allow lists
1 parent 704c07a commit 8b15569

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/Dispatch/MethodDispatcher.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ public function dispatch(
6464

6565
$response = $handler->handle($request, $input->parameter);
6666

67-
/** @var stdClass $decodedResponse */
68-
$decodedResponse = (object) Helper::toJSON($response);
69-
7067
$this->methodValidator->validateOutput(
7168
$schemaContent,
72-
$decodedResponse
69+
Helper::toJSON($response)
7370
);
7471

7572
return $response;

src/Dispatch/MethodValidator.php

+21-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
final class MethodValidator implements MethodValidatorInterface
1616
{
17-
public function __construct(private Validator $schemaValidator, private ErrorFormatter $errorFormatter)
18-
{
17+
public function __construct(
18+
private Validator $schemaValidator,
19+
private ErrorFormatter $errorFormatter
20+
) {
1921
}
2022

2123
/**
@@ -45,21 +47,30 @@ public function validateInput(
4547
*/
4648
public function validateOutput(
4749
stdClass $methodSchemaContent,
48-
stdClass $output
50+
$output
4951
): void {
5052
if (property_exists($methodSchemaContent->properties, 'response') === true) {
53+
$data = new stdClass();
54+
$data->data = $output;
55+
56+
// Wrap the response schema
57+
$response = (object) [
58+
'type' => 'object',
59+
'properties' => (object) [
60+
'data' => $methodSchemaContent->properties->response,
61+
],
62+
'required' => ['data']
63+
];
64+
5165
// Validate the response against the response definition in method schema
5266
$validationResult = $this->schemaValidator->validate(
53-
$output,
54-
$methodSchemaContent->properties->response
67+
$data,
68+
$response
5569
);
5670

71+
$error = $validationResult->error();
5772
// Throw exception if the input does not validate against the basic request schema
58-
if ($validationResult->isValid() === false) {
59-
60-
/** @var ValidationError $error */
61-
$error = $validationResult->error();
62-
73+
if ($error !== null) {
6374
throw new ResponseMalformedException(
6475
'Internal Server Error',
6576
StatusCode::INTERNAL_SERVER_ERROR,

src/Dispatch/MethodValidatorInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public function validateInput(
1717
): void;
1818

1919
/**
20+
* @param stdClass|array<mixed> $output
21+
*
2022
* @throws ResponseMalformedException
2123
*/
2224
public function validateOutput(
2325
stdClass $methodSchemaContent,
24-
stdClass $output
26+
$output
2527
): void;
2628
}

tests/Dispatch/MethodValidatorTest.php

+27-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testValidateInputThrowsExceptionIfInputDoesNotValidate(): void
5252
->with(
5353
$parameter,
5454
Mockery::on(static function ($value) use ($schemaParameter): bool {
55-
return (array) $value === $schemaParameter;
55+
return json_encode($value) === json_encode($schemaParameter);
5656
})
5757
)
5858
->once()
@@ -115,18 +115,24 @@ public function testValidateOutputThrowsExceptionIfOutputDoesNotValidate(): void
115115

116116
$this->schemaValidator->shouldReceive('validate')
117117
->with(
118-
$output,
118+
Mockery::on(static function ($value) use ($output): bool {
119+
return json_encode($value) === json_encode(['data' => $output]);
120+
}),
119121
Mockery::on(static function ($value) use ($schemaParameter): bool {
120-
return (array) $value === $schemaParameter;
122+
$schemaParameter = [
123+
'type' => 'object',
124+
'properties' => [
125+
'data' => $schemaParameter,
126+
],
127+
'required' => ['data']
128+
129+
];
130+
return json_encode($value) === json_encode($schemaParameter);
121131
})
122132
)
123133
->once()
124134
->andReturn($validationResult);
125135

126-
$validationResult->shouldReceive('isValid')
127-
->withNoArgs()
128-
->once()
129-
->andReturnFalse();
130136
$validationResult->shouldReceive('error')
131137
->withNoArgs()
132138
->once()
@@ -153,18 +159,28 @@ public function testValidateOutputValidates(): void
153159

154160
$this->schemaValidator->shouldReceive('validate')
155161
->with(
156-
$output,
162+
Mockery::on(static function ($value) use ($output): bool {
163+
return json_encode($value) === json_encode(['data' => $output]);
164+
}),
157165
Mockery::on(static function ($value) use ($schemaParameter): bool {
158-
return (array) $value === $schemaParameter;
166+
$schemaParameter = [
167+
'type' => 'object',
168+
'properties' => [
169+
'data' => $schemaParameter,
170+
],
171+
'required' => ['data']
172+
173+
];
174+
return json_encode($value) === json_encode($schemaParameter);
159175
})
160176
)
161177
->once()
162178
->andReturn($validationResult);
163179

164-
$validationResult->shouldReceive('isValid')
180+
$validationResult->shouldReceive('error')
165181
->withNoArgs()
166182
->once()
167-
->andReturnTrue();
183+
->andReturnNull();
168184

169185
$this->subject->validateOutput(
170186
$content,

0 commit comments

Comments
 (0)