Skip to content

Commit

Permalink
use schema source type
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 11, 2024
1 parent 68263f4 commit f237200
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/Generator/Client/Dto/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public int $code,
public Type $schema,
public ?string $className = null,
public ?Type $innerSchema = null,
public ?bool $isMapOrArray = null,
public ?string $contentType = null,
public ?string $contentShape = null,
)
Expand Down
8 changes: 1 addition & 7 deletions src/Generator/Client/Language/php-operation.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@
{% elseif payload.contentShape == 'text/plain' or payload.contentShape == 'application/xml' %}
{% if in_throw %} {% endif %}$data = (string) $body;
{% else %}
{% if payload.innerSchema.isMap %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isMap: true);
{% elseif payload.innerSchema.isArray %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isArray: true);
{% else %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.schema.type }}::class);
{% endif %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {% if payload.isMapOrArray %}\PSX\Schema\SchemaSource::fromType('{{ payload.schema.docType|raw }}', __NAMESPACE__){% else %}\PSX\Schema\SchemaSource::fromClass({{ payload.schema.type|raw }}::class){% endif %});
{% endif %}
{% endmacro %}
1 change: 0 additions & 1 deletion src/Generator/Client/LanguageAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use PSX\Schema\Generator;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
use PSX\Schema\Schema;
use PSX\Schema\TypeFactory;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
Expand Down
30 changes: 3 additions & 27 deletions src/Generator/Client/LanguageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@
use PSX\Schema\Generator\Type\GeneratorInterface as TypeGeneratorInterface;
use PSX\Schema\Generator\TypeAwareInterface;
use PSX\Schema\GeneratorInterface;
use PSX\Schema\Type\AnyType;
use PSX\Schema\Type\ArrayPropertyType;
use PSX\Schema\Type\ArrayType;
use PSX\Schema\Type\IntersectionType;
use PSX\Schema\Type\MapPropertyType;
use PSX\Schema\Type\MapType;
use PSX\Schema\Type\PropertyTypeAbstract;
use PSX\Schema\Type\ReferencePropertyType;
use PSX\Schema\Type\ReferenceType;
use PSX\Schema\Type\StructType;
use PSX\Schema\Type\UnionType;
use PSX\Schema\TypeInterface;
use PSX\Schema\TypeUtil;

Expand Down Expand Up @@ -199,13 +192,12 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
if (in_array($operation->getReturn()->getCode(), [200, 201, 202])) {
$returnSchema = $operation->getReturn()->getSchema();
$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 MapPropertyType || $returnSchema instanceof ArrayPropertyType,
$returnSchema instanceof ContentType ? $returnSchema->toString() : null,
$returnSchema instanceof ContentType ? $returnSchema->getShape() : null,
);
Expand All @@ -220,12 +212,11 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
$throwSchema = $throw->getSchema();

$exceptionImports = [];
if ($throwSchema instanceof TypeInterface) {
if ($throwSchema instanceof PropertyTypeAbstract) {
$this->resolveImport($throwSchema, $exceptionImports);
}

$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);
$exceptions[$exceptionClassName] = new Dto\Exception($exceptionClassName, $exceptionType, 'The server returned an error', $exceptionImports);
Expand All @@ -234,7 +225,7 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
$throw->getCode(),
$exceptionType,
$exceptionClassName,
$innerSchema,
$throwSchema instanceof MapPropertyType || $throwSchema instanceof ArrayPropertyType,
$throwSchema instanceof ContentType ? $throwSchema->toString() : null,
$throwSchema instanceof ContentType ? $throwSchema->getShape() : null,
);
Expand Down Expand Up @@ -263,21 +254,6 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
return $result;
}

