Skip to content

Commit 8a05085

Browse files
francescozanonihkarlstrom
authored andcommitted
Added JSON_PRESERVE_ZERO_FRACTION (#14)
Data conversion to JSON: .0 decimal part of a number is kept
1 parent cdec710 commit 8a05085

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/OpenApiValidation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function validateResponseBody(ResponseInterface &$response, string $path,
210210
}
211211
}
212212
$errors = $notAdditionalOrNullErrors;
213-
$response = $response->withBody((new StreamFactory())->createStream(json_encode($responseBodyData)));
213+
$response = $response->withBody((new StreamFactory())->createStream(json_encode($responseBodyData, JSON_PRESERVE_ZERO_FRACTION)));
214214
}
215215
return $errors;
216216
}
@@ -352,8 +352,8 @@ private function validateObject(array $schema, array $value) : array
352352
$validator->setFormats($this->formatContainer);
353353
$schema = SchemaHelper::openApiToJsonSchema($schema);
354354
try {
355-
$value = json_decode(json_encode($value));
356-
$schema = json_decode(json_encode($schema));
355+
$value = json_decode(json_encode($value, JSON_PRESERVE_ZERO_FRACTION));
356+
$schema = json_decode(json_encode($schema, JSON_PRESERVE_ZERO_FRACTION));
357357
$result = $validator->dataValidation($value, $schema, 99);
358358
} catch (Exception $e) {
359359
return [[
@@ -453,7 +453,7 @@ private function setExampleResponse(ServerRequestInterface $request, ResponseInt
453453
$exampleResponseBodyData = array_merge($exampleResponseBodyData, $requestBodyData);
454454
}
455455
$response = $response
456-
->withBody((new StreamFactory())->createStream(json_encode($exampleResponseBodyData)))
456+
->withBody((new StreamFactory())->createStream(json_encode($exampleResponseBodyData, JSON_PRESERVE_ZERO_FRACTION)))
457457
->withHeader('Content-Type', $mediaType.';charset=utf-8');
458458
if (is_numeric($responseObject->statusCode)) {
459459
$response = $response->withStatus($responseObject->statusCode);
@@ -476,7 +476,7 @@ private function error(int $code, string $message, array $errors = []) : Respons
476476
$json['errors'] = $errors;
477477
}
478478
$response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
479-
return $response->withBody((new StreamFactory())->createStream(json_encode($json)));
479+
return $response->withBody((new StreamFactory())->createStream(json_encode($json, JSON_PRESERVE_ZERO_FRACTION)));
480480
}
481481

482482
private function parseErrors(ValidationError $error, $name = null, $in = null) : array

src/OpenApiValidation/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(string $name, string $in, bool $required, $schema, $
2727
$this->name = $name;
2828
$this->in = $in;
2929
$this->required = $required;
30-
$this->schema = json_decode(json_encode($schema));
30+
$this->schema = json_decode(json_encode($schema, JSON_PRESERVE_ZERO_FRACTION));
3131
$this->value = $value ?? null;
3232
if (null == $this->value) {
3333
return;

0 commit comments

Comments
 (0)