-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJaneOpenApi.php
More file actions
169 lines (136 loc) · 6.47 KB
/
JaneOpenApi.php
File metadata and controls
169 lines (136 loc) · 6.47 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Jane\Component\OpenApiCommon;
use Jane\Component\JsonSchema\Generator\ChainGenerator;
use Jane\Component\JsonSchema\Generator\Context\Context;
use Jane\Component\JsonSchema\Generator\Naming;
use Jane\Component\JsonSchema\Guesser\ChainGuesser;
use Jane\Component\JsonSchema\Guesser\Validator\ChainValidatorFactory;
use Jane\Component\JsonSchema\Registry\Registry;
use Jane\Component\OpenApiCommon\Contracts\WhitelistFetchInterface;
use Jane\Component\OpenApiCommon\Guesser\Guess\ClassGuess;
use Jane\Component\OpenApiCommon\Guesser\Guess\ParentClass;
use Jane\Component\OpenApiCommon\Registry\Registry as OpenApiRegistry;
use Jane\Component\OpenApiCommon\Registry\Schema;
use Jane\Component\OpenApiCommon\SchemaParser\SchemaParser;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
abstract class JaneOpenApi extends ChainGenerator
{
protected const OBJECT_NORMALIZER_CLASS = null;
protected const WHITELIST_FETCH_CLASS = null;
protected SchemaParser $schemaParser;
protected Naming $naming;
protected NormalizerInterface|DenormalizerInterface $serializer;
/**
* @param class-string $schemaParserClass
*/
public function __construct(
string $schemaParserClass,
protected ChainGuesser $chainGuesser,
protected bool $strict = true,
) {
$this->serializer = static::buildSerializer();
$this->schemaParser = new $schemaParserClass($this->serializer);
$this->naming = new Naming();
}
public function getSerializer(): NormalizerInterface|DenormalizerInterface
{
return $this->serializer;
}
/**
* @param OpenApiRegistry $registry
*/
public function createContext(Registry $registry): Context
{
/** @var Schema[] $schemas */
$schemas = array_values($registry->getSchemas());
foreach ($schemas as $schema) {
$openApiSpec = $this->schemaParser->parseSchema($schema->getOrigin());
$this->chainGuesser->guessClass($openApiSpec, $schema->getRootName(), $schema->getOrigin() . '#', $registry);
$schema->setParsed($openApiSpec);
}
$chainValidator = ChainValidatorFactory::create($this->naming, $registry, $this->serializer);
foreach ($schemas as $schema) {
foreach ($schema->getClasses() as $class) {
$properties = $this->chainGuesser->guessProperties($class->getObject(), $schema->getRootName(), $class->getReference(), $registry);
$names = [];
foreach ($properties as $property) {
$deduplicatedName = $this->naming->getDeduplicatedName($property->getName(), $names);
$property->setAccessorName($deduplicatedName);
$property->setPhpName($this->naming->getPropertyName($deduplicatedName));
$property->setType($this->chainGuesser->guessType($property->getObject(), $property->getName(), $property->getReference(), $registry));
}
$class->setProperties($properties);
$schema->addClassRelations($class);
$extensionsTypes = [];
foreach ($class->getExtensionsObject() as $pattern => $extensionData) {
$extensionsTypes[$pattern] = $this->chainGuesser->guessType($extensionData['object'], $class->getName(), $extensionData['reference'], $registry);
}
$class->setExtensionsType($extensionsTypes);
$chainValidator->guess($class->getObject(), $class->getName(), $class);
}
$this->hydrateDiscriminatedClasses($schema, $registry);
// when we have a whitelist, we want to have only needed models to be generated
if (\count($registry->getWhitelistedPaths() ?? []) > 0) {
$this->whitelistFetch($schema, $registry);
}
}
return new Context($registry, $this->strict);
}
/**
* @param OpenApiRegistry $registry
*/
protected function whitelistFetch(Schema $schema, Registry $registry): void
{
$whitelistFetchClass = static::WHITELIST_FETCH_CLASS;
/** @var WhitelistFetchInterface $whitelistedSchema */
$whitelistedSchema = new $whitelistFetchClass($schema, static::buildSerializer());
foreach ($schema->getOperations() as $operation) {
$whitelistedSchema->addOperationRelations($operation, $registry);
}
$schema->filterRelations();
}
protected function hydrateDiscriminatedClasses(Schema $schema, Registry $registry)
{
foreach ($schema->getClasses() as $class) {
if ($class instanceof ParentClass) { // is parent class
foreach ($class->getChildReferences() as $reference) {
$guess = $registry->getClass($reference);
if ($guess instanceof ClassGuess) { // is child class
$guess->setParentClass($class);
}
}
}
}
}
public static function buildSerializer(): SerializerInterface|DenormalizerInterface|NormalizerInterface
{
$encoders = [
new JsonEncoder(new JsonEncode([JsonEncode::OPTIONS => \JSON_UNESCAPED_SLASHES]), new JsonDecode()),
new YamlEncoder(new Dumper(), new Parser()),
];
$objectNormalizerClass = static::OBJECT_NORMALIZER_CLASS;
return new Serializer([new $objectNormalizerClass()], $encoders);
}
abstract protected static function create(array $options = []): self;
abstract protected static function generators(DenormalizerInterface $denormalizer, array $options = []): \Generator;
public static function build(array $options = [])
{
$instance = static::create($options);
/** @var DenormalizerInterface $denormalizer */
$denormalizer = $instance->getSerializer();
$generators = static::generators($denormalizer, $options);
foreach ($generators as $generator) {
$instance->addGenerator($generator);
}
return $instance;
}
}