Skip to content

Commit 408813a

Browse files
author
=
committed
Fix nullable by type(3.1) and add open api 3.1 to the testing
1 parent 401f7e8 commit 408813a

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/Schema/Keywords/Nullable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ class Nullable extends BaseKeyword
1717
*/
1818
public function validate($data, bool $nullable): void
1919
{
20-
if (! $nullable && ($data === null)) {
20+
if (! $nullable && ($data === null) && !$this->nullableByType()) {
2121
throw KeywordMismatch::fromKeyword('nullable', $data, 'Value cannot be null');
2222
}
2323
}
24+
25+
/**
26+
* @return bool
27+
*/
28+
public function nullableByType(): bool
29+
{
30+
return !is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
31+
}
2432
}

tests/PSR7/BaseValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function makeGoodResponse(string $path, string $method): ResponseInter
2828
{
2929
switch ($method . ' ' . $path) {
3030
case 'get /path1':
31-
$body = ['propA' => 1];
31+
$body = ['propA' => 1, 'propD' => [1, 'string', null]];
3232

3333
return (new Response())
3434
->withHeader('Content-Type', 'application/json')

tests/stubs/api.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: 3.0.2
1+
openapi: 3.1.0
22
info:
33
title: Weather API
44
version: 0.0.1

tests/stubs/schemas.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ components:
1313
type: array
1414
items:
1515
type: string
16+
propD:
17+
type: array
18+
items:
19+
type:
20+
- string
21+
- integer
22+
- 'null'
1623
required:
1724
- propA
1825
- propB

0 commit comments

Comments
 (0)