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

feat(examples-prepartor): exclude array bodies from auto complete #63

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
20 changes: 16 additions & 4 deletions src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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<string, int|string>
*/
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private function getHeaders(array $headers): Parameters
/**
* @param array<OperationExample> $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);
Expand Down
4 changes: 2 additions & 2 deletions src/Definition/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
}

Expand Down
25 changes: 10 additions & 15 deletions src/Preparator/Error404Preparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,18 @@

$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

Check failure on line 46 in src/Preparator/Error404Preparator.php

View workflow job for this annotation

GitHub Actions / PHPStan

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.
) 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')
Expand Down
5 changes: 4 additions & 1 deletion src/Preparator/ExamplesPreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading