Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Schema/Keywords/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\OpenAPIValidation\Schema\Exception\KeywordMismatch;

use function in_array;
use function is_string;
use function is_array;

class Nullable extends BaseKeyword
{
Expand All @@ -27,6 +27,6 @@ public function validate($data, bool $nullable): void

public function nullableByType(): bool
{
return ! is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
return is_array($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
}
}
80 changes: 80 additions & 0 deletions tests/FromCommunity/IssueNullablePropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\Request;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;

use function json_encode;

final class IssueNullablePropertyTest extends BaseValidatorTest
{
public function testNullableFalseThrowInvalidBody(): void
{
$json = /** @lang json */
<<<JSON
{
"openapi": "3.0.0",
"paths": {
"/api/data": {
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"value"
],
"properties": {
"value": {
"nullable": false,
"oneOf": [
{"type": "string"},
{"type": "boolean"}
]
}
}
}
}
}
},
"responses": {
"200": {
"description": "A response",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
JSON;

$requestValidator = (new ValidatorBuilder())
->fromJson($json)
->getRequestValidator();

$request = new Request(
'POST',
'headers:/api/data',
['Content-Type' => 'application/json'],
json_encode(['value' => null])
);

$this->expectException(InvalidBody::class);

$requestValidator->validate($request);
}
}