Skip to content
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

start typeschema next migration #16

Merged
merged 6 commits into from
Oct 9, 2024
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.1",
"psx/json": "^3.0",
"psx/openapi": "^0.1",
"psx/openapi": "^1.0",
"psx/schema": "^7.0",
"symfony/cache": "^5.0|^6.0|^7.0",
"symfony/console": "^5.0|^6.0|^7.0",
Expand Down
7 changes: 0 additions & 7 deletions src/Attribute/ParamAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ public function __construct(
public string $name,
public Type $type,
public ?string $description = null,
public ?bool $required = null,
public ?array $enum = null,
public ?int $minLength = null,
public ?int $maxLength = null,
public ?string $pattern = null,
public ?Format $format = null,
public ?int $minimum = null,
public ?int $maximum = null
)
{
}
Expand Down
6 changes: 4 additions & 2 deletions src/Builder/SpecificationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use PSX\Api\SpecificationInterface;
use PSX\Schema\ContentType;
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Type\DefinitionTypeAbstract;
use PSX\Schema\Type\PropertyTypeAbstract;
use PSX\Schema\TypeInterface;

/**
Expand Down Expand Up @@ -54,7 +56,7 @@ public function setSecurity(SecurityInterface $security): void
$this->specification->setSecurity($security);
}

public function addOperation(string $operationId, string $method, string $path, int $statusCode, TypeInterface|ContentType $schema): OperationBuilderInterface
public function addOperation(string $operationId, string $method, string $path, int $statusCode, PropertyTypeAbstract|ContentType $schema): OperationBuilderInterface
{
if ($this->specification->getOperations()->has($operationId)) {
throw new OperationAlreadyExistsException('Operation "' . $operationId . '" already exists');
Expand All @@ -71,7 +73,7 @@ public function addDefinitions(DefinitionsInterface $definitions): self
return $this;
}

public function addType(string $name, TypeInterface $schema): self
public function addType(string $name, DefinitionTypeAbstract $schema): self
{
$this->specification->getDefinitions()->addType($name, $schema);
return $this;
Expand Down
7 changes: 4 additions & 3 deletions src/Builder/SpecificationBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use PSX\Api\SpecificationInterface;
use PSX\Schema\ContentType;
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\TypeInterface;
use PSX\Schema\Type\DefinitionTypeAbstract;
use PSX\Schema\Type\PropertyTypeAbstract;

/**
* SpecificationBuilderInterface
Expand All @@ -39,11 +40,11 @@ public function setBaseUrl(string $baseUrl): void;

public function setSecurity(SecurityInterface $security): void;

public function addOperation(string $operationId, string $method, string $path, int $statusCode, TypeInterface|ContentType $schema): OperationBuilderInterface;
public function addOperation(string $operationId, string $method, string $path, int $statusCode, PropertyTypeAbstract|ContentType $schema): OperationBuilderInterface;

public function addDefinitions(DefinitionsInterface $definitions): self;

public function addType(string $name, TypeInterface $schema): self;
public function addType(string $name, DefinitionTypeAbstract $schema): self;

public function getSpecification(): SpecificationInterface;
}
3 changes: 1 addition & 2 deletions src/Generator/Client/LanguageAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ public function generate(SpecificationInterface $specification): Generator\Code\

protected function generateSchema(DefinitionsInterface $definitions, Generator\Code\Chunks $chunks): void
{
$schema = new Schema(TypeFactory::getAny(), $definitions);
$result = $this->generator->generate($schema);
$result = $this->generator->generate(new Schema($definitions, null));

if ($result instanceof Generator\Code\Chunks) {
foreach ($result->getChunks() as $identifier => $code) {
Expand Down
57 changes: 18 additions & 39 deletions src/Generator/Client/LanguageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
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;
Expand Down Expand Up @@ -164,7 +168,7 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
} elseif ($argument->getIn() === ArgumentInterface::IN_QUERY) {
$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) {
if ($argument->getSchema() instanceof ReferencePropertyType) {
$queryStructNames[] = $realName;
}
} elseif ($argument->getIn() === ArgumentInterface::IN_BODY) {
Expand Down Expand Up @@ -259,40 +263,23 @@ private function getOperations(array $operations, DefinitionsInterface $definiti
return $result;
}

private function getInnerSchema(TypeInterface $type, DefinitionsInterface $definitions): ?Dto\Type
private function getInnerSchema(PropertyTypeAbstract $type, DefinitionsInterface $definitions): ?Dto\Type
{
if ($type instanceof MapType) {
$return = $this->newType($type->getAdditionalProperties(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE);
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 ArrayType) {
$return = $this->newType($type->getItems(), false, $definitions, Type\GeneratorInterface::CONTEXT_CLIENT | Type\GeneratorInterface::CONTEXT_RESPONSE);
} 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;
}
}

/**
* @throws InvalidTypeException
* @throws TypeNotFoundException
*/
private function newType(TypeInterface|ContentType $type, bool $optional, DefinitionsInterface $definitions, int $context): Dto\Type
private function newType(PropertyTypeAbstract|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
// some special cases we need to extract the type
$refType = $definitions->getType($type->getRef());
if ($refType instanceof ReferenceType) {
$refType = $definitions->getType($refType->getRef());
}

if (!$refType instanceof StructType && !$refType instanceof MapType && !$refType instanceof AnyType) {
throw new InvalidTypeException('A reference can only point to a struct or map type, got: ' . get_class($refType) . ' for reference: ' . $type->getRef());
}
}

