Skip to content

Commit 5cb0c02

Browse files
authored
Merge pull request #64 from php-api-clients/detect-nullable-objects-done-through-arrays
Detect nullable objects done through arrays
2 parents b3953ba + 4add1a1 commit 5cb0c02

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

src/Generator/Schema.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,39 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
104104
)
105105
)
106106
);
107+
$propertyType = $property->type;
107108
$setDefaylt = true;
108109
$nullable = '';
109110
if ($property->nullable) {
110111
$nullable = '?';
111112
$propertyStmt->setDefault(null);
112113
}
113-
if (is_string($property->type)) {
114+
115+
if (
116+
is_array($propertyType) &&
117+
count($propertyType) === 2 &&
118+
(
119+
in_array(null, $propertyType) ||
120+
in_array("null", $propertyType)
121+
)
122+
) {
123+
foreach ($propertyType as $pt) {
124+
if ($pt !== null && $pt !== "null") {
125+
$propertyType = $pt;
126+
break;
127+
}
128+
}
129+
130+
$nullable = '?';
131+
}
132+
133+
if (is_string($propertyType)) {
114134
if (is_array($schema->required) && !in_array($propertyName, $schema->required, false)) {
115135
$nullable = '?';
116136
$propertyStmt->setDefault(null);
117137
}
118138

119-
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) {
139+
if ($propertyType === 'array' && $property->items instanceof OpenAPiSchema) {
120140
// if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
121141
$methodDocBlock[] = '@return array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items, $className . '\\' . (new Convert($propertyName))->toPascal()) . '>';
122142
$propertyDocBlock[] = '@var array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items, $className . '\\' . (new Convert($propertyName))->toPascal()) . '>';
@@ -130,7 +150,7 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
130150
}
131151

132152

133-
if (is_string($property->type)) {
153+
if (is_string($propertyType)) {
134154
$t = str_replace([
135155
'object',
136156
'integer',
@@ -145,7 +165,7 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
145165
'',
146166
'',
147167
'bool',
148-
], $property->type);
168+
], $propertyType);
149169

150170
if ($t !== '') {
151171
$propertyStmt->setType(($t === 'array' ? '' : $nullable) . $t);
@@ -154,6 +174,8 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
154174
}
155175
}
156176

177+
// 74908
178+
157179
if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)*/) {
158180
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->anyOf[0], $className . '\\' . (new Convert($propertyName))->toPascal());
159181
$propertyStmt->setType($nullable . $fqcnn);
@@ -168,15 +190,16 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
168190
$setDefaylt = false;
169191
}
170192

171-
if ($property->type === 'object' && $property instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property), $schemaClassNameMap)*/) {
193+
// if (($property->type === 'object' || (is_array($property->type) && count($property->type) === 2)) && $property instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property), $schemaClassNameMap)*/) {
194+
if ($propertyType === 'object') {
172195
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property, $className . '\\' . (new Convert($propertyName))->toPascal());
173196
$propertyStmt->setType($nullable . $fqcnn);
174197
$method->setReturnType($nullable . $fqcnn);
175198
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
176199
$setDefaylt = false;
177200
}
178201

179-
if (is_string($property->type)) {
202+
if (is_string($propertyType)) {
180203
$t = str_replace([
181204
'object',
182205
'integer',
@@ -187,7 +210,7 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
187210
'int',
188211
'',
189212
'bool',
190-
], $property->type);
213+
], $propertyType);
191214
if ($t !== '') {
192215
if ($t === 'array' && $setDefaylt === true) {
193216
$propertyStmt->setDefault([]);

0 commit comments

Comments
 (0)