Skip to content

Commit fdb485d

Browse files
bmijnendonckxsoyuka
authored andcommitted
fix(openapi): example and default with nullable value not being shown
1 parent abb1fac commit fdb485d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ public function create(string $resourceClass, string $property, array $options =
8888

8989
$types = $propertyMetadata->getBuiltinTypes() ?? [];
9090

91-
if (!\array_key_exists('default', $propertySchema) && !empty($default = $propertyMetadata->getDefault()) && (!\count($types) || null === ($className = $types[0]->getClassName()) || !$this->isResourceClass($className))) {
91+
if (
92+
!\array_key_exists('default', $propertySchema)
93+
&& null !== ($default = $propertyMetadata->getDefault())
94+
&& false === (\is_array($default) && empty($default))
95+
&& (!\count($types) || null === ($className = $types[0]->getClassName()) || !$this->isResourceClass($className))
96+
) {
9297
if ($default instanceof \BackedEnum) {
9398
$default = $default->value;
9499
}
95100
$propertySchema['default'] = $default;
96101
}
97102

98-
if (!\array_key_exists('example', $propertySchema) && !empty($example = $propertyMetadata->getExample())) {
103+
if (!\array_key_exists('example', $propertySchema) && null !== ($example = $propertyMetadata->getExample()) && false === (\is_array($example) && empty($example))) {
99104
$propertySchema['example'] = $example;
100105
}
101106

@@ -104,7 +109,8 @@ public function create(string $resourceClass, string $property, array $options =
104109
}
105110

106111
// never override the following keys if at least one is already set or if there's a custom openapi context
107-
if ([] === $types
112+
if (
113+
[] === $types
108114
|| ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
109115
|| \array_key_exists('type', $propertyMetadata->getOpenapiContext() ?? [])
110116
) {

0 commit comments

Comments
 (0)