Repository: thephpleague/openapi-psr7-validator (php)

Issue:
Support Pattern Response Status Codes (2XX, 3XX, 4XX, and 5XX)
According to Open Api Specification 3.0.0 and above 
the Response Object alows to describe Error Responses in 
https://spec.openapis.org/oas/v3.0.0.html#patterned-fields-0

but the validation currently fails for schemas describing Status ranges.

        


Pull request:
Support Status Code Ranges 2XX, 3XX, 4XX, 5XX
fixes #158

Patch:
diff --git a/src/PSR7/SpecFinder.php b/src/PSR7/SpecFinder.php
index 8d58689..a3ad854 100644
--- a/src/PSR7/SpecFinder.php
+++ b/src/PSR7/SpecFinder.php
@@ -26,6 +26,7 @@ use Webmozart\Assert\Assert;
 use function json_decode;
 use function json_encode;
 use function property_exists;
+use function substr;
 
 final class SpecFinder
 {
@@ -211,6 +212,10 @@ final class SpecFinder
 
         $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');
         }


Test patch:
diff --git a/tests/PSR7/SpecFinderTest.php b/tests/PSR7/SpecFinderTest.php
index 98e2e36..2a5501b 100644
--- a/tests/PSR7/SpecFinderTest.php
+++ b/tests/PSR7/SpecFinderTest.php
@@ -6,6 +6,7 @@ namespace League\OpenAPIValidation\Tests\PSR7;
 
 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;
@@ -122,4 +123,61 @@ JSON;
         $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);
+    }
 }