Skip to content

Add support for document shapes in the code generator #1915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/CodeGenerator/src/Definition/DocumentShape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace AsyncAws\CodeGenerator\Definition;

/**
* @internal
*/
final class DocumentShape extends Shape
{
}
8 changes: 7 additions & 1 deletion src/CodeGenerator/src/Definition/Shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public static function create(string $name, array $data, \Closure $shapeLocator,
{
switch ($data['type']) {
case 'structure':
$shape = ($data['exception'] ?? false) ? new ExceptionShape() : new StructureShape();
if ($data['exception'] ?? false) {
$shape = new ExceptionShape();
} elseif ($data['document'] ?? false) {
$shape = new DocumentShape();
} else {
$shape = new StructureShape();
}

break;
case 'list':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\CodeGenerator;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Operation;
Expand Down Expand Up @@ -127,6 +128,8 @@ private function generateProperties(StructureShape $shape, ClassBuilder $classBu
}

$nullable = false;
} elseif ($memberShape instanceof DocumentShape) {
$nullable = false; // the type is already nullable, not need to add an extra union
} elseif ($member->isStreaming()) {
$returnType = ResultStream::class;
$parameterType = 'ResultStream';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\CodeGenerator;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Shape;
Expand Down Expand Up @@ -90,6 +91,8 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
} else {
$param = 'array<string, ' . $param . '>';
}
} elseif ($memberShape instanceof DocumentShape) {
$param = 'bool|string|int|float|null|list<mixed>|array<string, mixed>';
} elseif ($member->isStreaming()) {
$param = 'string|resource|(callable(int): string)|iterable<string>';
} elseif ('timestamp' === $param = $memberShape->getType()) {
Expand Down Expand Up @@ -168,6 +171,10 @@ public function getPhpType(Shape $shape): array
return ['array', $doc, $memberClassNames];
}

if ($shape instanceof DocumentShape) {
return ['bool|string|int|float|null|array', 'bool|string|int|float|null|list<mixed>|array<string, mixed>', []];
}

$type = $doc = $this->getNativePhpType($shape->getType());
if (!empty($shape->getEnum())) {
$memberClassNames[] = $memberClassName = $this->namespaceRegistry->getEnum($shape);
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGenerator/src/Generator/InputGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Member;
Expand Down Expand Up @@ -210,6 +211,8 @@ public function generate(Operation $operation): ClassName
// It is a scalar, like a string
$constructorBody .= strtr('$this->PROPERTY = $input["NAME"] ?? null;' . "\n", ['PROPERTY' => GeneratorHelper::normalizeName($member->getName()), 'NAME' => $member->getName()]);
}
} elseif ($memberShape instanceof DocumentShape) {
$getterSetterNullable = false; // The type itself is already nullable
} elseif ($member->isStreaming()) {
$parameterType = 'string|resource|(callable(int): string)|iterable<string>';
$returnType = null;
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGenerator/src/Generator/ObjectGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Shape;
Expand Down Expand Up @@ -307,6 +308,8 @@ private function addProperties(StructureShape $shape, ClassBuilder $classBuilder
$enumClassName = $this->enumGenerator->generate($memberShape);
$classBuilder->addUse($enumClassName->getFqdn());
}
} elseif ($memberShape instanceof DocumentShape) {
$nullable = false;
} elseif ($member->isStreaming()) {
$returnType = ResultStream::class;
$parameterType = ResultStream::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\RequestSerializer;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Member;
Expand Down Expand Up @@ -183,6 +184,8 @@ private function dumpArrayElement(string $output, string $input, string $context
return $this->dumpArrayList($output, $input, $contextProperty, $shape);
case $shape instanceof MapShape:
return $this->dumpArrayMap($output, $input, $contextProperty, $shape);
case $shape instanceof DocumentShape:
throw new \LogicException('Document shapes are not supported in the query serializer for now.');
}

switch ($shape->getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\RequestSerializer;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Member;
Expand Down Expand Up @@ -180,6 +181,8 @@ private function dumpArrayElement(string $output, string $input, string $context
return $this->dumpArrayList($output, $input, $contextProperty, $shape);
case $shape instanceof MapShape:
return $this->dumpArrayMap($output, $input, $contextProperty, $shape);
case $shape instanceof DocumentShape:
return $this->dumpArrayScalar($output, $input, $contextProperty, $shape);
}

switch ($shape->getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\RequestSerializer;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\Member;
use AsyncAws\CodeGenerator\Definition\Operation;
Expand Down Expand Up @@ -158,6 +159,8 @@ private function dumpXmlShape(Member $member, Shape $shape, string $output, stri
return $this->dumpXmlShapeStructure($member, $shape, $output, $input);
case $shape instanceof ListShape:
return $this->dumpXmlShapeList($member, $shape, $output, $input);
case $shape instanceof DocumentShape:
throw new \LogicException('Document shapes are not supported in the XML serializer for now.');
}

switch ($shape->getType()) {
Expand Down
12 changes: 12 additions & 0 deletions src/CodeGenerator/src/Generator/ResponseParser/RestJsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\ResponseParser;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Member;
Expand Down Expand Up @@ -193,6 +194,8 @@ private function parseElement(string $input, Shape $shape, bool $required, bool
return $this->parseResponseStructure($shape, $input, $required);
case $shape instanceof MapShape:
return $this->parseResponseMap($shape, $input, $required, $inObject);
case $shape instanceof DocumentShape:
return $this->parseResponseDocument($input, $required);
}

switch ($shape->getType()) {
Expand Down Expand Up @@ -296,6 +299,15 @@ private function parseResponseBlob(string $input, bool $required): string
return strtr('isset(INPUT) ? base64_decode((string) INPUT) : null', ['INPUT' => $input]);
}

private function parseResponseDocument(string $input, bool $required): string
{
if ($required) {
return strtr('INPUT', ['INPUT' => $input]);
}

return strtr('INPUT ?? null', ['INPUT' => $input]);
}

private function parseResponseList(ListShape $shape, string $input, bool $required, bool $inObject): string
{
$shapeMember = $shape->getMember();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator\ResponseParser;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Member;
Expand Down Expand Up @@ -177,6 +178,8 @@ private function parseXmlElement(string $input, Shape $shape, bool $required, bo
return $this->parseXmlResponseStructure($shape, $input, $required);
case $shape instanceof MapShape:
return $this->parseXmlResponseMap($shape, $input, $required, $inObject);
case $shape instanceof DocumentShape:
throw new \LogicException('Document shapes are not supported in the XML parser for now.');
}

switch ($shape->getType()) {
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGenerator/src/Generator/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AsyncAws\CodeGenerator\Generator;

use AsyncAws\CodeGenerator\Definition\DocumentShape;
use AsyncAws\CodeGenerator\Definition\ListShape;
use AsyncAws\CodeGenerator\Definition\MapShape;
use AsyncAws\CodeGenerator\Definition\Operation;
Expand Down Expand Up @@ -292,6 +293,8 @@ private function getInputCode(ClassBuilder $classBuilder, Shape $shape, bool $in
return strtr('["change me" => INPUT_ARGUMENTS]', [
'INPUT_ARGUMENTS' => $this->getInputCode($classBuilder, $shape->getValue()->getShape(), $includeOptionalParameters, $recursion),
]);
case $shape instanceof DocumentShape:
return '"change me"';
}

switch ($shape->getType()) {
Expand Down