private function getInnerSchema(PropertyTypeAbstract $type, DefinitionsInterface $definitions): ?Dto\Type
{
if ($type instanceof MapPropertyType) {
$return = $this->newType($type->getSchema(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE);
$return->isMap = true;
return $return;
} elseif ($type instanceof ArrayPropertyType) {
$return = $this->newType($type->getSchema(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE);
$return->isArray = true;
return $return;
} else {
return null;
}
}

private function newType(PropertyTypeAbstract|ContentType $type, bool $optional, DefinitionsInterface $definitions, int $context): Dto\Type
{
if ($type instanceof ContentType) {
Expand Down
20 changes: 10 additions & 10 deletions tests/Generator/Client/resource/php/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function get(string $name, string $type, ?int $startIndex = null, ?float
$response = $this->httpClient->request('GET', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryCollection::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryCollection::class));

return $data;
} catch (ClientException $e) {
Expand Down Expand Up @@ -100,7 +100,7 @@ public function create(string $name, string $type, EntryCreate $payload): EntryM
$response = $this->httpClient->request('POST', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

return $data;
} catch (ClientException $e) {
Expand All @@ -110,13 +110,13 @@ public function create(string $name, string $type, EntryCreate $payload): EntryM
$statusCode = $e->getResponse()->getStatusCode();

if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}

if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public function update(string $name, string $type, \PSX\Record\Record $payload):
$response = $this->httpClient->request('PUT', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromType('\PSX\Record\Record<EntryMessage>', __NAMESPACE__));

return $data;
} catch (ClientException $e) {
Expand All @@ -167,13 +167,13 @@ public function update(string $name, string $type, \PSX\Record\Record $payload):
$statusCode = $e->getResponse()->getStatusCode();

if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}

if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromType('\PSX\Record\Record<EntryMessage>', __NAMESPACE__));

throw new MapEntryMessageException($data);
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public function patch(string $name, string $type, array $payload): array
$response = $this->httpClient->request('PATCH', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromType('array<EntryMessage>', __NAMESPACE__));

return $data;
} catch (ClientException $e) {
Expand All @@ -261,13 +261,13 @@ public function patch(string $name, string $type, array $payload): array
$statusCode = $e->getResponse()->getStatusCode();

if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}

if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromType('array<EntryMessage>', __NAMESPACE__));

throw new ArrayEntryMessageException($data);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/Client/resource/php_collection/BarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function find(string $foo): EntryCollection
$response = $this->httpClient->request('GET', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryCollection::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryCollection::class));

return $data;
} catch (ClientException $e) {
Expand Down Expand Up @@ -78,7 +78,7 @@ public function put(EntryCreate $payload): EntryMessage
$response = $this->httpClient->request('POST', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

return $data;
} catch (ClientException $e) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Generator/Client/resource/php_collection/FooBarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get(): EntryCollection
$response = $this->httpClient->request('GET', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryCollection::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryCollection::class));

return $data;
} catch (ClientException $e) {
Expand Down Expand Up @@ -77,7 +77,7 @@ public function create(EntryCreate $payload): EntryMessage
$response = $this->httpClient->request('POST', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

return $data;
} catch (ClientException $e) {
Expand All @@ -87,13 +87,13 @@ public function create(EntryCreate $payload): EntryMessage
$statusCode = $e->getResponse()->getStatusCode();

if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}

if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

throw new EntryMessageException($data);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/Client/resource/php_collection/FooBazTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function get(string $year): EntryCollection
$response = $this->httpClient->request('GET', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryCollection::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryCollection::class));

return $data;
} catch (ClientException $e) {
Expand Down Expand Up @@ -78,7 +78,7 @@ public function create(EntryCreate $payload): EntryMessage
$response = $this->httpClient->request('POST', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, EntryMessage::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(EntryMessage::class));

return $data;
} catch (ClientException $e) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/Client/resource/php_import/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function foo(): \External\Bar\MyType
$response = $this->httpClient->request('GET', $url, $options);
$body = $response->getBody();

$data = $this->parser->parse((string) $body, \External\Bar\MyType::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(\External\Bar\MyType::class));

return $data;
} catch (ClientException $e) {
Expand All @@ -49,7 +49,7 @@ public function foo(): \External\Bar\MyType
$statusCode = $e->getResponse()->getStatusCode();

if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, \External\Bar\MyType::class);
$data = $this->parser->parse((string) $body, \PSX\Schema\SchemaSource::fromClass(\External\Bar\MyType::class));

throw new ImportMyTypeException($data);
}
Expand Down
Loading

0 comments on commit f237200

Please sign in to comment.