-
Notifications
You must be signed in to change notification settings - Fork 937
/
SchemaQueryParameter.php
84 lines (70 loc) · 3.01 KB
/
SchemaQueryParameter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace SchemaQueryParameterProcessor;
use OpenApi\Analysis;
use OpenApi\Annotations\Operation;
use OpenApi\Generator;
use OpenApi\Attributes\Parameter;
use OpenApi\Attributes\Schema;
/**
* Custom processor to translate the vendor tag `query-args-$ref` into query parameter annotations.
*
* Details for the parameters are taken from the referenced schema.
*/
class SchemaQueryParameter
{
public const REF = 'query-args-$ref';
public function __invoke(Analysis $analysis): void
{
/** @var Operation[] $operations */
$operations = $analysis->getAnnotationsOfType(Operation::class);
foreach ($operations as $operation) {
if ($operation->x !== Generator::UNDEFINED && array_key_exists(self::REF, $operation->x)) {
if (!is_string($operation->x[self::REF])) {
throw new \InvalidArgumentException('Value of `x.' . self::REF . '` must be a string');
}
$schema = $analysis->getSchemaForSource($operation->x[self::REF]);
if (!$schema instanceof Schema) {
throw new \InvalidArgumentException('Value of `x.' . self::REF . "` contains reference to unknown schema: `{$operation->x[self::REF]}`");
}
$this->expandQueryArgs($operation, $schema);
$this->cleanUp($operation);
}
}
}
/**
* Expand the given operation by injecting parameters for all properties of the given schema.
*/
private function expandQueryArgs(Operation $operation, Schema $schema): void
{
if (Generator::isDefault($schema->properties) || !$schema->properties) {
return;
}
$operation->parameters = Generator::isDefault($operation->parameters) ? [] : $operation->parameters;
foreach ($schema->properties as $property) {
$isNullable = Generator::isDefault($property->nullable) ? false : $property->nullable;
$schema = new Schema(
type: Generator::isDefault($property->format) ? $property->type : $property->format,
nullable: $isNullable
);
$schema->_context = $operation->_context; // inherit context from operation, required to pretend to be a parameter
$parameter = new Parameter(
name: $property->property,
description: Generator::isDefault($property->description) ? null : $property->description,
in: 'query',
required: !$isNullable,
schema: $schema,
example: $property->example,
);
$parameter->_context = $operation->_context; // inherit context from operation, required to pretend to be a parameter
$operation->parameters[] = $parameter;
}
}
private function cleanUp(Operation $operation): void
{
unset($operation->x[self::REF]);
if (!$operation->x) {
$operation->x = Generator::UNDEFINED;
}
}
}