Skip to content

Commit fdc419b

Browse files
authored
Fix some issues in the code generator (#1454)
* Fix the GenerateCommand when loading a manifest without variables * Fix the generated test assertions They should use the same logic than the PopulatorGenerator to get the name of the getter method, to ensure we get the same output. * Add missing client use statement in the generated client test * Fix type in variable name
1 parent 43b6bc5 commit fdc419b

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/CodeGenerator/src/Command/GenerateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ private function fixCs(ClassName $clientClass, SymfonyStyle $io): void
442442

443443
private function loadFile(string $path, string $cacheKey, array $patch = []): array
444444
{
445-
$path = strtr($path, $this->loadManifest()['variables'] ?? [[]]);
445+
$path = strtr($path, $this->loadManifest()['variables'] ?? []);
446446

447447
$data = $this->cache->get(__CLASS__ . ':' . $cacheKey);
448448
if (null === $data || $path !== ($data['path'] ?? null)) {

src/CodeGenerator/src/Generator/TestGenerator.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,25 @@ private function generateInput(Operation $operation, StructureShape $shape): voi
9090
switch ($operation->getService()->getProtocol()) {
9191
case 'rest-xml':
9292
$stub = substr(var_export($this->arrayToXml($exampleInput ?? ['change' => 'it']), true), 1, -1);
93-
$contenType = 'application/xml';
93+
$contentType = 'application/xml';
9494

9595
break;
9696
case 'rest-json':
9797
$stub = substr(var_export(json_encode($exampleInput ?? ['change' => 'it'], \JSON_PRETTY_PRINT), true), 1, -1);
98-
$contenType = 'application/json';
98+
$contentType = 'application/json';
9999

100100
break;
101101
case 'json':
102102
$stub = substr(var_export(json_encode($exampleInput ?? ['change' => 'it'], \JSON_PRETTY_PRINT), true), 1, -1);
103-
$contenType = 'application/x-amz-json-' . number_format($operation->getService()->getJsonVersion(), 1);
103+
$contentType = 'application/x-amz-json-' . number_format($operation->getService()->getJsonVersion(), 1);
104104

105105
break;
106106
case 'query':
107107
$stub = substr(var_export($exampleInput ? (\is_array($exampleInput) ? http_build_query($exampleInput, '', '&', \PHP_QUERY_RFC3986) : $exampleInput) : "
108108
Action={$operation->getName()}
109109
&Version={$operation->getApiVersion()}
110110
", true), 1, -1);
111-
$contenType = 'application/x-www-form-urlencoded';
111+
$contentType = 'application/x-www-form-urlencoded';
112112

113113
break;
114114
default:
@@ -137,7 +137,7 @@ private function generateInput(Operation $operation, StructureShape $shape): voi
137137
'SERVICE' => strtolower($operation->getService()->getName()),
138138
'METHOD' => $operation->getHttpMethod(),
139139
'OPERATION' => $operation->getName(),
140-
'CONTENT_TYPE' => $contenType,
140+
'CONTENT_TYPE' => $contentType,
141141
'STUB' => trim($stub),
142142
]));
143143
}
@@ -341,6 +341,7 @@ private function generateClient(Operation $operation): void
341341
$classBuilder->addUse(MockHttpClient::class);
342342
$classBuilder->addUse(NullProvider::class);
343343
$classBuilder->addUse($output->getFqdn());
344+
$classBuilder->addUse($clientName->getFqdn());
344345

345346
$classBuilder->addMethod($methodName)
346347
->setReturnType('void')
@@ -364,16 +365,18 @@ private function generateClient(Operation $operation): void
364365
private function getResultAssert(StructureShape $shape): string
365366
{
366367
return implode("\n", array_map(function (StructureMember $member) {
368+
$getterMethodName = 'get' . ucfirst(GeneratorHelper::normalizeName($member->getName()));
369+
367370
switch ($member->getShape()->getType()) {
368371
case 'string':
369-
return sprintf('self::assertSame("changeIt", $result->get%s());', $member->getName());
372+
return sprintf('self::assertSame("changeIt", $result->%s());', $getterMethodName);
370373
case 'boolean':
371-
return sprintf('self::assertFalse($result->get%s());', $member->getName());
374+
return sprintf('self::assertFalse($result->%s());', $getterMethodName);
372375
case 'integer':
373376
case 'long':
374-
return sprintf('self::assertSame(1337, $result->get%s());', $member->getName());
377+
return sprintf('self::assertSame(1337, $result->%s());', $getterMethodName);
375378
default:
376-
return sprintf('// self::assertTODO(expected, $result->get%s());', $member->getName());
379+
return sprintf('// self::assertTODO(expected, $result->%s());', $getterMethodName);
377380
}
378381
}, $shape->getMembers()));
379382
}

0 commit comments

Comments
 (0)