Skip to content

Commit 374262e

Browse files
authored
Handle legacy MediaType::encoding values (#1845)
1 parent 8542c1f commit 374262e

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

src/Annotations/MediaType.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,29 @@ class MediaType extends AbstractAnnotation
8282
Response::class,
8383
RequestBody::class,
8484
];
85+
86+
public function __construct(array $properties)
87+
{
88+
if (array_key_exists('encoding', $properties)) {
89+
$properties['encoding'] = $this->encodingCompat(
90+
$properties['encoding'],
91+
fn (array $args): Encoding => new Encoding($args),
92+
);
93+
}
94+
parent::__construct($properties);
95+
}
96+
97+
protected function encodingCompat($encoding, callable $factory)
98+
{
99+
if (!is_array($encoding)) {
100+
return $encoding;
101+
}
102+
103+
$compat = [];
104+
foreach ($encoding as $name => $value) {
105+
$compat[] = is_array($value) ? $factory([...$value, 'property' => $name]) : $value;
106+
}
107+
108+
return $compat;
109+
}
85110
}

src/Attributes/MediaType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function __construct(
3333
'example' => $example,
3434
'x' => $x ?? Generator::UNDEFINED,
3535
'attachables' => $attachables ?? Generator::UNDEFINED,
36-
'value' => $this->combine($schema, $examples, $encoding),
36+
'value' => $this->combine($schema, $examples, $this->encodingCompat(
37+
$encoding,
38+
fn (array $args): Encoding => new Encoding(...$args),
39+
)),
3740
]);
3841
}
3942
}

tests/Fixtures/Scratch/Encoding.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace OpenApi\Tests\Fixtures\Scratch;
88

9+
use OpenApi\Annotations as OA;
910
use OpenApi\Attributes as OAT;
1011

1112
#[OAT\Schema()]
@@ -133,4 +134,57 @@ public function multipartFormDataJsonContent()
133134
{
134135

135136
}
137+
138+
#[OAT\Post(
139+
path: '/endpoint/bc-encoding-attr',
140+
requestBody: new OAT\RequestBody(
141+
content: new OAT\MediaType(
142+
mediaType: 'multipart/form-data',
143+
schema: new OAT\Schema(
144+
properties: [
145+
new OAT\Property(property: 'encname', type: 'string'),
146+
],
147+
),
148+
encoding: [
149+
'encname' => [
150+
'contentType' => 'application/json',
151+
],
152+
],
153+
),
154+
),
155+
responses: [
156+
new OAT\Response(
157+
response: 200,
158+
description: 'All good',
159+
),
160+
]
161+
)]
162+
public function bcEncodingAttr()
163+
{
164+
165+
}
166+
167+
/**
168+
* @OA\Post(
169+
* path="/endpoint/bc-encoding-annot",
170+
* @OA\RequestBody(
171+
* @OA\MediaType(
172+
* mediaType="multipart-form-data",
173+
* @OA\Schema(
174+
* @OA\Property(property="encname")
175+
* ),
176+
* encoding={
177+
* "encname": {
178+
* "contentType": "application/json"
179+
* }
180+
* }
181+
* )
182+
* ),
183+
* @OA\Response(response="200", description="OK")
184+
* )
185+
*/
186+
public function bcEncodingAnnot()
187+
{
188+
189+
}
136190
}

tests/Fixtures/Scratch/Encoding3.0.0.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,39 @@ paths:
6363
responses:
6464
'200':
6565
description: 'All good'
66+
/endpoint/bc-encoding-attr:
67+
post:
68+
operationId: 93c969c0f67dc55b9f7f88f6e46d4230
69+
requestBody:
70+
content:
71+
multipart/form-data:
72+
schema:
73+
properties:
74+
encname:
75+
type: string
76+
type: object
77+
encoding:
78+
encname:
79+
contentType: application/json
80+
responses:
81+
'200':
82+
description: 'All good'
83+
/endpoint/bc-encoding-annot:
84+
post:
85+
operationId: efe7afb9b99c111a0d781e14982abcdf
86+
requestBody:
87+
content:
88+
multipart-form-data:
89+
schema:
90+
properties:
91+
encname: { }
92+
type: object
93+
encoding:
94+
encname:
95+
contentType: application/json
96+
responses:
97+
'200':
98+
description: OK
6699
components:
67100
schemas:
68101
EncodingMetadata: { }

tests/Fixtures/Scratch/Encoding3.1.0.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,39 @@ paths:
6363
responses:
6464
'200':
6565
description: 'All good'
66+
/endpoint/bc-encoding-attr:
67+
post:
68+
operationId: 93c969c0f67dc55b9f7f88f6e46d4230
69+
requestBody:
70+
content:
71+
multipart/form-data:
72+
schema:
73+
properties:
74+
encname:
75+
type: string
76+
type: object
77+
encoding:
78+
encname:
79+
contentType: application/json
80+
responses:
81+
'200':
82+
description: 'All good'
83+
/endpoint/bc-encoding-annot:
84+
post:
85+
operationId: efe7afb9b99c111a0d781e14982abcdf
86+
requestBody:
87+
content:
88+
multipart-form-data:
89+
schema:
90+
properties:
91+
encname: { }
92+
type: object
93+
encoding:
94+
encname:
95+
contentType: application/json
96+
responses:
97+
'200':
98+
description: OK
6699
components:
67100
schemas:
68101
EncodingMetadata: { }

0 commit comments

Comments
 (0)