Skip to content

Commit

Permalink
Merge pull request #64 from OpenClassrooms/chore_examples_add_param_d…
Browse files Browse the repository at this point in the history
…eep_object_handling

chore(examples): Added DeepObject parameter handling to generate appropriate examples
  • Loading branch information
DotnDev authored Aug 13, 2024
2 parents be171f6 + 10482e8 commit 9102388
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Generated files
.idea/**/contentModel.xml
.idea/php-test-framework.xml

# Sensitive or high-churn files
.idea/**/dataSources/
Expand Down
15 changes: 0 additions & 15 deletions .idea/php-test-framework.xml

This file was deleted.

3 changes: 0 additions & 3 deletions config/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ parameters:
paths:
- ../src
- ../tests
excludePaths:
- ../src/Util/Collection.php
- ../src/Util/functions.php
ignoreErrors:
- '/Constructor in .+ has parameter .+ with default value./'
- '/Method .+ has parameter .+ with null as default value./'
Expand Down
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
10 changes: 8 additions & 2 deletions 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 @@ -401,7 +401,11 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
$parameter->schema instanceof Schema ? $parameter->schema->type : null,
);
}
if ($parameter->schema instanceof Schema && $parameter->schema->example !== null) {
if ($parameter->schema instanceof Schema &&
($parameter->schema->example !== null || $parameter->schema->type === 'object')
) {
$deepObject = $parameter->style === 'deepObject';

$operationExample = $this->getExample('default', $examples, $successStatusCode);
try {
$example = $this->extractDeepExamples($parameter->schema, path: 'parameter.' . $parameter->name);
Expand All @@ -414,8 +418,10 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
$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
{
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', (array) \scandir($dir), true)) {
if ($dir === \dirname($dir)) {
return $dir . '/' . trim($path, '/');
}
$dir = \dirname($dir);
}

return $dir . '/' . trim($path, '/');
}
}
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'
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

0 comments on commit 9102388

Please sign in to comment.