Skip to content

Commit 9102388

Browse files
authored
Merge pull request #64 from OpenClassrooms/chore_examples_add_param_deep_object_handling
chore(examples): Added DeepObject parameter handling to generate appropriate examples
2 parents be171f6 + 10482e8 commit 9102388

File tree

10 files changed

+68
-25
lines changed

10 files changed

+68
-25
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Generated files
1313
.idea/**/contentModel.xml
14+
.idea/php-test-framework.xml
1415

1516
# Sensitive or high-churn files
1617
.idea/**/dataSources/

.idea/php-test-framework.xml

-15
This file was deleted.

config/phpstan.neon

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ parameters:
1313
paths:
1414
- ../src
1515
- ../tests
16-
excludePaths:
17-
- ../src/Util/Collection.php
18-
- ../src/Util/functions.php
1916
ignoreErrors:
2017
- '/Constructor in .+ has parameter .+ with default value./'
2118
- '/Method .+ has parameter .+ with null as default value./'

src/Definition/Example/OperationExample.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,22 @@ public function withParameter(string $name, string $value, string $in): self
8383
return $clone;
8484
}
8585

86-
public function setParameter(string $name, mixed $value, string $in, ?string $type = null): self
87-
{
86+
public function setParameter(
87+
string $name,
88+
mixed $value,
89+
string $in,
90+
?string $type = null,
91+
bool $deepObject = false
92+
): self {
93+
$paramProp = $this->getParametersProp($in);
94+
95+
if ($deepObject && \is_array($value)) {
96+
foreach ($value as $attribute => $attributeValue) {
97+
$this->{$paramProp}[$name][$attribute] = (string) $attributeValue;
98+
}
99+
return $this;
100+
}
101+
88102
if (\is_array($value)) {
89103
$value = implode(',', $value);
90104
}
@@ -94,7 +108,6 @@ public function setParameter(string $name, mixed $value, string $in, ?string $ty
94108
if ($type === 'boolean') {
95109
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
96110
}
97-
$paramProp = $this->getParametersProp($in);
98111
$this->{$paramProp}[$name] = (string) $value;
99112

100113
return $this;

src/Definition/Loader/OpenApiDefinitionLoader.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private function getSecurities(array $securitySchemes, array $requirements = [])
350350
}
351351
}
352352
$notFoundRequirements = array_diff(
353-
$requirements[$name],
353+
$requirements[$name] ?? [],
354354
array_unique(array_merge(...$supportedScopes))
355355
);
356356
if ($notFoundRequirements !== []) {
@@ -401,7 +401,11 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
401401
$parameter->schema instanceof Schema ? $parameter->schema->type : null,
402402
);
403403
}
404-
if ($parameter->schema instanceof Schema && $parameter->schema->example !== null) {
404+
if ($parameter->schema instanceof Schema &&
405+
($parameter->schema->example !== null || $parameter->schema->type === 'object')
406+
) {
407+
$deepObject = $parameter->style === 'deepObject';
408+
405409
$operationExample = $this->getExample('default', $examples, $successStatusCode);
406410
try {
407411
$example = $this->extractDeepExamples($parameter->schema, path: 'parameter.' . $parameter->name);
@@ -414,8 +418,10 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
414418
$example,
415419
$parameter->in,
416420
$parameter->schema->type,
421+
$deepObject
417422
);
418423
}
424+
419425
if ($operationExample === null || !$operationExample->hasParameter($parameter->name, $parameter->in)) {
420426
if ($parameter->schema instanceof Schema && isset($parameter->schema->default)) {
421427
$operationExample = $this->getExample('default', $examples);

src/Preparator/Config/ResponseConfig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getStatusCode(): ?string
2222
return $this->statusCode;
2323
}
2424

25-
public function setStatusCode(string|TaggedValue|null $statusCode): void
25+
public function setStatusCode(string|TaggedValue|int|null $statusCode): void
2626
{
2727
if ($statusCode instanceof TaggedValue) {
2828
if ($statusCode->getTag() === 'NOT') {

src/Util/Path.php

+14
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ public static function getBasePath(): string
1414

1515
return \dirname((string) $reflection->getFileName(), 3);
1616
}
17+
18+
public static function getFullPath(string $path): string
19+
{
20+
$dir = \dirname(__DIR__);
21+
22+
while (!in_array('vendor', (array) \scandir($dir), true)) {
23+
if ($dir === \dirname($dir)) {
24+
return $dir . '/' . trim($path, '/');
25+
}
26+
$dir = \dirname($dir);
27+
}
28+
29+
return $dir . '/' . trim($path, '/');
30+
}
1731
}

src/Util/Yaml.php

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public static function parseFile(?string $path): array
1717
return [];
1818
}
1919

20+
if (!is_file($path)) {
21+
$path = Path::getFullPath($path);
22+
}
23+
2024
/** @var array<array-key, mixed> */
2125
return \Symfony\Component\Yaml\Yaml::parseFile($path);
2226
}

tests/Fixtures/Config/api-tester.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ suites:
4646
names: [ 'RangeConfig' ]
4747
unit: items
4848
- name: examples
49-
extensionPath: tests/Fixtures/Examples/oc
49+
extensionPath: 'tests/Fixtures/Examples/petstore/examples.new.yml'
5050
filters:
5151
include:
5252
- {id: oc_api_learning_activity_learning_path_projects_with_user_information_get}

tests/Fixtures/OpenAPI/openclassrooms-api.yml

+23
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,29 @@ paths:
586586
summary: 'Get funding-mechanisms'
587587
description: 'Get funding-mechanisms'
588588
operationId: oc_api_funding_mechanisms_get
589+
parameters:
590+
- name: op
591+
in: query
592+
description: 'Filter operator on ID'
593+
required: false
594+
style: deepObject
595+
schema:
596+
type: object
597+
properties:
598+
id:
599+
type: string
600+
enum: [in, nin]
601+
example: in
602+
- name: id
603+
in: query
604+
description: 'Filter by ID'
605+
required: false
606+
explode: false
607+
schema:
608+
type: array
609+
items:
610+
type: integer
611+
example: 1
589612
responses:
590613
200:
591614
description: Ok

0 commit comments

Comments
 (0)