Skip to content

Commit da48dba

Browse files
author
Сергей Лавриеня
committed
add nullable type support
?string - type: [string, null] not type: string
1 parent ab37d9e commit da48dba

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/Generator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ protected function generateProperty(PropertyInterface $property): ?Property
137137

138138
$required = $default === null && !$type->allowsNull();
139139
if ($type->isBuiltin()) {
140-
return new Property($type->getName(), $options, $title, $description, $required, $default, $format);
140+
return new Property($type->getName(), $options, $title, $description, $required, $type->allowsNull(), $default, $format);
141141
}
142142

143143
// Class or enum
144144
$class = $type->getName();
145145

146146
return \is_string($class) && \class_exists($class)
147-
? new Property($class, [], $title, $description, $required, $default, $format)
147+
? new Property($class, [], $title, $description, $required, $type->allowsNull(), $default, $format)
148148
: null;
149149
}
150150
}

src/Schema/Property.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(
2020
public readonly string $title = '',
2121
public readonly string $description = '',
2222
public readonly bool $required = false,
23+
public readonly bool $allowsNull = false,
2324
public readonly mixed $default = null,
2425
public readonly ?Format $format = null,
2526
) {
@@ -61,7 +62,7 @@ public function jsonSerialize(): array
6162
return $property;
6263
}
6364

64-
$property['type'] = $this->type->value;
65+
$property['type'] = $this->allowsNull ? [$this->type->value, Type::Null] : $this->type->value;
6566

6667
if ($this->type === Type::Array) {
6768
if (\count($this->options) === 1) {

0 commit comments

Comments
 (0)