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

Adding Tunable SuccessStatusCode either 200 or 201 for Example Prepator #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ final class OperationExample

public function __construct(
private string $name,
Operation $parent = null
Operation $parent = null,
int $statusCode = null
) {
if ($parent !== null) {
$this->parent = $parent;
}
$this->response = new ResponseExample();
$this->response = new ResponseExample($statusCode !== null ? "{$statusCode}" : null);
$this->deepCopy = new DeepCopy();
$this->deepCopy->addTypeFilter(
new ShallowCopyFilter(),
Expand Down
12 changes: 9 additions & 3 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
{
$examples = [];

$successStatusCodes = array_filter(
array_keys($operation->responses?->getResponses() ?? []),
static fn ($status) => in_array($status, [200, 201], true)
);
$successStatusCode = count($successStatusCodes) > 0 ? array_shift($successStatusCodes) : null;

foreach ($parameters as $parameter) {
foreach ($parameter->examples ?? [] as $name => $example) {
$operationExample = $this->getExample((string) $name, $examples);
Expand All @@ -363,7 +369,7 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
if (\is_array($example)) {
$example = implode(',', $example);
}
$operationExample = $this->getExample('properties', $examples);
$operationExample = $this->getExample('properties', $examples, $successStatusCode);
$operationExample->setParameter($parameter->name, (string) $example, $parameter->in);
}
}
Expand Down Expand Up @@ -465,10 +471,10 @@ private function getHeaders(array $headers): Parameters
/**
* @param array<OperationExample> $examples
*/
private function getExample(string $name, array &$examples): OperationExample
private function getExample(string $name, array &$examples, int $statusCode = null): OperationExample
{
if (!isset($examples[$name])) {
$examples[$name] = new OperationExample($name);
$examples[$name] = new OperationExample($name, null, $statusCode);
}

return $examples[$name];
Expand Down
Loading