-
Notifications
You must be signed in to change notification settings - Fork 451
/
SpecMerger.php
68 lines (58 loc) · 2.34 KB
/
SpecMerger.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
<?php
namespace Concrete\Core\Api\OpenApi;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Schema;
use OpenApi\Serializer;
class SpecMerger
{
/**
* @var Serializer
*/
protected $serializer;
public function __construct(Serializer $serializer)
{
$this->serializer = $serializer;
}
public function merge(SpecFragment $object, OpenApi $openApi): OpenApi
{
$mergeables = [];
$pathCollection = $object->getPaths();
if ($pathCollection) {
$mergeables = $pathCollection->getPathsAsPathItems();
}
$openApi->merge($mergeables);
// Merge custom models from Express objects:
// AE: I'm not sure why it has to work this way. I couldn't get the merge flow above to work with Schema
// Or with Component objects so I had to do something like this, which is uglier but does work to get the
// Express models included with everything else.
$components = $object->getComponents();
if ($components) {
$componentsObject = $this->serializer->deserialize(json_encode($components), 'OpenApi\Annotations\Components');
}
foreach ($componentsObject->schemas as $schema) {
$openApi->components->schemas[] = $schema;
}
foreach ($componentsObject->requestBodies as $requestBody) {
$openApi->components->requestBodies[] = $requestBody;
}
// End custom Express merge.
// Security schemes merge
// This is obnoxious but it's actually the easiest way to dynamically merge
foreach ($object->getSecuritySchemes() as $securityScheme) {
foreach ($openApi->components->securitySchemes as $openApiSecurityScheme) {
if ($openApiSecurityScheme->securityScheme === $securityScheme->getName()) {
foreach ($securityScheme->getScopes() as $scope => $description) {
$openApiSecurityScheme->flows[0]->scopes[$scope] = $description;
}
}
}
}
return $openApi;
}
public function mergeProperty(SpecProperty $specProperty, Schema $schema)
{
$property = $this->serializer->deserialize(json_encode($specProperty), 'OpenApi\Annotations\Property');
$schema->properties[] = $property;
return $schema;
}
}