Skip to content

Commit

Permalink
Merge pull request #33 from digilist/fix-empty-object-validation
Browse files Browse the repository at this point in the history
Fix validation of empty objects
  • Loading branch information
scaytrase authored Nov 6, 2019
2 parents d7e6c9c + a4a9e06 commit 5d2a74e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Schema/Keywords/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function validate($data, string $type, ?string $format = null) : void
{
switch ($type) {
case CebeType::OBJECT:
if (! is_object($data) && ! (is_array($data) && ArrayHelper::isAssoc($data))) {
if (! is_object($data) && ! (is_array($data) && ArrayHelper::isAssoc($data)) && $data !== []) {
throw TypeMismatch::becauseTypeDoesNotMatch(CebeType::OBJECT, $data);
}
break;
Expand Down
48 changes: 48 additions & 0 deletions tests/FromCommunity/EmptyObjectValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;
use function GuzzleHttp\Psr7\stream_for;

final class EmptyObjectValidationTest extends TestCase
{
/**
* @see https://github.com/lezhnev74/openapi-psr7-validator/issues/57
*/
public function testIssue57000() : void
{
$yaml = /** @lang yaml */
<<<YAML
openapi: 3.0.0
info:
title: Product import API
version: '1.0'
servers:
- url: 'http://localhost:8000/api/v1'
paths:
/products.create:
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
YAML;

$validator = (new ValidatorBuilder())->fromYaml($yaml)->getServerRequestValidator();

$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/v1/products.create'))
->withHeader('Content-Type', 'application/json')
->withBody(stream_for('{}'));

$validator->validate($psrRequest);

$this->addToAssertionCount(1);
}
}

0 comments on commit 5d2a74e

Please sign in to comment.