Skip to content

Commit

Permalink
provide type context
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Sep 15, 2024
1 parent f344f40 commit c5bf824
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/Generator/Client/LanguageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Generator/Server/ServerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit c5bf824

Please sign in to comment.