Skip to content

Commit 5e7e6c5

Browse files
authored
Fix the generator when header is assigned to a required property (#1851)
1 parent aaf11ec commit 5e7e6c5

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/Generator/CodeGenerator/PopulatorGenerator.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,39 @@ private function generatePopulator(Operation $operation, StructureShape $shape,
190190
$memberShape = $member->getShape();
191191
switch ($memberShape->getType()) {
192192
case 'timestamp':
193-
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? new \DateTimeImmutable($headers["LOCATION_NAME"][0]) : null;' . "\n", [
194-
'PROPERTY_NAME' => $propertyName,
195-
'LOCATION_NAME' => $locationName,
196-
]);
193+
$propertyValue = 'new \DateTimeImmutable($headers["LOCATION_NAME"][0])';
197194

198195
break;
199196
case 'integer':
200197
case 'long':
201-
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? (int) $headers["LOCATION_NAME"][0] : null;' . "\n", [
202-
'PROPERTY_NAME' => $propertyName,
203-
'LOCATION_NAME' => $locationName,
204-
]);
198+
$propertyValue = '(int) $headers["LOCATION_NAME"][0]';
205199

206200
break;
207201
case 'boolean':
208202
$this->requirementsRegistry->addRequirement('ext-filter');
209203

210-
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? filter_var($headers["LOCATION_NAME"][0], FILTER_VALIDATE_BOOLEAN) : null;' . "\n", [
211-
'PROPERTY_NAME' => $propertyName,
212-
'LOCATION_NAME' => $locationName,
213-
]);
204+
$propertyValue = 'filter_var($headers["LOCATION_NAME"][0], FILTER_VALIDATE_BOOLEAN)';
214205

215206
break;
216207
case 'string':
217-
$body .= strtr('$this->PROPERTY_NAME = $headers["LOCATION_NAME"][0] ?? null;' . "\n", [
218-
'PROPERTY_NAME' => $propertyName,
219-
'LOCATION_NAME' => $locationName,
220-
]);
208+
$propertyValue = '$headers["LOCATION_NAME"][0]';
221209

222210
break;
223211
default:
224212
throw new \RuntimeException(\sprintf('Type %s is not yet implemented', $memberShape->getType()));
225213
}
214+
if (!$member->isRequired()) {
215+
if ('$headers["LOCATION_NAME"][0]' === $propertyValue) {
216+
$propertyValue .= '?? null';
217+
} else {
218+
$propertyValue = 'isset($headers["LOCATION_NAME"][0]) ? ' . $propertyValue . ' : null';
219+
}
220+
}
221+
222+
$body .= strtr('$this->PROPERTY_NAME = ' . $propertyValue . ';' . "\n", [
223+
'PROPERTY_NAME' => $propertyName,
224+
'LOCATION_NAME' => $locationName,
225+
]);
226226
}
227227

228228
// This will catch arbitrary values that exists in undefined "headers"

0 commit comments

Comments
 (0)