-
Notifications
You must be signed in to change notification settings - Fork 2
/
WhitelistedSchema.php
112 lines (102 loc) · 5.15 KB
/
WhitelistedSchema.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Jane\Component\OpenApi3;
use Jane\Component\JsonSchemaRuntime\Reference;
use Jane\Component\OpenApi3\JsonSchema\Model\MediaType;
use Jane\Component\OpenApi3\JsonSchema\Model\Parameter;
use Jane\Component\OpenApi3\JsonSchema\Model\RequestBody;
use Jane\Component\OpenApi3\JsonSchema\Model\Response;
use Jane\Component\OpenApi3\JsonSchema\Model\Schema as SchemaModel;
use Jane\Component\OpenApiCommon\Contracts\WhitelistFetchInterface;
use Jane\Component\OpenApiCommon\Guesser\Guess\OperationGuess;
use Jane\Component\OpenApiCommon\Guesser\GuessClass;
use Jane\Component\OpenApiCommon\Naming\ChainOperationNaming;
use Jane\Component\OpenApiCommon\Naming\OperationIdNaming;
use Jane\Component\OpenApiCommon\Naming\OperationUrlNaming;
use Jane\Component\OpenApiCommon\Registry\Registry;
use Jane\Component\OpenApiCommon\Registry\Schema;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class WhitelistedSchema implements WhitelistFetchInterface
{
private $schema;
private $naming;
private $guessClass;
public function __construct(Schema $schema, DenormalizerInterface $denormalizer)
{
$this->schema = $schema;
$this->naming = new ChainOperationNaming([
new OperationIdNaming(),
new OperationUrlNaming(),
]);
$this->guessClass = new GuessClass(SchemaModel::class, $denormalizer);
}
public function addOperationRelations(OperationGuess $operationGuess, Registry $registry): void
{
$baseOperation = $this->naming->getEndpointName($operationGuess);
if ($this->schema->relationExists($baseOperation)) {
return;
}
/** @var RequestBody|null $requestBody */
$requestBody = $operationGuess->getOperation()->getRequestBody();
if (null !== $requestBody) {
if (null !== $requestBody->getContent() && is_iterable($requestBody->getContent())) {
/** @var MediaType $content */
foreach ($requestBody->getContent() as $contentType => $content) {
if (\in_array($contentType, ['application/json', 'application/x-www-form-urlencoded'], true)) {
$contentReference = $operationGuess->getReference() . '/content/' . $contentType . '/schema';
$schema = $content->getSchema();
$classGuess = $this->guessClass->guessClass($schema, $contentReference, $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
}
}
}
/** @var Response[]|null $responses */
$responses = $operationGuess->getOperation()->getResponses();
if (null !== $responses && \count($responses) > 0) {
foreach ($responses as $response) {
if ($response instanceof Reference) {
[$_, $response] = $this->guessClass->resolve($response, Response::class);
}
if (!($response instanceof Response)) {
continue;
}
if (null === $response->getContent()) {
$schema = null;
$classGuess = $this->guessClass->guessClass($schema, $operationGuess->getReference(), $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
if (null !== $response->getContent() && is_iterable($response->getContent())) {
/** @var MediaType $content */
foreach ($response->getContent() as $contentType => $content) {
if ('application/json' === $contentType) {
$contentReference = $operationGuess->getReference() . '/content/' . $contentType . '/schema';
$schema = $content->getSchema();
$classGuess = $this->guessClass->guessClass($schema, $contentReference, $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
}
}
}
}
/** @var Parameter[]|null $parameters */
$parameters = $operationGuess->getOperation()->getParameters();
if (null !== $parameters && \count($parameters) > 0) {
foreach ($parameters as $key => $parameter) {
if ($parameter instanceof Parameter && 'body' === $parameter->getIn()) {
$reference = $operationGuess->getReference() . '/parameters/' . $key;
$schema = $parameter->getSchema();
$classGuess = $this->guessClass->guessClass($schema, $reference, $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
}
}
}
}