Skip to content

Commit

Permalink
Fix filterProperties when data does not contain properties (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi authored Jan 28, 2024
1 parent 72bf886 commit 73f96b4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Validation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ protected function filterProperties(object $data, ?string $mode = null): object
return $data;
}

if (! isset($data->properties)) {
return $data;
}

/**
* Create a new array of properties that need to be filtered out.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/Arrays.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,21 @@ paths:
name: orgUuid
in: path
required: true
/array-of-strings:
get:
summary: Get arrays of string
tags: []
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
type: string
components:
schemas: {}
38 changes: 38 additions & 0 deletions tests/ResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,44 @@ public function test_response_fails_with_invalid_array(): void
]);
}

/**
* @dataProvider arrayOfStringsProvider
*/
public function test_array_of_strings(mixed $payload, bool $isValid): void
{
Spectator::using('Arrays.v1.yaml');

Route::get('/array-of-strings', static function () use ($payload) {
return ['data' => $payload];
})->middleware(Middleware::class);

if ($isValid) {
$this->getJson('/array-of-strings')
->assertValidResponse();
} else {
$this->getJson('/array-of-strings')
->assertInvalidResponse();
}
}

public static function arrayOfStringsProvider(): array
{
return [
'valid' => [
['foo', 'bar'],
true,
],
'invalid as string' => [
'foo',
false,
],
'invalid as object' => [
['foo' => 'bar'],
false,
],
];
}

public function test_array_any_of(): void
{
Spectator::using('ArrayAnyOf.v1.yml');
Expand Down

0 comments on commit 73f96b4

Please sign in to comment.