Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Status Code Ranges 2XX, 3XX, 4XX, 5XX #159

Merged
merged 5 commits into from
Mar 1, 2022
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
5 changes: 5 additions & 0 deletions src/PSR7/SpecFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function json_decode;
use function json_encode;
use function property_exists;
use function substr;

final class SpecFinder
{
Expand Down Expand Up @@ -211,6 +212,10 @@ public function findResponseSpec($addr): ResponseSpec

$response = $operation->responses->getResponse((string) $addr->responseCode());

if (! $response) {
$response = $operation->responses->getResponse(substr((string) $addr->responseCode(), 0, 1) . 'XX');
}

if (! $response) {
$response = $operation->responses->getResponse('default');
}
Expand Down
58 changes: 58 additions & 0 deletions tests/PSR7/SpecFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use League\OpenAPIValidation\PSR7\CallbackAddress;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ResponseAddress;
use League\OpenAPIValidation\PSR7\SpecFinder;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -122,4 +123,61 @@ public function testHandleParameters(): void
$pathItem = $specFinder->findPathSpec(new OperationAddress('/api/1.0/order/123', 'get'));
self::assertSame('The order object', $pathItem->get->responses[200]->description);
}

public function testResponseStatusCodesWithWildcards(): void
{
$yaml = <<<YAML
openapi: 3.0.0
info:
title: Product import API
version: '1.0'
servers:
- url: 'http://localhost:8000/api/v1'
paths:
/products.find:
get:
responses:
'404':
description: Not Found
content:
application/json:
schema:
properties:
message:
type: string
'4XX':
description: Client Error
content:
application/json:
schema:
properties:
message:
type: string
'default':
description: Unexpected Error
content:
application/json:
schema:
properties:
message:
type: string
YAML;

$schema = (new ValidatorBuilder())->fromYaml($yaml)->getServerRequestValidator()->getSchema();
$specFinder = new SpecFinder($schema);
$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 404)
);
self::assertSame('Not Found', $responseSpec->description);

$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 400)
);
self::assertSame('Client Error', $responseSpec->description);

$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 500)
);
self::assertSame('Unexpected Error', $responseSpec->description);
}
}