Skip to content

Commit e274585

Browse files
authored
fix(openapi): model Example, Header and Reference (#5716)
1 parent 146f553 commit e274585

File tree

5 files changed

+343
-1
lines changed

5 files changed

+343
-1
lines changed

src/OpenApi/Model/Components.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ final class Components
1919

2020
private ?\ArrayObject $schemas;
2121

22+
/**
23+
* @param \ArrayObject<string, Schema>|\ArrayObject<string, Reference> $schemas
24+
* @param \ArrayObject<string, Response>|\ArrayObject<string, Reference> $responses
25+
* @param \ArrayObject<string, Parameter>|\ArrayObject<string, Reference> $parameters
26+
* @param \ArrayObject<string, Example>|\ArrayObject<string, Reference> $examples
27+
* @param \ArrayObject<string, RequestBody>|\ArrayObject<string, Reference> $requestBodies
28+
* @param \ArrayObject<string, Header>|\ArrayObject<string, Reference> $headers
29+
* @param \ArrayObject<string, SecurityScheme>|\ArrayObject<string, Reference> $headers
30+
* @param \ArrayObject<string, Link>|\ArrayObject<string, Reference> $links
31+
* @param \ArrayObject<string, array<string, PathItem>>|\ArrayObject<string, array<string, Reference>> $callbacks
32+
* @param \ArrayObject<string, PathItem>|\ArrayObject<string, Reference> $pathItems
33+
*/
2234
public function __construct(\ArrayObject $schemas = null, private ?\ArrayObject $responses = null, private ?\ArrayObject $parameters = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $requestBodies = null, private ?\ArrayObject $headers = null, private ?\ArrayObject $securitySchemes = null, private ?\ArrayObject $links = null, private ?\ArrayObject $callbacks = null, private ?\ArrayObject $pathItems = null)
2335
{
2436
$schemas?->ksort();

src/OpenApi/Model/Example.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\OpenApi\Model;
15+
16+
final class Example
17+
{
18+
use ExtensionTrait;
19+
20+
public function __construct(private ?string $summary = null, private ?string $description = null, private mixed $value = null, private ?string $externalValue = null)
21+
{
22+
}
23+
24+
public function getSummary(): ?string
25+
{
26+
return $this->summary;
27+
}
28+
29+
public function withSummary(string $summary): self
30+
{
31+
$clone = clone $this;
32+
$clone->summary = $summary;
33+
34+
return $clone;
35+
}
36+
37+
public function getDescription(): ?string
38+
{
39+
return $this->description;
40+
}
41+
42+
public function withDescription(string $description): self
43+
{
44+
$clone = clone $this;
45+
$clone->description = $description;
46+
47+
return $clone;
48+
}
49+
50+
public function getValue(): mixed
51+
{
52+
return $this->value;
53+
}
54+
55+
public function withValue(mixed $value): self
56+
{
57+
$clone = clone $this;
58+
$clone->value = $value;
59+
60+
return $clone;
61+
}
62+
63+
public function getExternalValue(): ?string
64+
{
65+
return $this->externalValue;
66+
}
67+
68+
public function withExternalValue(string $externalValue): self
69+
{
70+
$clone = clone $this;
71+
$clone->externalValue = $externalValue;
72+
73+
return $clone;
74+
}
75+
}

src/OpenApi/Model/Header.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\OpenApi\Model;
15+
16+
final class Header
17+
{
18+
use ExtensionTrait;
19+
20+
public function __construct(private readonly string $in = 'header', private string $description = '', private bool $required = false, private bool $deprecated = false, private bool $allowEmptyValue = false, private array $schema = [], private ?string $style = null, private bool $explode = false, private bool $allowReserved = false, private $example = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $content = null)
21+
{
22+
if (null === $style) {
23+
$this->style = 'simple';
24+
}
25+
}
26+
27+
public function getIn(): string
28+
{
29+
return $this->in;
30+
}
31+
32+
public function getDescription(): string
33+
{
34+
return $this->description;
35+
}
36+
37+
public function getRequired(): bool
38+
{
39+
return $this->required;
40+
}
41+
42+
public function getDeprecated(): bool
43+
{
44+
return $this->deprecated;
45+
}
46+
47+
public function canAllowEmptyValue(): bool
48+
{
49+
return $this->allowEmptyValue;
50+
}
51+
52+
public function getAllowEmptyValue(): bool
53+
{
54+
return $this->allowEmptyValue;
55+
}
56+
57+
public function getSchema(): array
58+
{
59+
return $this->schema;
60+
}
61+
62+
public function getStyle(): string
63+
{
64+
return $this->style;
65+
}
66+
67+
public function canExplode(): bool
68+
{
69+
return $this->explode;
70+
}
71+
72+
public function getExplode(): bool
73+
{
74+
return $this->explode;
75+
}
76+
77+
public function canAllowReserved(): bool
78+
{
79+
return $this->allowReserved;
80+
}
81+
82+
public function getAllowReserved(): bool
83+
{
84+
return $this->allowReserved;
85+
}
86+
87+
public function getExample()
88+
{
89+
return $this->example;
90+
}
91+
92+
public function getExamples(): ?\ArrayObject
93+
{
94+
return $this->examples;
95+
}
96+
97+
public function getContent(): ?\ArrayObject
98+
{
99+
return $this->content;
100+
}
101+
102+
public function withDescription(string $description): self
103+
{
104+
$clone = clone $this;
105+
$clone->description = $description;
106+
107+
return $clone;
108+
}
109+
110+
public function withRequired(bool $required): self
111+
{
112+
$clone = clone $this;
113+
$clone->required = $required;
114+
115+
return $clone;
116+
}
117+
118+
public function withDeprecated(bool $deprecated): self
119+
{
120+
$clone = clone $this;
121+
$clone->deprecated = $deprecated;
122+
123+
return $clone;
124+
}
125+
126+
public function withAllowEmptyValue(bool $allowEmptyValue): self
127+
{
128+
$clone = clone $this;
129+
$clone->allowEmptyValue = $allowEmptyValue;
130+
131+
return $clone;
132+
}
133+
134+
public function withSchema(array $schema): self
135+
{
136+
$clone = clone $this;
137+
$clone->schema = $schema;
138+
139+
return $clone;
140+
}
141+
142+
public function withStyle(string $style): self
143+
{
144+
$clone = clone $this;
145+
$clone->style = $style;
146+
147+
return $clone;
148+
}
149+
150+
public function withExplode(bool $explode): self
151+
{
152+
$clone = clone $this;
153+
$clone->explode = $explode;
154+
155+
return $clone;
156+
}
157+
158+
public function withAllowReserved(bool $allowReserved): self
159+
{
160+
$clone = clone $this;
161+
$clone->allowReserved = $allowReserved;
162+
163+
return $clone;
164+
}
165+
166+
public function withExample($example): self
167+
{
168+
$clone = clone $this;
169+
$clone->example = $example;
170+
171+
return $clone;
172+
}
173+
174+
public function withExamples(\ArrayObject $examples): self
175+
{
176+
$clone = clone $this;
177+
$clone->examples = $examples;
178+
179+
return $clone;
180+
}
181+
182+
public function withContent(\ArrayObject $content): self
183+
{
184+
$clone = clone $this;
185+
$clone->content = $content;
186+
187+
return $clone;
188+
}
189+
}

src/OpenApi/Model/Reference.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\OpenApi\Model;
15+
16+
use Symfony\Component\Serializer\Annotation\SerializedName;
17+
18+
final class Reference
19+
{
20+
use ExtensionTrait;
21+
22+
public function __construct(private string $ref, private ?string $summary = null, private ?string $description = null)
23+
{
24+
}
25+
26+
public function getSummary(): ?string
27+
{
28+
return $this->summary;
29+
}
30+
31+
public function withSummary(string $summary): self
32+
{
33+
$clone = clone $this;
34+
$clone->summary = $summary;
35+
36+
return $clone;
37+
}
38+
39+
public function getDescription(): ?string
40+
{
41+
return $this->description;
42+
}
43+
44+
public function withDescription(string $description): self
45+
{
46+
$clone = clone $this;
47+
$clone->description = $description;
48+
49+
return $clone;
50+
}
51+
52+
#[SerializedName('$ref')]
53+
public function getRef(): string
54+
{
55+
return $this->ref;
56+
}
57+
58+
public function withRef(string $ref): self
59+
{
60+
$clone = clone $this;
61+
$clone->ref = $ref;
62+
63+
return $clone;
64+
}
65+
}

src/OpenApi/Tests/Serializer/OpenApiNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
3838
use ApiPlatform\OpenApi\Model\Parameter;
3939
use ApiPlatform\OpenApi\Model\Paths;
40+
use ApiPlatform\OpenApi\Model\Schema;
4041
use ApiPlatform\OpenApi\Model\Server;
4142
use ApiPlatform\OpenApi\OpenApi;
4243
use ApiPlatform\OpenApi\Options;
@@ -64,7 +65,7 @@ class OpenApiNormalizerTest extends TestCase
6465

6566
public function testNormalizeWithSchemas(): void
6667
{
67-
$openApi = new OpenApi(new Info('My API', '1.0.0', 'An amazing API'), [new Server('https://example.com')], new Paths(), new Components(new \ArrayObject(['z' => [], 'b' => []])));
68+
$openApi = new OpenApi(new Info('My API', '1.0.0', 'An amazing API'), [new Server('https://example.com')], new Paths(), new Components(new \ArrayObject(['z' => new Schema(), 'b' => new Schema()])));
6869
$encoders = [new JsonEncoder()];
6970
$normalizers = [new ObjectNormalizer()];
7071

0 commit comments

Comments
 (0)