Skip to content

Commit

Permalink
Use {"nullable": true, "anyOf": [{"$ref": ...}]} to comply with Ope…
Browse files Browse the repository at this point in the history
…nAPI 3.0

OpenAPI 3.1 is not yet released, but fixes nullability in
the way we had fixed it before (via `{"oneOf": [{"type": "null"}, ...]}`) in
OAI/OpenAPI-Specification#1977.

Until OpenAPI 3.1 is released, things like ``{"type": ["integer", "null"]}` are
not valid definitions (because `"null"` is not yet a recognized type).

We'll stick to OpenAPI 3.0 for now, using:

 * `{"nullable": true, ...}` for simple types
 * `{"nullable": true, "anyOf": [{"$ref": ...}]}` for type references
  • Loading branch information
Ocramius authored and teohhanhui committed Feb 27, 2020
1 parent aac6d9c commit 4dd589e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, a
return $jsonSchema;
}

return [
'oneOf' => [
['type' => 'null'],
$jsonSchema,
],
];
if (\array_key_exists('$ref', $jsonSchema)) {
return [
'nullable' => true,
'anyOf' => [$jsonSchema],
];
}

return array_merge($jsonSchema, ['nullable' => true]);
}
}

0 comments on commit 4dd589e

Please sign in to comment.