Skip to content

chore(examples): Added DeepObject parameter handling to generate appropriate examples #64

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

Merged
merged 11 commits into from
Aug 13, 2024
Merged
19 changes: 16 additions & 3 deletions src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,22 @@ public function withParameter(string $name, string $value, string $in): self
return $clone;
}

public function setParameter(string $name, mixed $value, string $in, ?string $type = null): self
{
public function setParameter(
string $name,
mixed $value,
string $in,
?string $type = null,
bool $deepObject = false
): self {
$paramProp = $this->getParametersProp($in);

if ($deepObject && \is_array($value)) {
foreach ($value as $attribute => $attributeValue) {
$this->{$paramProp}[$name][$attribute] = (string) $attributeValue;
}
return $this;
}

if (\is_array($value)) {
$value = implode(',', $value);
}
Expand All @@ -94,7 +108,6 @@ public function setParameter(string $name, mixed $value, string $in, ?string $ty
if ($type === 'boolean') {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
}
$paramProp = $this->getParametersProp($in);
$this->{$paramProp}[$name] = (string) $value;

return $this;
Expand Down
23 changes: 22 additions & 1 deletion src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private function getSecurities(array $securitySchemes, array $requirements = [])
}
}
$notFoundRequirements = array_diff(
$requirements[$name],
$requirements[$name] ?? [],
array_unique(array_merge(...$supportedScopes))
);
if ($notFoundRequirements !== []) {
Expand Down Expand Up @@ -416,6 +416,27 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
$parameter->schema->type,
);
}

if ($parameter->schema instanceof Schema && $parameter->schema->type === 'object') {
$deepObject = $parameter->style === 'deepObject';
$operationExample = $this->getExample('default', $examples, $successStatusCode);

try {
$example = $this->extractDeepExamples($parameter->schema, path: 'parameter.' . $parameter->name);
} catch (InvalidExampleException $e) {
$this->logger->warning($e->getMessage());
continue;
}

$operationExample->setParameter(
$parameter->name,
$example,
$parameter->in,
$parameter->schema->type,
$deepObject
);
}

if ($operationExample === null || !$operationExample->hasParameter($parameter->name, $parameter->in)) {
if ($parameter->schema instanceof Schema && isset($parameter->schema->default)) {
$operationExample = $this->getExample('default', $examples);
Expand Down
2 changes: 1 addition & 1 deletion src/Preparator/Config/ResponseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getStatusCode(): ?string
return $this->statusCode;
}

public function setStatusCode(string|TaggedValue|null $statusCode): void
public function setStatusCode(string|TaggedValue|int|null $statusCode): void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the config only mentions a return code, it's treated as an int, not a TaggedValue

{
if ($statusCode instanceof TaggedValue) {
if ($statusCode->getTag() === 'NOT') {
Expand Down
14 changes: 14 additions & 0 deletions src/Util/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ public static function getBasePath(): string

return \dirname((string) $reflection->getFileName(), 3);
}

public static function getFullPath(string $path): string
{
$dir = \dirname(__DIR__);

while (!in_array('vendor', \scandir($dir), true)) {
if ($dir === \dirname($dir)) {
return $dir . '/' . trim($path, '/');
}
$dir = \dirname($dir);
}

return $dir . '/' . trim($path, '/');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Yaml::parseFile() was breaking as the path needs to be absolute

}
4 changes: 4 additions & 0 deletions src/Util/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static function parseFile(?string $path): array
return [];
}

if (!is_file($path)) {
$path = Path::getFullPath($path);
}

/** @var array<array-key, mixed> */
return \Symfony\Component\Yaml\Yaml::parseFile($path);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Config/api-tester.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ suites:
names: [ 'RangeConfig' ]
unit: items
- name: examples
extensionPath: tests/Fixtures/Examples/oc
extensionPath: 'tests/Fixtures/Examples/petstore/examples.new.yml'
Copy link
Contributor Author

@DotnDev DotnDev Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an existing file so the PlanTest.testOC() can work (the code related to the extension expects an 'operation' key and this file has one)

filters:
include:
- {id: oc_api_learning_activity_learning_path_projects_with_user_information_get}
Expand Down
23 changes: 23 additions & 0 deletions tests/Fixtures/OpenAPI/openclassrooms-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,29 @@ paths:
summary: 'Get funding-mechanisms'
description: 'Get funding-mechanisms'
operationId: oc_api_funding_mechanisms_get
parameters:
- name: op
in: query
description: 'Filter operator on ID'
required: false
style: deepObject
schema:
type: object
properties:
id:
type: string
enum: [in, nin]
example: in
- name: id
in: query
description: 'Filter by ID'
required: false
explode: false
schema:
type: array
items:
type: integer
example: 1
responses:
200:
description: Ok
Expand Down
Loading