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(example): auto generate example when missing in deep prop #59

Merged
merged 4 commits into from
Feb 19, 2024
Merged
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
39 changes: 14 additions & 25 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use cebe\openapi\spec\Schema;
use cebe\openapi\spec\SecurityRequirement;
use cebe\openapi\spec\SecurityScheme;
use Vural\OpenAPIFaker\Options;
use Vural\OpenAPIFaker\SchemaFaker\SchemaFaker;

final class OpenApiDefinitionLoader implements DefinitionLoader
{
Expand Down Expand Up @@ -383,13 +385,10 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
if ($mediaType->schema->example !== null) {
$operationExample = $this->getExample('default', $examples);
$operationExample->setBody(BodyExample::create((array) $mediaType->schema->example));
}
try {
} else {
$example = $this->extractDeepExamples($mediaType->schema);
$operationExample = $this->getExample('properties', $examples);
$operationExample->setBody(BodyExample::create($example));
} catch (ExampleNotExtractableException) {
// @ignoreException
}
}
}
Expand Down Expand Up @@ -424,15 +423,12 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
$operationExample->setResponse(
new ResponseExample((string) $statusCode, (array) $mediaType->schema->example)
);
}
try {
} else {
$example = $this->extractDeepExamples($mediaType->schema);
$operationExample = $this->getExample('properties', $examples);
$operationExample->setResponse(
new ResponseExample((string) $statusCode, $example)
);
} catch (ExampleNotExtractableException) {
// @ignoreException
}
}
}
Expand Down Expand Up @@ -475,8 +471,6 @@ private function getExample(string $name, array &$examples): OperationExample
}

/**
* @throws ExampleNotExtractableException
*
* @return array<mixed>
*/
private function extractDeepExamples(Schema $schema, bool $optional = false): array
Expand All @@ -496,21 +490,16 @@ private function extractDeepExamples(Schema $schema, bool $optional = false): ar
if ($return !== [] || $isRequired) {
$parent[$name] = $return;
}
} else {
if (
!$optional
&& !isset($property->default)
&& !$property->nullable
&& !isset($property->example)
&& \in_array($name, $schema->required ?? [], true)) {
throw new ExampleNotExtractableException();
}
if (isset($property->example)) {
$parent[$name] = $property->example;
} elseif (isset($property->default)) {
$parent[$name] = $property->default;
} elseif ($property->nullable) {
$parent[$name] = null;
} elseif (isset($property->example)) {
$parent[$name] = $property->example;
} elseif (isset($property->default)) {
$parent[$name] = $property->default;
} elseif ($property->nullable) {
$parent[$name] = null;
} elseif (!$optional && \in_array($name, $schema->required ?? [], true)) {
$fakeSchema = (new SchemaFaker($schema, new Options()))->generate();
if (is_array($fakeSchema) && isset($fakeSchema[$name])) {
$parent[$name] = $fakeSchema[$name];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Preparator/Error400BadTypesPreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ private function getSchemaExample(string $type)

private function isAllowedType(string $passedType, string $testedType): bool
{
return $passedType === $testedType || (self::NUMBER_TYPE === $passedType && self::INTEGER_TYPE === $testedType);
return $passedType === $testedType || ($passedType === self::NUMBER_TYPE && $testedType === self::INTEGER_TYPE);
}
}
Loading