Skip to content

Commit 1e014f1

Browse files
committed
Fix #31
1 parent 68ca650 commit 1e014f1

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/OpenApiValidation.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function validateResponseBody(ResponseInterface &$response, string $path,
204204
if (null === $responseBodyData && $responseSchema) {
205205
return [['name' => 'responseBody', 'code' => 'error_required']];
206206
}
207-
$errors = $this->validateObject($responseSchema, $responseBodyData);
207+
$errors = $this->validateObject($responseSchema, json_encode($responseBodyData, JSON_PRESERVE_ZERO_FRACTION));
208208
if ($this->options['stripResponse']) {
209209
$notAdditionalOrNullErrors = [];
210210
foreach ($errors as $error) {
@@ -250,7 +250,14 @@ public function validateRequest(ServerRequestInterface &$request, string $path,
250250
if ($requestBody && $requestMediaType = $requestBody->getContent($mediaType)) {
251251
if (null === $requestBodyData && $requestBody->required) {
252252
$errors[] = ['name' => 'requestBody', 'code' => 'error_required'];
253-
} elseif ($requestBodyData && $this->isJsonMediaType($mediaType)) {
253+
} elseif (null !== $requestBodyData && $this->isJsonMediaType($mediaType)) {
254+
255+
if (empty($requestBodyData)) {
256+
// We don't know if the empty request body was an array [] or a object {}, both are decoded to [] by json_decode
257+
$requestBodyData = $request->getBody();
258+
} else {
259+
$requestBodyData = json_encode($requestBodyData, JSON_PRESERVE_ZERO_FRACTION);
260+
}
254261
$errors = array_merge($errors, $this->validateObject($requestMediaType->schema, $requestBodyData));
255262
} elseif ('multipart/form-data' === $mediaType) {
256263
$errors = array_merge($errors, $this->validateFormData($requestMediaType->schema, $requestMediaType->encoding, $request));
@@ -364,7 +371,7 @@ private function validateProperties(array $properties) : ?array
364371
return $errors;
365372
}
366373

367-
private function validateObject(array $schema, array $value) : array
374+
private function validateObject(array $schema, string $value) : array
368375
{
369376
$errors = [];
370377
foreach (SchemaHelper::getFormats($schema) as $f) {
@@ -374,7 +381,7 @@ private function validateObject(array $schema, array $value) : array
374381
$validator->setFormats($this->formatContainer);
375382
$schema = SchemaHelper::openApiToJsonSchema($schema);
376383
try {
377-
$value = json_decode(json_encode($value, JSON_PRESERVE_ZERO_FRACTION));
384+
$value = json_decode($value);
378385
$schema = json_decode(json_encode($schema, JSON_PRESERVE_ZERO_FRACTION));
379386
$result = $validator->dataValidation($value, $schema, 99);
380387
} catch (Exception $e) {

tests/BaseTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ protected function response(string $method, string $uri, array $args = []) : Res
3939
]);
4040
$request = Request::createFromEnvironment($env);
4141
if (isset($args['body'])) {
42-
$request->getBody()->write(json_encode($args['body']));
42+
if (is_array($args['body'])) {
43+
$request->getBody()->write(json_encode($args['body']));
44+
} else {
45+
$request->getBody()->write($args['body']);
46+
}
4347
$request->getBody()->rewind();
4448
$request = $request->withHeader('Content-Type', $args['bodyContentType'] ?? 'application/json');
4549
}

tests/RequestBodyTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,17 @@ public function testHashMapObject()
188188

189189
public function testEmptyBody()
190190
{
191-
$response = $this->response('post', '/request/body/empty/required', ['body' => []]);
191+
$response = $this->response('post', '/request/body/empty/required');
192192
$json = $this->json($response);
193-
$this->assertTrue($json['ok']);
194-
$this->assertSame(200, $response->getStatusCode());
193+
$this->assertSame(400, $response->getStatusCode());
194+
$this->assertSame('requestBody', $json['errors'][0]['name']);
195+
$this->assertSame('error_required', $json['errors'][0]['code']);
196+
197+
$response = $this->response('post', '/request/body/empty/required', ['body' => '{}']);
198+
$json = $this->json($response);
199+
$this->assertSame(400, $response->getStatusCode());
200+
$this->assertSame('foo', $json['errors'][0]['name']);
201+
$this->assertSame('error_required', $json['errors'][0]['code']);
202+
$this->assertSame('body', $json['errors'][0]['in']);
195203
}
196204
}

tests/testapi.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
"application/json": {
250250
"schema": {
251251
"type": "object",
252+
"required":["foo"],
252253
"properties": {
253254
"foo": {
254255
"type": "string"
@@ -280,6 +281,7 @@
280281
"application/json": {
281282
"schema": {
282283
"type": "object",
284+
"required":["foo"],
283285
"properties": {
284286
"foo": {
285287
"type": "string"

0 commit comments

Comments
 (0)