|
5 | 5 | namespace League\OpenAPIValidation\PSR7;
|
6 | 6 |
|
7 | 7 | use cebe\openapi\spec\OpenApi;
|
8 |
| -use League\OpenAPIValidation\PSR7\Exception\InvalidServerRequestMessage; |
9 | 8 | use League\OpenAPIValidation\PSR7\Exception\MultipleOperationsMismatchForRequest;
|
10 | 9 | use League\OpenAPIValidation\PSR7\Exception\NoOperation;
|
11 | 10 | use League\OpenAPIValidation\PSR7\Exception\ValidationFailed;
|
@@ -54,44 +53,40 @@ public function getSchema() : OpenApi
|
54 | 53 | */
|
55 | 54 | public function validate(ServerRequestInterface $serverRequest) : OperationAddress
|
56 | 55 | {
|
57 |
| - try { |
58 |
| - $path = $serverRequest->getUri()->getPath(); |
59 |
| - $method = strtolower($serverRequest->getMethod()); |
60 |
| - |
61 |
| - // 0. Find matching operations |
62 |
| - // If there is only one - then proceed with checking |
63 |
| - // If there are multiple candidates, then check each one, if all fail - we don't know which one supposed to be the one, so we need to throw an exception like |
64 |
| - // "This request matched operations A,B and C, but mismatched its schemas." |
65 |
| - $matchingOperationsAddrs = $this->findMatchingOperations($serverRequest); |
66 |
| - |
67 |
| - if (! $matchingOperationsAddrs) { |
68 |
| - throw NoOperation::fromPathAndMethod($path, $method); |
69 |
| - } |
| 56 | + $path = $serverRequest->getUri()->getPath(); |
| 57 | + $method = strtolower($serverRequest->getMethod()); |
70 | 58 |
|
71 |
| - // Single match is the most desirable variant, because we reduce ambiguity down to zero |
72 |
| - if (count($matchingOperationsAddrs) === 1) { |
73 |
| - $this->validator->validate($matchingOperationsAddrs[0], $serverRequest); |
| 59 | + // 0. Find matching operations |
| 60 | + // If there is only one - then proceed with checking |
| 61 | + // If there are multiple candidates, then check each one, if all fail - we don't know which one supposed to be the one, so we need to throw an exception like |
| 62 | + // "This request matched operations A,B and C, but mismatched its schemas." |
| 63 | + $matchingOperationsAddrs = $this->findMatchingOperations($serverRequest); |
74 | 64 |
|
75 |
| - return $matchingOperationsAddrs[0]; |
76 |
| - } |
| 65 | + if (! $matchingOperationsAddrs) { |
| 66 | + throw NoOperation::fromPathAndMethod($path, $method); |
| 67 | + } |
77 | 68 |
|
78 |
| - // there are multiple matching operations, this is bad, because if none of them match the message |
79 |
| - // then we cannot say reliably which one intended to match |
80 |
| - foreach ($matchingOperationsAddrs as $matchedAddr) { |
81 |
| - try { |
82 |
| - $this->validator->validate($matchedAddr, $serverRequest); |
| 69 | + // Single match is the most desirable variant, because we reduce ambiguity down to zero |
| 70 | + if (count($matchingOperationsAddrs) === 1) { |
| 71 | + $this->validator->validate($matchingOperationsAddrs[0], $serverRequest); |
83 | 72 |
|
84 |
| - return $matchedAddr; // Good, operation matched and request is valid against it, stop here |
85 |
| - } catch (Throwable $e) { |
86 |
| - // that operation did not match |
87 |
| - } |
88 |
| - } |
| 73 | + return $matchingOperationsAddrs[0]; |
| 74 | + } |
| 75 | + |
| 76 | + // there are multiple matching operations, this is bad, because if none of them match the message |
| 77 | + // then we cannot say reliably which one intended to match |
| 78 | + foreach ($matchingOperationsAddrs as $matchedAddr) { |
| 79 | + try { |
| 80 | + $this->validator->validate($matchedAddr, $serverRequest); |
89 | 81 |
|
90 |
| - // no operation matched at all... |
91 |
| - throw MultipleOperationsMismatchForRequest::fromMatchedAddrs($matchingOperationsAddrs); |
92 |
| - } catch (ValidationFailed $e) { |
93 |
| - throw InvalidServerRequestMessage::because($e); |
| 82 | + return $matchedAddr; // Good, operation matched and request is valid against it, stop here |
| 83 | + } catch (Throwable $e) { |
| 84 | + // that operation did not match |
| 85 | + } |
94 | 86 | }
|
| 87 | + |
| 88 | + // no operation matched at all... |
| 89 | + throw MultipleOperationsMismatchForRequest::fromMatchedAddrs($matchingOperationsAddrs); |
95 | 90 | }
|
96 | 91 |
|
97 | 92 | /**
|
|
0 commit comments