diff --git a/src/Definition/Loader/OpenApiDefinitionLoader.php b/src/Definition/Loader/OpenApiDefinitionLoader.php index 5a5f40d..fdc9843 100644 --- a/src/Definition/Loader/OpenApiDefinitionLoader.php +++ b/src/Definition/Loader/OpenApiDefinitionLoader.php @@ -133,6 +133,7 @@ private function getOperations(array $paths, array $securitySchemes, array $filt ->setTags($this->getTags($operation->tags)) ->setSecurities($this->getSecurities($securitySchemes, $requirements)) ->setExamples($this->getExamples($operation, $parameters)) + ->setExtensions($operation->getExtensions()) ); } } diff --git a/src/Definition/Operation.php b/src/Definition/Operation.php index b5f406b..94a503c 100644 --- a/src/Definition/Operation.php +++ b/src/Definition/Operation.php @@ -43,6 +43,11 @@ final class Operation implements Filterable private OperationExamples $examples; + /** + * @var array + */ + private array $extensions; + public function __construct( private readonly string $id, private readonly string $path, @@ -200,6 +205,14 @@ public function setResponses(Responses $responses): self return $this; } + /** + * @return array $extensions + */ + public function getExtensions(): array + { + return $this->extensions; + } + public function getResponse(int $status): ?Response { return $this->responses->get($status); @@ -277,6 +290,16 @@ public function setExamples(OperationExamples $examples): self return $this; } + /** + * @param array $extensions + */ + public function setExtensions(array $extensions): self + { + $this->extensions = $extensions; + + return $this; + } + /** * @param array $params * diff --git a/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php b/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php new file mode 100644 index 0000000..18e17d0 --- /dev/null +++ b/src/Preparator/Config/Error400BadFormatsPreparatorConfig.php @@ -0,0 +1,9 @@ + */ return $operations - ->map(fn (Operation $op) => $this->prepareTestCases($op)) + ->map(fn (Operation $operation) => $this->prepareTestCases($operation)) ->flatten() ; } @@ -118,6 +122,10 @@ protected function getStatusCode(): string */ private function prepareTestCases(Operation $operation): array { + if ($this->config->excludeOpenApiEndpoints + && isset($operation->getExtensions()['x-usecase'])) { + return []; + } $requiredParams = $operation->getParameters(true); return array_merge( diff --git a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php index 524573e..b762e1f 100644 --- a/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php +++ b/tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php @@ -21,11 +21,13 @@ final class Error400MissingRequiredFieldsPreparatorTest extends \PHPUnit\Framewo /** * @dataProvider getData * + * @param array> $config * @param TestCase[] $expected */ - public function test(Api $api, array $expected): void + public function test(Api $api, array $expected, array $config = []): void { $preparator = new Error400MissingRequiredFieldsPreparator(); + $preparator->configure($config); Assert::objectsEqual( $expected, $preparator->doPrepare($api->getOperations()), @@ -163,5 +165,48 @@ public function getData(): iterable ), ], ]; + + yield 'openapi endpoint is ignored' => [ + Api::create() + ->addOperation( + Operation::create( + 'test', + '/test', + 'POST' + ) + ->addRequestBody( + (new Body( + new Schema([ + 'type' => 'object', + 'properties' => [ + 'foo' => [ + 'type' => 'string', + ], + 'bar' => [ + 'type' => 'string', + ], + ], + 'required' => ['foo'], + ]), + 'application/json' + )) + ) + ->addExample( + OperationExample::create('foo') + ->setBodyContent([ + 'foo' => 'foo_body1', + 'bar' => 'bar_body1', + ]) + ->setQueryParameter('foo_query', 'foo1') + )->setExtensions([ + 'x-usecase' => 'UseCaseName', + ]) + ), + [ + ], + [ + 'excludeOpenApiEndpoints' => true, + ], + ]; } }