Skip to content

Commit 8e5e282

Browse files
committed
fix(OpenApiType): Wrap $ref in allOf to allow nullable
Signed-off-by: provokateurin <[email protected]>
1 parent 4c7a66a commit 8e5e282

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

src/OpenApiType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ public static function resolve(string $context, array $definitions, ParamTagValu
265265

266266
if ($node instanceof NullableTypeNode || $node instanceof NullableType) {
267267
$type = self::resolve($context, $definitions, $node->type);
268+
if ($type->ref !== null) {
269+
// https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-354037150
270+
$type = new OpenApiType(
271+
$context,
272+
allOf: [$type],
273+
);
274+
}
268275
$type->nullable = true;
269276
return $type;
270277
}
@@ -336,6 +343,13 @@ enum: $values,
336343

337344
if (count($items) == 1) {
338345
$type = $items[0];
346+
if ($type->ref !== null) {
347+
// https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-354037150
348+
$type = new OpenApiType(
349+
$context,
350+
allOf: [$type],
351+
);
352+
}
339353
$type->nullable = $nullable;
340354
return $type;
341355
}

tests/lib/ResponseDefinitions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
* // A comment.
6464
* // Another comment.
6565
* multipleComments: string,
66+
* ref1: ?NotificationsPushDevice,
67+
* ref2: null|NotificationsPushDevice,
68+
* ref3: string|null|NotificationsPushDevice,
6669
* }
6770
*/
6871
class ResponseDefinitions {

tests/openapi-full.json

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@
256256
"required": [
257257
"publicKey",
258258
"signature",
259-
"multipleComments"
259+
"multipleComments",
260+
"ref1",
261+
"ref2",
262+
"ref3"
260263
],
261264
"properties": {
262265
"publicKey": {
@@ -270,6 +273,33 @@
270273
"multipleComments": {
271274
"type": "string",
272275
"description": "A comment. Another comment."
276+
},
277+
"ref1": {
278+
"nullable": true,
279+
"allOf": [
280+
{
281+
"$ref": "#/components/schemas/PushDevice"
282+
}
283+
]
284+
},
285+
"ref2": {
286+
"nullable": true,
287+
"allOf": [
288+
{
289+
"$ref": "#/components/schemas/PushDevice"
290+
}
291+
]
292+
},
293+
"ref3": {
294+
"nullable": true,
295+
"anyOf": [
296+
{
297+
"type": "string"
298+
},
299+
{
300+
"$ref": "#/components/schemas/PushDevice"
301+
}
302+
]
273303
}
274304
}
275305
}

tests/openapi.json

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,48 @@
218218
}
219219
}
220220
},
221+
"PushDevice": {
222+
"allOf": [
223+
{
224+
"$ref": "#/components/schemas/PushDeviceBase"
225+
},
226+
{
227+
"type": "object",
228+
"required": [
229+
"publicKey",
230+
"signature"
231+
],
232+
"properties": {
233+
"publicKey": {
234+
"type": "string"
235+
},
236+
"signature": {
237+
"type": "string"
238+
}
239+
}
240+
}
241+
]
242+
},
243+
"PushDeviceBase": {
244+
"type": "object",
245+
"required": [
246+
"deviceIdentifier"
247+
],
248+
"properties": {
249+
"deviceIdentifier": {
250+
"type": "string"
251+
}
252+
}
253+
},
221254
"RequestProperty": {
222255
"type": "object",
223256
"required": [
224257
"publicKey",
225258
"signature",
226-
"multipleComments"
259+
"multipleComments",
260+
"ref1",
261+
"ref2",
262+
"ref3"
227263
],
228264
"properties": {
229265
"publicKey": {
@@ -237,6 +273,33 @@
237273
"multipleComments": {
238274
"type": "string",
239275
"description": "A comment. Another comment."
276+
},
277+
"ref1": {
278+
"nullable": true,
279+
"allOf": [
280+
{
281+
"$ref": "#/components/schemas/PushDevice"
282+
}
283+
]
284+
},
285+
"ref2": {
286+
"nullable": true,
287+
"allOf": [
288+
{
289+
"$ref": "#/components/schemas/PushDevice"
290+
}
291+
]
292+
},
293+
"ref3": {
294+
"nullable": true,
295+
"anyOf": [
296+
{
297+
"type": "string"
298+
},
299+
{
300+
"$ref": "#/components/schemas/PushDevice"
301+
}
302+
]
240303
}
241304
}
242305
}

0 commit comments

Comments
 (0)