if ($type instanceof ContentType) {
$dataType = $this->typeGenerator->getContentType($type, $context);
$docType = $dataType;
Expand All @@ -311,27 +298,19 @@ private function newType(TypeInterface|ContentType $type, bool $optional, Defini
/**
* @throws GeneratorException
*/
private function resolveImport(TypeInterface $type, array &$imports): void
private function resolveImport(PropertyTypeAbstract $type, array &$imports): void
{
if ($type instanceof ReferenceType) {
$this->buildImport($type->getRef(), $imports);
if ($type instanceof ReferencePropertyType) {
$this->buildImport($type->getTarget(), $imports);
if ($type->getTemplate()) {
foreach ($type->getTemplate() as $typeRef) {
$this->buildImport($typeRef, $imports);
}
}
} elseif ($type instanceof MapType && $type->getAdditionalProperties() instanceof TypeInterface) {
$this->resolveImport($type->getAdditionalProperties(), $imports);
} elseif ($type instanceof ArrayType && $type->getItems() instanceof TypeInterface) {
$this->resolveImport($type->getItems(), $imports);
} elseif ($type instanceof UnionType && $type->getOneOf()) {
foreach ($type->getOneOf() as $item) {
$this->resolveImport($item, $imports);
}
} elseif ($type instanceof IntersectionType) {
foreach ($type->getAllOf() as $item) {
$this->resolveImport($item, $imports);
}
} elseif ($type instanceof MapPropertyType && $type->getSchema() instanceof PropertyTypeAbstract) {
$this->resolveImport($type->getSchema(), $imports);
} elseif ($type instanceof ArrayPropertyType && $type->getSchema() instanceof PropertyTypeAbstract) {
$this->resolveImport($type->getSchema(), $imports);
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/Generator/Client/Util/Naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
use PSX\Api\Exception\InvalidTypeException;
use PSX\Schema\ContentType;
use PSX\Schema\Generator\Normalizer\NormalizerInterface;
use PSX\Schema\Type\ArrayPropertyType;
use PSX\Schema\Type\ArrayType;
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\StringType;
use PSX\Schema\TypeInterface;
Expand Down Expand Up @@ -55,7 +59,10 @@ public function buildMethodNameByTag(string $tagName): string
return $this->normalizer->method($tagName);
}

public function buildExceptionClassNameByType(TypeInterface|ContentType $type): string
/**
* @throws InvalidTypeException
*/
public function buildExceptionClassNameByType(PropertyTypeAbstract|ContentType $type): string
{
if ($type instanceof ContentType) {
return match ($type->getShape()) {
Expand All @@ -66,12 +73,12 @@ public function buildExceptionClassNameByType(TypeInterface|ContentType $type):
ContentType::TEXT => 'TextException',
ContentType::XML => 'XmlException',
};
} elseif ($type instanceof ReferenceType) {
return $this->normalizer->class($type->getRef(), 'Exception');
} elseif ($type instanceof MapType) {
return 'Map' . $this->buildExceptionClassNameByType($type->getAdditionalProperties());
} elseif ($type instanceof ArrayType) {
return 'Array' . $this->buildExceptionClassNameByType($type->getItems());
} elseif ($type instanceof ReferencePropertyType) {
return $this->normalizer->class($type->getTarget(), 'Exception');
} elseif ($type instanceof MapPropertyType) {
return 'Map' . $this->buildExceptionClassNameByType($type->getSchema());
} elseif ($type instanceof ArrayPropertyType) {
return 'Array' . $this->buildExceptionClassNameByType($type->getSchema());
} else {
throw new InvalidTypeException('Provided an invalid type must be reference, map or array type');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Markup/MarkupAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function startLines(Dto\Client $client): array

protected function generateSchema(DefinitionsInterface $definitions): string
{
$schema = new Schema(TypeFactory::getAny(), $definitions);
$schema = new Schema($definitions, null);
$return = $this->generator->generate($schema);

return $return;
Expand Down
22 changes: 12 additions & 10 deletions src/Generator/Server/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use PSX\Schema\ContentType;
use PSX\Schema\Generator;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
use PSX\Schema\Type\PropertyTypeAbstract;
use PSX\Schema\Type\ReferencePropertyType;
use PSX\Schema\Type\ReferenceType;
use PSX\Schema\TypeInterface;

Expand Down Expand Up @@ -119,9 +121,9 @@ protected function generateFooter(File $file): string
return $controller;
}

protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
protected function generateArgumentPath(string $rawName, string $variableName, string $type, PropertyTypeAbstract|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
if ($argumentType instanceof ReferencePropertyType) {
$type = 'Model\\' . $type;
}

Expand All @@ -132,9 +134,9 @@ protected function generateArgumentPath(string $rawName, string $variableName, s
}
}

protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
protected function generateArgumentQuery(string $rawName, string $variableName, string $type, PropertyTypeAbstract|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
if ($argumentType instanceof ReferencePropertyType) {
$type = 'Model\\' . $type;
}

Expand All @@ -145,9 +147,9 @@ protected function generateArgumentQuery(string $rawName, string $variableName,
}
}

protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
protected function generateArgumentHeader(string $rawName, string $variableName, string $type, PropertyTypeAbstract|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
if ($argumentType instanceof ReferencePropertyType) {
$type = 'Model\\' . $type;
}

Expand All @@ -158,18 +160,18 @@ protected function generateArgumentHeader(string $rawName, string $variableName,
}
}

protected function generateArgumentBody(string $variableName, string $type, TypeInterface|ContentType $argumentType): string
protected function generateArgumentBody(string $variableName, string $type, PropertyTypeAbstract|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
if ($argumentType instanceof ReferencePropertyType) {
$type = 'Model\\' . $type;
}

return '#[Body] ' . $type . ' $' . $variableName;
}

protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface|ContentType $returnType): string
protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, PropertyTypeAbstract|ContentType $returnType): string
{
if ($returnType instanceof ReferenceType) {
if ($returnType instanceof ReferencePropertyType) {
$type = 'Model\\' . $type;
}

Expand Down
Loading