Skip to content

Commit 2bc8286

Browse files
lezhnev74hkarlstrom
authored andcommitted
Add a failing test for nullable values (#12)
Fix #11
1 parent 45fc280 commit 2bc8286

File tree

5 files changed

+52
-39
lines changed

5 files changed

+52
-39
lines changed

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/OpenApiValidation.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,7 @@ private function validateRequest(ServerRequestInterface &$request, string $path,
260260
switch ($mediaType) {
261261
case 'application/json':
262262
if (!empty($requestBodyData)) {
263-
$objectErrors = $this->validateObject($requestMediaType->schema, $requestBodyData);
264-
foreach ($objectErrors as $error) {
265-
if (!('error_type' == $error['code']
266-
&& 'null' == $error['used']
267-
&& SchemaHelper::isNullable($requestMediaType->schema, explode('.', $error['name'])))) {
268-
$errors[] = $error;
269-
}
270-
}
263+
$errors = $this->validateObject($requestMediaType->schema, $requestBodyData);
271264
}
272265
break;
273266
case 'multipart/form-data':
@@ -364,7 +357,7 @@ private function validateObject(array $schema, array $value) : array
364357
}
365358
$validator = new Validator();
366359
$validator->setFormats($this->formatContainer);
367-
$schema = SchemaHelper::mergeAllOf($schema);
360+
$schema = SchemaHelper::openApiToJsonSchema($schema);
368361
try {
369362
$value = json_decode(json_encode($value));
370363
$schema = json_decode(json_encode($schema));

src/OpenApiValidation/Helpers/Schema.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@
1313

1414
class Schema
1515
{
16-
public static function addRecursive(array $schema, string $attribute, $value) : array
17-
{
18-
if (($schema['type'] ?? null) === 'object') {
19-
$schema[$attribute] = $value;
20-
}
21-
foreach ($schema as $attr => $val) {
22-
if (is_array($val)) {
23-
$schema[$attr] = self::addRecursive($schema[$attr], $attribute, $value);
24-
}
25-
}
26-
return $schema;
27-
}
28-
29-
public static function mergeAllOf(array $schema) : array
16+
public static function openApiToJsonSchema(array $schema) : array
3017
{
3118
if (isset($schema['allOf']) && is_array($schema['allOf'])) {
3219
$allOf = $schema['allOf'];
@@ -38,9 +25,13 @@ public static function mergeAllOf(array $schema) : array
3825
}
3926
$schema = \Ckr\Util\ArrayMerger::doMerge($schema, $merged);
4027
}
28+
if ($schema['nullable'] ?? false) {
29+
unset($schema['nullable']);
30+
$schema['type'] = [$schema['type'], 'null'];
31+
}
4132
foreach ($schema as $attr => $val) {
4233
if (is_array($val)) {
43-
$schema[$attr] = self::mergeAllOf($val);
34+
$schema[$attr] = self::openApiToJsonSchema($val);
4435
}
4536
}
4637
return $schema;
@@ -94,9 +85,4 @@ public static function getFormats(array $schema) : array
9485
$callback($schema, $formats);
9586
return $formats;
9687
}
97-
98-
public static function isNullable(array $schema, array $path) : bool
99-
{
100-
return true;
101-
}
10288
}

tests/ResponsesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,16 @@ public function testResponseInvalidHeaderFormat()
116116
$this->assertSame('string', $error['used']);
117117
$this->assertSame('header', $error['in']);
118118
}
119+
120+
public function testResponseWithNullableBodyAttributes()
121+
{
122+
$response = $this->response('get', '/response/nullable', [
123+
'customHandler' => function ($request, ResponseInterface $response) {
124+
return $response->withJson(['ok' => null]);
125+
},
126+
]);
127+
$json = $this->json($response);
128+
$this->assertSame(200, $response->getStatusCode());
129+
$this->assertSame(null, $json['ok']);
130+
}
119131
}

tests/testapi.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,28 @@
657657
}
658658
}
659659
}
660+
},
661+
"/response/nullable": {
662+
"get": {
663+
"operationId": "getNullableResponse",
664+
"responses": {
665+
"200": {
666+
"content": {
667+
"application/json": {
668+
"schema": {
669+
"type": "object",
670+
"properties": {
671+
"ok": {
672+
"type": "boolean",
673+
"nullable": true
674+
}
675+
}
676+
}
677+
}
678+
}
679+
}
680+
}
681+
}
660682
}
661683
}
662684
}

0 commit comments

Comments
 (0)