diff --git a/src/Definition/Example/OperationExample.php b/src/Definition/Example/OperationExample.php index 18d5930..1c92d80 100644 --- a/src/Definition/Example/OperationExample.php +++ b/src/Definition/Example/OperationExample.php @@ -51,7 +51,7 @@ final class OperationExample private DeepCopy $deepCopy; public function __construct( - private string $name, + private string|int $name, Operation $parent = null, ?int $statusCode = null, ) { @@ -153,18 +153,27 @@ public function withBody(BodyExample $body): self return $clone; } - public function getName(): string + public function getName(): string|int { return $this->name; } - public function setName(string $name): self + public function setName(string|int $name): self { $this->name = $name; return $this; } + public function withName(string|int $name): self + { + /** @var OperationExample $clone */ + $clone = $this->deepCopy->copy($this); + $clone->name = $name; + + return $clone; + } + /** * @return array */ @@ -324,7 +333,10 @@ public function getBody(): ?BodyExample if ($this->autoComplete) { $randomBody = BodyExample::create($requestBody->getRandomContent()); if ($this->body !== null) { - $this->body->setContent(array_merge($randomBody->getContent(), $this->body->getContent())); + $content = $this->body->getContent(); + if (!isset($content[0])) { + $this->body->setContent(array_merge($randomBody->getContent(), $content)); + } } else { $this->body = $randomBody; } diff --git a/src/Definition/Loader/OpenApiDefinitionLoader.php b/src/Definition/Loader/OpenApiDefinitionLoader.php index bba1da5..9e0ca29 100644 --- a/src/Definition/Loader/OpenApiDefinitionLoader.php +++ b/src/Definition/Loader/OpenApiDefinitionLoader.php @@ -549,7 +549,7 @@ private function getHeaders(array $headers): Parameters /** * @param array $examples */ - private function getExample(string $name, array &$examples, ?int $statusCode = null): OperationExample + private function getExample(string|int $name, array &$examples, ?int $statusCode = null): OperationExample { if (!isset($examples[$name])) { $examples[$name] = new OperationExample($name, null, $statusCode); diff --git a/src/Definition/Operation.php b/src/Definition/Operation.php index b5f406b..8c9de33 100644 --- a/src/Definition/Operation.php +++ b/src/Definition/Operation.php @@ -419,12 +419,12 @@ public function setParametersIn(Parameters $parameters, string $in): self return $this; } - public function getExample(?string $name = null): OperationExample + public function getExample(?string $name = null, mixed $default = null): OperationExample { $examples = $this->getExamples(); if ($name !== null) { return $examples - ->get($name) + ->get($name, $default) ; } diff --git a/src/Preparator/Error404Preparator.php b/src/Preparator/Error404Preparator.php index 08302be..99c66cb 100644 --- a/src/Preparator/Error404Preparator.php +++ b/src/Preparator/Error404Preparator.php @@ -40,23 +40,18 @@ private function prepareTestCase(DefinitionResponse $response): array $testcases = []; - if ($operation->getRequestBodies()->count() === 0) { - $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() - ->setResponse( - ResponseExample::create() - ->setStatusCode($this->config->response->getStatusCode() ?? '404') - ->setHeaders($this->config->response->headers ?? []) - ->setContent($this->config->response->body ?? $response->getDescription()) - ) + foreach ( + range( + 1, + $operation->getRequestBodies()->count() ?: 1 + ) as $ignored + ) { + $notFoundExample = $operation->getExample( + '404', + OperationExample::create('RandomPath', $operation)->setForceRandom() ); - } - - foreach ($operation->getRequestBodies() as $ignored) { $testcases[] = $this->buildTestCase( - OperationExample::create('RandomPath', $operation) - ->setForceRandom() + $notFoundExample ->setResponse( ResponseExample::create() ->setStatusCode($this->config->response->getStatusCode() ?? '404') diff --git a/src/Preparator/ExamplesPreparator.php b/src/Preparator/ExamplesPreparator.php index 1cc5bbc..e9810fd 100644 --- a/src/Preparator/ExamplesPreparator.php +++ b/src/Preparator/ExamplesPreparator.php @@ -57,7 +57,10 @@ private function handleExtension(Operations $operations): Operations */ private function prepareTestCases(Operation $operation): iterable { - $examples = $operation->getExamples(); + $examples = $operation + ->getExamples() + ->filter(fn (OperationExample $example) => $example->getName() !== '404') + ; if ($this->config->autoCreateWhenMissing && $examples->count() === 0) { $examples = new OperationExamples([ $operation->getRandomExample(),