Skip to content

Commit ea60f14

Browse files
authored
Resolve class names to refs in Discriminator::mapping (#1736)
1 parent c2b6571 commit ea60f14

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

docs/examples/specs/polymorphism/attributes/AbstractResponsible.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
discriminator: new OAT\Discriminator(
1818
propertyName: 'type',
1919
mapping: [
20-
'fl' => '#/components/schemas/FlResponsible',
21-
'employee' => '#/components/schemas/EmployeeResponsible',
20+
'fl' => Fl::class,
21+
'employee' => Employee::class,
2222
]
2323
)
2424
)]

src/Attributes/Schema.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace OpenApi\Attributes;
88

9-
use OpenApi\Annotations\Examples;
109
use OpenApi\Generator;
1110
use OpenApi\Annotations as OA;
1211

src/Generator.php

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public function getProcessorPipeline(): Pipeline
215215
new Processors\AugmentSchemas(),
216216
new Processors\AugmentRequestBody(),
217217
new Processors\AugmentProperties(),
218+
new Processors\AugmentDiscriminators(),
218219
new Processors\BuildPaths(),
219220
new Processors\AugmentParameters(),
220221
new Processors\AugmentRefs(),
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Processors;
8+
9+
use OpenApi\Analysis;
10+
use OpenApi\Annotations as OA;
11+
use OpenApi\Generator;
12+
13+
/**
14+
* Use the property context to extract useful information and inject that into the annotation.
15+
*/
16+
class AugmentDiscriminators
17+
{
18+
public function __invoke(Analysis $analysis)
19+
{
20+
/** @var OA\Discriminator[] $discriminators */
21+
$discriminators = $analysis->getAnnotationsOfType(OA\Discriminator::class);
22+
23+
foreach ($discriminators as $discriminator) {
24+
if (!Generator::isDefault($discriminator->mapping)) {
25+
foreach ($discriminator->mapping as $value => $type) {
26+
if (is_string($type) && $typeSchema = $analysis->getSchemaForSource($type)) {
27+
$discriminator->mapping[$value] = OA\Components::ref($typeSchema);
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}

src/Processors/AugmentProperties.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use OpenApi\Analysis;
1010
use OpenApi\Annotations as OA;
11-
use OpenApi\Annotations\Components;
1211
use OpenApi\Context;
1312
use OpenApi\Generator;
1413

@@ -82,15 +81,15 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex
8281
if (!$this->mapNativeType($property, $type)) {
8382
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
8483
if (Generator::isDefault($property->ref) && $schema) {
85-
$property->ref = Components::ref($schema);
84+
$property->ref = OA\Components::ref($schema);
8685
}
8786
}
8887

8988
// ok, so we possibly have a type or ref
9089
if (!Generator::isDefault($property->ref) && $typeMatches[2] === '' && !Generator::isDefault($property->nullable) && $property->nullable) {
9190
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
9291
if ($schema) {
93-
$property->ref = Components::ref($schema);
92+
$property->ref = OA\Components::ref($schema);
9493
}
9594
} elseif ($typeMatches[2] === '[]') {
9695
if (Generator::isDefault($property->items)) {
@@ -147,11 +146,11 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex
147146
if (!$this->mapNativeType($property, $type)) {
148147
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
149148
if (Generator::isDefault($property->ref) && $schema) {
150-
$this->applyRef($analysis, $property, Components::ref($schema));
149+
$this->applyRef($analysis, $property, OA\Components::ref($schema));
151150
} else {
152151
if (is_string($context->type) && $typeSchema = $analysis->getSchemaForSource($context->type)) {
153152
if (Generator::isDefault($property->format)) {
154-
$property->ref = Components::ref($typeSchema);
153+
$property->ref = OA\Components::ref($typeSchema);
155154
$property->type = Generator::UNDEFINED;
156155
}
157156
}

0 commit comments

Comments
 (0)