Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"require": {
"php": ">=8.1",
"ext-json": "*",
"justinrainbow/json-schema": "^5.2.13",
"justinrainbow/json-schema": "^5.2.13 || ^6",
"nyholm/psr7": "^1.1",
"nyholm/psr7-server": "^1.0",
"psr/http-message": "^2.0",
Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ parameters:
count: 1
path: src/Adapters/Slim/OpenApiVerifierMiddleware.php

-
message: '#^Call to sprintf contains 2 placeholders, 1 value given\.$#'
identifier: argument.sprintf
count: 1
path: src/OpenApiSchemaMismatchException.php

-
message: '#^Call to function property_exists\(\) with \$this\(Radebatz\\OpenApi\\Verifier\\Tests\\Adapters\\LaravelAdapterTest\) and ''openapiSpecification'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
Expand Down
49 changes: 36 additions & 13 deletions src/OpenApiSchemaMismatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

namespace Radebatz\OpenApi\Verifier;

/**
* @phpstan-type JsonSchemaValidationError array{
* property: string,
* pointer: string,
* message: string,
* constraint: string|array{name: string, params: array<mixed>},
* context: int
* }
*/
class OpenApiSchemaMismatchException extends \Exception
{
protected $errors = [];
/** @var list<JsonSchemaValidationError> */
protected array $errors = [];

/**
* @return list<JsonSchemaValidationError>
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* @param list<JsonSchemaValidationError> $errors
*/
public function setErrors(array $errors): OpenApiSchemaMismatchException
{
$this->errors = $errors;
Expand All @@ -29,32 +45,39 @@ public function getErrorSummary(): ?string
foreach ($this->errors as $error) {
$wildcarded = preg_replace('/\[[0-9]+\]/', '[*]', $error['property']);

$errorType = $error['constraint'] . '|' . $error['message'];
// ensures compatibility with both, JsonSchema 5.x and 6.x
$constraintName = $error['constraint']['name']
?? $error['constraint'];

$errorType = sprintf('%s|%s', $constraintName, $error['message']);
if (!array_key_exists($errorType, $errorTypeMap)) {
$errorTypeMap[$errorType] = [
'properties' => [
$wildcarded => [$error['property']],
$wildcarded => 1,
],
'constraint' => $error['constraint'],
'constraint' => $constraintName,
'message' => $error['message'],
];
} else {
if (!in_array($wildcarded, $errorTypeMap[$errorType]['properties'])) {
$errorTypeMap[$errorType]['properties'][$wildcarded] = [$error['property']];
// track usage of the same wildcard-property
if (!array_key_exists($wildcarded, $errorTypeMap[$errorType]['properties'])) {
$errorTypeMap[$errorType]['properties'][$wildcarded] = 1;
} else {
$errorTypeMap[$errorType]['properties'][] = $wildcarded;
$errorTypeMap[$errorType]['properties'][$wildcarded]++;
}
}
}

$summary = [];
foreach ($errorTypeMap as $es) {
$summary[] = sprintf('%s - %s', $es['constraint'], $es['message']);
foreach ($es['properties'] as $ps => $pl) {
if (1 == count($pl)) {
$summary[] = sprintf(' - %s', $ps);
foreach ($errorTypeMap as $errorType) {
$summary[] = sprintf('%s - %s', $errorType['constraint'], $errorType['message']);

foreach ($errorType['properties'] as $propertyName => $propertyAmount) {
// if the property errored multiple times, add the amount to the error-message
if (1 === $propertyAmount) {
$summary[] = sprintf(' - %s', $propertyName);
} else {
$summary[] = sprintf(' - %s (%s more)', count($pl) - 1);
$summary[] = sprintf(' - %s (%s more)', $propertyName, $propertyAmount - 1);
}
}
}
Expand Down
143 changes: 143 additions & 0 deletions tests/OpenApiSchemaMismatchExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php declare(strict_types=1);

namespace Radebatz\OpenApi\Verifier\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Radebatz\OpenApi\Verifier\OpenApiSchemaMismatchException;

class OpenApiSchemaMismatchExceptionTest extends TestCase
{
private OpenApiSchemaMismatchException $subject;

protected function setUp(): void
{
$this->subject = new OpenApiSchemaMismatchException();
}

#[Test]
public function getErrorsReturnsSetData(): void
{
$value = [[
'property' => 'some-property',
'pointer' => 'some-pointer',
'message' => 'some-message',
'constraint' => 'some-constraint',
'context' => PHP_INT_MAX,
]];

$this->subject->setErrors($value);

static::assertSame(
$value,
$this->subject->getErrors(),
);
}

#[Test]
#[DataProvider('errorDataProvider')]
public function getErrorSummaryReturnsExpectedResult(
array $errors,
null|string $expected_result,
): void {
$this->subject->setErrors($errors);

static::assertSame(
$expected_result,
$this->subject->getErrorSummary(),
);
}

public static function errorDataProvider(): \Generator
{
// no errors, no message
yield [
[],
null,
];

// single error; JsonSchema 5.x
yield [
[
[
'property' => 'some-property',
'pointer' => 'some-pointer',
'message' => 'some-message',
'constraint' => 'some-constraint',
'context' => PHP_INT_MAX,
],
],
<<<ERROR
some-constraint - some-message
- some-property
ERROR,
];

// single error; JsonSchema 6
yield [
[
[
'property' => 'some-property',
'pointer' => 'some-pointer',
'message' => 'some-message',
'constraint' => ['name' => 'some-constraint', 'params' => []],
'context' => PHP_INT_MAX,
],
],
<<<ERROR
some-constraint - some-message
- some-property
ERROR,
];

// multiple errors
yield [
[
[
'property' => 'some-property2',
'pointer' => 'some-pointer2',
'message' => 'some-message',
'constraint' => ['name' => 'some-constraint', 'params' => []],
'context' => PHP_INT_MAX,
],
[
'property' => 'some-property',
'pointer' => 'some-pointer',
'message' => 'some-message',
'constraint' => ['name' => 'some-constraint', 'params' => []],
'context' => PHP_INT_MAX,
],
],
<<<ERROR
some-constraint - some-message
- some-property2
- some-property
ERROR,
];

// multiple errors having the same property
yield [
[
[
'property' => 'some-property[2]',
'pointer' => 'some-pointer2',
'message' => 'some-message',
'constraint' => ['name' => 'some-constraint', 'params' => []],
'context' => abs(PHP_INT_MAX),
],
[
'property' => 'some-property[2]',
'pointer' => 'some-pointer',
'message' => 'some-message',
'constraint' => ['name' => 'some-constraint', 'params' => []],
'context' => PHP_INT_MAX,
],
],
<<<ERROR
some-constraint - some-message
- some-property[*] (1 more)
ERROR,
];
}
}
2 changes: 1 addition & 1 deletion tests/specifications/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@
}
}
}
}
}