Skip to content

Commit fbefe5a

Browse files
authored
Complete polymorphism example and add to examples in docs (zircote#1735)
1 parent 1902bbd commit fbefe5a

File tree

11 files changed

+236
-3
lines changed

11 files changed

+236
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Polymorphism
2+
3+
Demonstrates the use of `Discriminator`

docs/examples/specs/polymorphism/annotations/Controller.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Controller
2929
/**
3030
* @OA\Get(
3131
* path="/test",
32+
* operationId="test",
3233
* description="Get test",
3334
* tags={"api"},
3435
* @OA\Response(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Examples\Specs\Polymorphism\Attributes;
8+
9+
use OpenApi\Attributes as OAT;
10+
11+
#[OAT\Schema(
12+
schema: 'Responsible',
13+
oneOf: [
14+
new OAT\Schema(ref: Fl::class),
15+
new OAT\Schema(ref: Employee::class),
16+
],
17+
discriminator: new OAT\Discriminator(
18+
propertyName: 'type',
19+
mapping: [
20+
'fl' => '#/components/schemas/FlResponsible',
21+
'employee' => '#/components/schemas/EmployeeResponsible',
22+
]
23+
)
24+
)]
25+
abstract class AbstractResponsible
26+
{
27+
protected const TYPE = null;
28+
29+
#[OAT\Property(
30+
nullable: false,
31+
enum: ['employee', 'assignee', 'fl']
32+
)]
33+
protected string $type;
34+
35+
public function __construct()
36+
{
37+
$this->type = static::TYPE;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Examples\Specs\Polymorphism\Attributes;
8+
9+
use OpenApi\Attributes as OAT;
10+
11+
#[OAT\Info(
12+
title: 'Polymorphism',
13+
description: 'Polymorphism example',
14+
version: '1',
15+
contact: new OAT\Contact(
16+
name: 'Swagger API Team'
17+
)
18+
)]
19+
#[OAT\Tag(
20+
name: 'api',
21+
description: 'API operations'
22+
)]
23+
#[OAT\Server(
24+
url: 'https://example.localhost',
25+
description: 'API server'
26+
)]
27+
class Controller
28+
{
29+
#[OAT\Get(
30+
path: '/test',
31+
operationId: 'test',
32+
description: 'Get test',
33+
tags: ['api'],
34+
responses: [
35+
new OAT\Response(
36+
response: 200,
37+
description: 'Polymorphism',
38+
content: new OAT\JsonContent(
39+
ref: Request::class
40+
)
41+
),
42+
]
43+
)]
44+
public function getProduct($id)
45+
{
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Examples\Specs\Polymorphism\Attributes;
8+
9+
use OpenApi\Attributes as OAT;
10+
11+
#[OAT\Schema(
12+
schema: 'EmployeeResponsible'
13+
)]
14+
final class Employee extends AbstractResponsible
15+
{
16+
#[OAT\Property(
17+
property: 'type'
18+
)]
19+
protected const TYPE = 'Virtual';
20+
21+
#[OAT\Property(
22+
nullable: false
23+
)]
24+
public string $property2;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Examples\Specs\Polymorphism\Attributes;
8+
9+
use OpenApi\Attributes as OAT;
10+
11+
#[OAT\Schema(
12+
schema: 'FlResponsible'
13+
)]
14+
final class Fl extends AbstractResponsible
15+
{
16+
public const TYPE = 'fl';
17+
18+
#[OAT\Property()]
19+
public ?string $property3;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Examples\Specs\Polymorphism\Attributes;
8+
9+
use OpenApi\Attributes as OAT;
10+
11+
#[OAT\Schema()]
12+
final class Request
13+
{
14+
protected const TYPE = 'employee';
15+
16+
#[OAT\Property()]
17+
public AbstractResponsible $payload;
18+
}

docs/examples/specs/polymorphism/polymorphism-3.0.0.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ paths:
1515
tags:
1616
- api
1717
description: 'Get test'
18-
operationId: c2ce98e75eedefc189265e365c6e54de
18+
operationId: test
1919
responses:
2020
'200':
2121
description: Polymorphism

docs/examples/specs/polymorphism/polymorphism-3.1.0.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ paths:
1515
tags:
1616
- api
1717
description: 'Get test'
18-
operationId: c2ce98e75eedefc189265e365c6e54de
18+
operationId: test
1919
responses:
2020
'200':
2121
description: Polymorphism

docs/guide/examples.md

+80
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,83 @@ Classic petstore sample app. Uses `OAuth`
299299
</template>
300300
</codeblock>
301301

302+
303+
## Polymorphism
304+
305+
Demonstrates the use of `Discriminator`
306+
307+
### Controller.php
308+
309+
<codeblock id="polymorphism-Controller">
310+
<template v-slot:at>
311+
312+
<<< @/examples/specs/polymorphism/attributes/Controller.php
313+
314+
</template>
315+
<template v-slot:an>
316+
317+
<<< @/examples/specs/polymorphism/annotations/Controller.php
318+
319+
</template>
320+
</codeblock>
321+
322+
### Request.php
323+
324+
<codeblock id="polymorphism-Request">
325+
<template v-slot:at>
326+
327+
<<< @/examples/specs/polymorphism/attributes/Request.php
328+
329+
</template>
330+
<template v-slot:an>
331+
332+
<<< @/examples/specs/polymorphism/annotations/Request.php
333+
334+
</template>
335+
</codeblock>
336+
337+
### Employee.php
338+
339+
<codeblock id="polymorphism-Employee">
340+
<template v-slot:at>
341+
342+
<<< @/examples/specs/polymorphism/attributes/Employee.php
343+
344+
</template>
345+
<template v-slot:an>
346+
347+
<<< @/examples/specs/polymorphism/annotations/Employee.php
348+
349+
</template>
350+
</codeblock>
351+
352+
### AbstractResponsible.php
353+
354+
<codeblock id="polymorphism-AbstractResponsible">
355+
<template v-slot:at>
356+
357+
<<< @/examples/specs/polymorphism/attributes/AbstractResponsible.php
358+
359+
</template>
360+
<template v-slot:an>
361+
362+
<<< @/examples/specs/polymorphism/annotations/AbstractResponsible.php
363+
364+
</template>
365+
</codeblock>
366+
367+
### Fl.php
368+
369+
<codeblock id="polymorphism-Fl">
370+
<template v-slot:at>
371+
372+
<<< @/examples/specs/polymorphism/attributes/Fl.php
373+
374+
</template>
375+
<template v-slot:an>
376+
377+
<<< @/examples/specs/polymorphism/annotations/Fl.php
378+
379+
</template>
380+
</codeblock>
381+

tools/examplegen.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ob_start();
1010
echo $gen->preamble('Example');
1111

12-
foreach (['api', 'petstore'] as $name) {
12+
foreach (['api', 'petstore', 'polymorphism'] as $name) {
1313
$exampleFolder = $gen->examplePath("specs/$name");
1414

1515
echo PHP_EOL;

0 commit comments

Comments
 (0)