Skip to content
Snippets Groups Projects
Commit e1c580eb authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

chore: openapi reusable schemas

parent dd7421c1
No related branches found
No related tags found
No related merge requests found
......@@ -30,10 +30,6 @@ readonly class GivesHttpResponseExtractor extends OpenAPIMetadataResponseExtract
description: $attribute->description,
status: $attribute->status,
jsonSchema: $attribute->jsonSchema,
jsonSchemaName: sprintf(
'Response_%s',
str_replace('\\', '', $reflectionClass->getName()),
),
),
];
}
......
......@@ -5,46 +5,47 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
use Ds\Map;
use Ds\Set;
use LogicException;
readonly class OpenAPIReusableSchemaCollection
{
/**
* @var Map<JsonSchema,string>
* @var Map<string,string>
*/
public Map $references;
public Map $hashes;
/**
* @var Set<string>
* @var Map<JsonSchema,string>
*/
private Set $jsonSchemaNames;
public Map $references;
public function __construct()
{
$this->jsonSchemaNames = new Set();
$this->hashes = new Map();
$this->references = new Map();
}
public function reuse(JsonSchema $jsonSchema, string $jsonSchemaName): JsonSchema
public function reuse(JsonSchema $jsonSchema): JsonSchema
{
if (!$this->references->hasKey($jsonSchema)) {
if ($this->jsonSchemaNames->contains($jsonSchemaName)) {
throw new LogicException(sprintf(
'Non-unique json schema name: "%s"',
$jsonSchemaName,
));
}
$this->jsonSchemaNames->add($jsonSchemaName);
$this->references->put($jsonSchema, $jsonSchemaName);
$hashed = $this->makeHash($jsonSchema);
if (!$this->hashes->hasKey($hashed)) {
$this->hashes->put($hashed, uniqid());
}
$schemaId = $this->hashes->get($hashed);
$this->references->put($jsonSchema, $schemaId);
return new JsonSchema([
'$ref' => sprintf(
'#/components/schemas/%s',
$this->references->get($jsonSchema),
$schemaId,
),
]);
}
private function makeHash(JsonSchema $jsonSchema): string
{
return serialize($jsonSchema->schema);
}
}
......@@ -37,10 +37,6 @@ readonly class ValidatedRequestExtractor extends OpenAPIRouteRequestBodyContentE
->inputValidators
->get($attribute->validator)
->jsonSchema,
jsonSchemaName: sprintf(
'Request_%s',
str_replace('\\', '', $attribute->validator),
),
),
];
}
......
......@@ -7,7 +7,6 @@ namespace Distantmagic\Resonance;
readonly class OpenAPISchemaRequestBodyContent implements OpenAPISerializableFieldInterface
{
public function __construct(
public string $jsonSchemaName,
public string $mimeType,
public JsonSchema $jsonSchema,
) {}
......@@ -15,7 +14,7 @@ readonly class OpenAPISchemaRequestBodyContent implements OpenAPISerializableFie
public function toArray(OpenAPIReusableSchemaCollection $openAPIReusableSchemaCollection): array
{
return [
'schema' => $openAPIReusableSchemaCollection->reuse($this->jsonSchema, $this->jsonSchemaName),
'schema' => $openAPIReusableSchemaCollection->reuse($this->jsonSchema),
];
}
}
......@@ -9,7 +9,6 @@ readonly class OpenAPISchemaResponse implements OpenAPISerializableFieldInterfac
public function __construct(
public ContentType $contentType,
public JsonSchema $jsonSchema,
public string $jsonSchemaName,
public int $status,
public ?string $description = null,
) {}
......@@ -24,7 +23,7 @@ readonly class OpenAPISchemaResponse implements OpenAPISerializableFieldInterfac
$response['content'] = [
$this->contentType->value => [
'schema' => $openAPIReusableSchemaCollection->reuse($this->jsonSchema, $this->jsonSchemaName),
'schema' => $openAPIReusableSchemaCollection->reuse($this->jsonSchema),
],
];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment