diff --git a/src/Generator/Client/LanguageBuilder.php b/src/Generator/Client/LanguageBuilder.php index 24074e7..12fe12f 100644 --- a/src/Generator/Client/LanguageBuilder.php +++ b/src/Generator/Client/LanguageBuilder.php @@ -20,6 +20,7 @@ namespace PSX\Api\Generator\Client; +use PSX\Schema\Generator\Type; use PSX\Api\Exception\InvalidTypeException; use PSX\Api\Generator\Client\Util\Naming; use PSX\Api\Operation\ArgumentInterface; @@ -153,16 +154,16 @@ private function getOperations(array $operations, DefinitionsInterface $definiti $normalized = $this->normalizer->argument($name); if ($argument->getIn() === ArgumentInterface::IN_PATH) { - $path[$normalized] = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), false, $definitions)); + $path[$normalized] = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_REQUEST)); $pathNames[$normalized] = $realName; } elseif ($argument->getIn() === ArgumentInterface::IN_QUERY) { - $query[$normalized] = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), true, $definitions)); + $query[$normalized] = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), true, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_REQUEST)); $queryNames[$normalized] = $realName; if ($argument->getSchema() instanceof ReferenceType) { $queryStructNames[] = $realName; } } elseif ($argument->getIn() === ArgumentInterface::IN_BODY) { - $body = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), false, $definitions)); + $body = new Dto\Argument($argument->getIn(), $this->newType($argument->getSchema(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_REQUEST)); $bodyName = $normalized; if ($argument->getSchema() instanceof ContentType) { @@ -186,7 +187,7 @@ private function getOperations(array $operations, DefinitionsInterface $definiti $return = null; if (in_array($operation->getReturn()->getCode(), [200, 201, 202])) { $returnSchema = $operation->getReturn()->getSchema(); - $returnType = $this->newType($returnSchema, false, $definitions); + $returnType = $this->newType($returnSchema, false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE); $innerSchema = $returnSchema instanceof TypeInterface ? $this->getInnerSchema($returnSchema, $definitions) : null; $return = new Dto\Response($operation->getReturn()->getCode(), $returnType, null, $innerSchema, $returnSchema instanceof ContentType ? $returnSchema->value : null); @@ -205,7 +206,7 @@ private function getOperations(array $operations, DefinitionsInterface $definiti $this->resolveImport($throwSchema, $exceptionImports); } - $exceptionType = $this->newType($throwSchema, false, $definitions); + $exceptionType = $this->newType($throwSchema, false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE); $innerSchema = $throwSchema instanceof TypeInterface ? $this->getInnerSchema($throwSchema, $definitions) : null; $exceptionClassName = $this->naming->buildExceptionClassNameByType($throwSchema); @@ -239,11 +240,11 @@ private function getOperations(array $operations, DefinitionsInterface $definiti private function getInnerSchema(TypeInterface $type, DefinitionsInterface $definitions): ?Dto\Type { if ($type instanceof MapType) { - $return = $this->newType($type->getAdditionalProperties(), false, $definitions); + $return = $this->newType($type->getAdditionalProperties(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE); $return->isMap = true; return $return; } elseif ($type instanceof ArrayType) { - $return = $this->newType($type->getItems(), false, $definitions); + $return = $this->newType($type->getItems(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE); $return->isArray = true; return $return; } else { @@ -255,7 +256,7 @@ private function getInnerSchema(TypeInterface $type, DefinitionsInterface $defin * @throws InvalidTypeException * @throws TypeNotFoundException */ - private function newType(TypeInterface|ContentType $type, bool $optional, DefinitionsInterface $definitions): Dto\Type + private function newType(TypeInterface|ContentType $type, bool $optional, DefinitionsInterface $definitions, int $context): Dto\Type { if ($type instanceof ReferenceType) { // in case we have a reference type we take a look at the reference, normally this is a struct type but in @@ -271,7 +272,7 @@ private function newType(TypeInterface|ContentType $type, bool $optional, Defini } if ($type instanceof ContentType) { - $dataType = $this->typeGenerator->getContentType($type); + $dataType = $this->typeGenerator->getContentType($type, $context); $docType = $dataType; } else { $dataType = $this->typeGenerator->getType($type); diff --git a/src/Generator/Server/ServerAbstract.php b/src/Generator/Server/ServerAbstract.php index f78c5f7..47b1443 100644 --- a/src/Generator/Server/ServerAbstract.php +++ b/src/Generator/Server/ServerAbstract.php @@ -185,7 +185,7 @@ protected function buildFolderStructure(SpecificationInterface $specification): * @throws InvalidTypeException * @throws TypeNotFoundException */ - protected function newType(TypeInterface|ContentType $type, DefinitionsInterface $definitions): Dto\Type + protected function newType(TypeInterface|ContentType $type, DefinitionsInterface $definitions, int $context): Dto\Type { if ($type instanceof ReferenceType) { // in case we have a reference type we take a look at the reference, normally this is a struct type but in @@ -201,7 +201,7 @@ protected function newType(TypeInterface|ContentType $type, DefinitionsInterface } if ($type instanceof ContentType) { - $dataType = $this->typeGenerator->getContentType($type); + $dataType = $this->typeGenerator->getContentType($type, $context); $docType = $dataType; } else { $dataType = $this->typeGenerator->getType($type); @@ -239,7 +239,7 @@ private function generateControllerFile(File $file, SpecificationInterface $spec $rawName = $argumentName; $variableName = $this->normalizer->argument($argumentName); $argumentType = $argument->getSchema(); - $type = $this->newType($argumentType, $specification->getDefinitions()); + $type = $this->newType($argumentType, $specification->getDefinitions(), Type\GeneratorInterface::CONTEXT_SERVER | Type\GeneratorInterface::CONTEXT_REQUEST); if ($argument->getIn() === ArgumentInterface::IN_PATH) { $args[] = $this->generateArgumentPath($rawName, $variableName, $type->type, $argumentType); @@ -257,7 +257,7 @@ private function generateControllerFile(File $file, SpecificationInterface $spec } $returnType = $operation->getReturn()->getSchema(); - $type = $this->newType($returnType, $specification->getDefinitions()); + $type = $this->newType($returnType, $specification->getDefinitions(), Type\GeneratorInterface::CONTEXT_SERVER | Type\GeneratorInterface::CONTEXT_RESPONSE); if ($returnType instanceof TypeInterface) { $this->resolveImport($returnType, $imports);