-
Notifications
You must be signed in to change notification settings - Fork 2
/
WhitelistedSchema.php
78 lines (69 loc) · 3.27 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
<?php
namespace Jane\Component\OpenApi2;
use Jane\Component\JsonSchemaRuntime\Reference;
use Jane\Component\OpenApi2\JsonSchema\Model\BodyParameter;
use Jane\Component\OpenApi2\JsonSchema\Model\Operation;
use Jane\Component\OpenApi2\JsonSchema\Model\Response;
use Jane\Component\OpenApi2\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 Operation $operation */
$operation = $operationGuess->getOperation();
if (null !== $operation->getResponses() && \count($operation->getResponses()) > 0) {
foreach ($operation->getResponses() as $status => $response) {
$reference = $operationGuess->getReference() . '/responses/' . $status;
if ($response instanceof Reference) {
[$reference, $response] = $this->guessClass->resolve($response, Response::class);
}
/** @var Response $response */
if (null === $response->getSchema()) {
continue;
}
$schema = $response->getSchema();
$classGuess = $this->guessClass->guessClass($schema, $reference, $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
}
if (null !== $operation->getParameters() && \count($operation->getParameters()) > 0) {
foreach ($operation->getParameters() as $key => $parameter) {
if ($parameter instanceof BodyParameter && null !== $parameter->getSchema()) {
$reference = $operationGuess->getReference() . '/parameters/' . $key;
$schema = $parameter->getSchema();
$classGuess = $this->guessClass->guessClass($schema, $reference, $registry);
if (null !== $classGuess) {
$this->schema->addOperationRelation($baseOperation, $classGuess->getName());
}
}
}
}
}
}