Skip to content

Commit

Permalink
update typeschema (#43)
Browse files Browse the repository at this point in the history
* update typeschema

* catch fitting exception
  • Loading branch information
chriskapp authored Nov 2, 2024
1 parent 2f45b73 commit 1d2ba28
Show file tree
Hide file tree
Showing 19 changed files with 3,073 additions and 2,008 deletions.
10 changes: 5 additions & 5 deletions src/Provider/Generator/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function setup(SetupInterface $setup, ParametersInterface $configuration)
$generator = new Generator\TypeSchema();
$definitions = $specification->getDefinitions();

foreach ($definitions->getTypes(DefinitionsInterface::SELF_NAMESPACE) as $name => $type) {
$schema = new Schema(TypeFactory::getReference($name), clone $definitions);
foreach ($definitions->getTypes() as $name => $type) {
$schema = new Schema(clone $definitions, $name);
(new SchemaResolver())->resolve($schema);

$result = (string) $generator->generate($schema);
Expand Down Expand Up @@ -251,9 +251,9 @@ private function getThrows(OperationInterface $operation): ?OperationThrows

private function getRef(TypeInterface|ContentType $schema): string
{
if ($schema instanceof Type\ReferenceType) {
return $schema->getRef() ?? throw new \RuntimeException('No ref provided');
} elseif ($schema instanceof Type\AnyType) {
if ($schema instanceof Type\ReferencePropertyType) {
return $schema->getTarget() ?? throw new \RuntimeException('No ref provided');
} elseif ($schema instanceof Type\AnyPropertyType) {
return SchemaName::PASSTHRU;
} elseif ($schema instanceof ContentType) {
return SchemaName::PASSTHRU;
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Generator/EntityCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function createSchemas(array $schemas, string $prefix, UserContext $conte
$record->setName($this->buildName($prefix, $record->getName() ?? ''));

$source = $record->getSource();
$import = $source?->get('$import');
$import = $source?->get('import');
if (is_iterable($import) || $import instanceof \stdClass) {
$result = [];
foreach ($import as $name => $schema) {
Expand All @@ -72,7 +72,7 @@ public function createSchemas(array $schemas, string $prefix, UserContext $conte
$result[$name] = $schema;
}
}
$source->put('$import', $result);
$source->put('import', $result);
}

$existing = $this->schemaTable->findOneByName($record->getName() ?? '');
Expand Down
19 changes: 9 additions & 10 deletions src/Service/Operation/SpecificationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
use PSX\Schema\Parser\TypeSchema;
use PSX\Schema\SchemaInterface;
use PSX\Schema\SchemaManagerInterface;
use PSX\Schema\Type\ReferenceType;
use PSX\Schema\TypeFactory;
use PSX\Schema\Type\Factory\PropertyTypeFactory;

/**
* Service which builds a specification based on the schemas defined at the database
Expand Down Expand Up @@ -113,7 +112,7 @@ private function getReturn(Table\Generated\OperationRow $row, DefinitionsInterfa

$definitions->addSchema($name, $schema);

return new Operation\Response($row->getHttpCode(), TypeFactory::getReference($name));
return new Operation\Response($row->getHttpCode(), PropertyTypeFactory::getReference($name));
}

private function getArguments(Table\Generated\OperationRow $row, DefinitionsInterface $definitions): Operation\Arguments
Expand All @@ -129,7 +128,7 @@ private function getArguments(Table\Generated\OperationRow $row, DefinitionsInte

$definitions->addSchema($name, $schema);

$arguments->add('payload', new Operation\Argument(ArgumentInterface::IN_BODY, TypeFactory::getReference($name)));
$arguments->add('payload', new Operation\Argument(ArgumentInterface::IN_BODY, PropertyTypeFactory::getReference($name)));
}

$rawParameters = $row->getParameters();
Expand Down Expand Up @@ -157,7 +156,7 @@ private function getThrows(Table\Generated\OperationRow $row, DefinitionsInterfa

$definitions->addSchema($name, $schema);

$result[] = new Operation\Response($httpCode, TypeFactory::getReference($name));
$result[] = new Operation\Response($httpCode, PropertyTypeFactory::getReference($name));
}

return $result;
Expand All @@ -183,7 +182,7 @@ private function buildPathParameters(Operation\Arguments $arguments, string $pat
}

if ($name !== null) {
$arguments->add($name, new Operation\Argument(ArgumentInterface::IN_PATH, TypeFactory::getString()));
$arguments->add($name, new Operation\Argument(ArgumentInterface::IN_PATH, PropertyTypeFactory::getString()));
}
}
}
Expand All @@ -196,15 +195,15 @@ private function buildQueryParametersFromJson(Operation\Arguments $arguments, \s
continue;
}

$arguments->add($name, new Operation\Argument(ArgumentInterface::IN_QUERY, $this->schemaParser->parseType($schema)));
$arguments->add($name, new Operation\Argument(ArgumentInterface::IN_QUERY, $this->schemaParser->parsePropertyType($schema)));
}
}

private function getNameForSchema(string $source, SchemaInterface $schema): string
{
$root = $schema->getType();
if ($root instanceof ReferenceType) {
return $root->getRef() ?? throw new \RuntimeException('Provided an invalid ref');
$root = $schema->getRoot();
if (!empty($root)) {
return $root;
}

$pos = strpos($source, '://');
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function update(string $rateId, RateUpdate $rate, UserContext $context):
$existing->setPriority($rate->getPriority() ?? $existing->getPriority());
$existing->setName($rate->getName() ?? $existing->getName());
$existing->setRateLimit($rate->getRateLimit() ?? $existing->getRateLimit());
$existing->setTimespan((string) ($rate->getTimespan() ?? $existing->getTimespan()));
$existing->setTimespan($rate->getTimespan() ?? $existing->getTimespan());
$existing->setMetadata($rate->getMetadata() !== null ? json_encode($rate->getMetadata()) : $existing->getMetadata());
$this->rateTable->update($existing);

Expand Down
4 changes: 2 additions & 2 deletions src/Service/Test/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use PSX\Http\ResponseInterface;
use PSX\Http\Stream\Stream;
use PSX\OAuth2\AccessToken;
use PSX\Schema\Exception\ValidationException;
use PSX\Schema\Exception\TraverserException;
use PSX\Schema\SchemaManagerInterface;
use PSX\Schema\SchemaTraverser;
use PSX\Uri\Uri;
Expand Down Expand Up @@ -138,7 +138,7 @@ public function run(Table\Generated\TestRow $test, Table\Generated\OperationRow
(new SchemaTraverser(ignoreUnknown: false))->traverse($data, $schema);

$this->set($test, Table\Test::STATUS_SUCCESS, '', $body);
} catch (ValidationException $e) {
} catch (TraverserException $e) {
$this->set($test, Table\Test::STATUS_ERROR, $e->getMessage(), $body);
} catch (\Throwable $e) {
$this->set($test, Table\Test::STATUS_ERROR, $this->getErrorMessage($e), $body);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/User/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function resetPassword(UserEmail $reset, UserContext $context): void

$email = $reset->getEmail();
if (empty($email)) {
throw new StatusCode\NotFoundException('No email was provided');
throw new StatusCode\BadRequestException('No email was provided');
}

$user = $this->userTable->findOneByTenantAndEmail($context->getTenantId(), $email);
Expand Down
21 changes: 10 additions & 11 deletions tests/Backend/Api/Generator/resource/changelog_sqldatabase.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
{
"name": "News_SQL_GetAll",
"source": {
"$import": {
"import": {
"entity": "schema:\/\/News_SQL_Get"
},
"definitions": {
"App_News_Collection": {
"type": "object",
"type": "struct",
"properties": {
"totalResults": {
"type": "integer"
Expand All @@ -21,33 +21,32 @@
},
"entry": {
"type": "array",
"items": {
"$ref": "entity:App_News"
"schema": {
"type": "reference",
"target": "entity:App_News"
}
}
}
}
},
"$ref": "App_News_Collection"
"root": "App_News_Collection"
}
},
{
"name": "News_SQL_Get",
"source": {
"definitions": {
"App_News": {
"type": "object",
"type": "struct",
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "string",
"maxLength": 64
"type": "string"
},
"content": {
"type": "string",
"maxLength": 255
"type": "string"
},
"date": {
"type": "string",
Expand All @@ -56,7 +55,7 @@
}
}
},
"$ref": "App_News"
"root": "App_News"
}
}
],
Expand Down
Loading

0 comments on commit 1d2ba28

Please sign in to comment.