@@ -204,7 +204,7 @@ public function validateResponseBody(ResponseInterface &$response, string $path,
204
204
if (null === $ responseBodyData && $ responseSchema ) {
205
205
return [['name ' => 'responseBody ' , 'code ' => 'error_required ' ]];
206
206
}
207
- $ errors = $ this ->validateObject ($ responseSchema , $ responseBodyData );
207
+ $ errors = $ this ->validateObject ($ responseSchema , json_encode ( $ responseBodyData, JSON_PRESERVE_ZERO_FRACTION ) );
208
208
if ($ this ->options ['stripResponse ' ]) {
209
209
$ notAdditionalOrNullErrors = [];
210
210
foreach ($ errors as $ error ) {
@@ -250,7 +250,14 @@ public function validateRequest(ServerRequestInterface &$request, string $path,
250
250
if ($ requestBody && $ requestMediaType = $ requestBody ->getContent ($ mediaType )) {
251
251
if (null === $ requestBodyData && $ requestBody ->required ) {
252
252
$ 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
+ }
254
261
$ errors = array_merge ($ errors , $ this ->validateObject ($ requestMediaType ->schema , $ requestBodyData ));
255
262
} elseif ('multipart/form-data ' === $ mediaType ) {
256
263
$ errors = array_merge ($ errors , $ this ->validateFormData ($ requestMediaType ->schema , $ requestMediaType ->encoding , $ request ));
@@ -364,7 +371,7 @@ private function validateProperties(array $properties) : ?array
364
371
return $ errors ;
365
372
}
366
373
367
- private function validateObject (array $ schema , array $ value ) : array
374
+ private function validateObject (array $ schema , string $ value ) : array
368
375
{
369
376
$ errors = [];
370
377
foreach (SchemaHelper::getFormats ($ schema ) as $ f ) {
@@ -374,7 +381,7 @@ private function validateObject(array $schema, array $value) : array
374
381
$ validator ->setFormats ($ this ->formatContainer );
375
382
$ schema = SchemaHelper::openApiToJsonSchema ($ schema );
376
383
try {
377
- $ value = json_decode (json_encode ( $ value, JSON_PRESERVE_ZERO_FRACTION ) );
384
+ $ value = json_decode ($ value );
378
385
$ schema = json_decode (json_encode ($ schema , JSON_PRESERVE_ZERO_FRACTION ));
379
386
$ result = $ validator ->dataValidation ($ value , $ schema , 99 );
380
387
} catch (Exception $ e ) {
0 commit comments