Skip to content

Commit

Permalink
feat: filter JMS properties by route area
Browse files Browse the repository at this point in the history
  • Loading branch information
jneidel committed Dec 9, 2024
1 parent 9411e9c commit a2422e1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/ApiDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ final class ApiDocGenerator
*/
private $openApiVersion;

private ?string $area = null;

private Generator $generator;

/**
Expand Down Expand Up @@ -86,6 +88,11 @@ public function setOpenApiVersion(?string $openApiVersion): void
$this->openApiVersion = $openApiVersion;
}

public function setArea(string $area): void
{
$this->area = $area;
}

public function generate(): OpenApi
{
if (null !== $this->openApi) {
Expand All @@ -108,7 +115,7 @@ public function generate(): OpenApi
$context = Util::createContext(['version' => $this->generator->getVersion()]);

$this->openApi = new OpenApi(['_context' => $context]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames, $this->area);
if (null !== $this->logger) {
$modelRegistry->setLogger($this->logger);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class Model
*/
private array $serializationContext;

private ?string $area = null;

/**
* @param string[]|null $groups
* @param mixed[]|null $options
Expand Down Expand Up @@ -73,6 +75,16 @@ public function getHash(): string
return md5(serialize([$this->type, $this->getSerializationContext()]));
}

public function setArea(?string $area): void
{
$this->area = $area;
}

public function getArea(): ?string
{
return $this->area;
}

/**
* @return mixed[]
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ final class ModelRegistry
*/
private iterable $modelDescribers;

private ?string $area;

private OA\OpenApi $api;

/**
Expand All @@ -61,10 +63,11 @@ final class ModelRegistry
*
* @internal
*/
public function __construct($modelDescribers, OA\OpenApi $api, array $alternativeNames = [])
public function __construct($modelDescribers, OA\OpenApi $api, array $alternativeNames = [], ?string $area = null)
{
$this->modelDescribers = $modelDescribers;
$this->api = $api;
$this->area = $area;
$this->logger = new NullLogger();
foreach (array_reverse($alternativeNames) as $alternativeName => $criteria) {
$this->alternativeNames[] = $model = new Model(
Expand All @@ -81,6 +84,7 @@ public function __construct($modelDescribers, OA\OpenApi $api, array $alternativ

public function register(Model $model): string
{
$model->setArea($this->area);
$hash = $model->getHash();
if (!isset($this->models[$hash])) {
$this->models[$hash] = $model;
Expand Down
6 changes: 6 additions & 0 deletions src/ModelDescriber/JMSModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use JMS\Serializer\Context;
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
use JMS\Serializer\Exclusion\VersionExclusionStrategy;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
use JMS\Serializer\SerializationContext;
Expand Down Expand Up @@ -232,6 +233,11 @@ public function getSerializationContext(Model $model): Context
}
}

$area = $model->getArea();
if ( null !== $area ) {
$context->addExclusionStrategy(new VersionExclusionStrategy($area));

Check warning on line 238 in src/ModelDescriber/JMSModelDescriber.php

View check run for this annotation

Codecov / codecov/patch

src/ModelDescriber/JMSModelDescriber.php#L238

Added line #L238 was not covered by tests
}

return $context;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Render/RenderOpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OpenApi\Generator;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\ApiDocGenerator;

class RenderOpenApi
{
Expand Down Expand Up @@ -81,8 +82,14 @@ public function render(string $format, string $area, array $options = []): strin
throw new RenderInvalidArgumentException(sprintf('Format "%s" is not supported.', $format));
}

$generator = $this->generatorLocator->get($area);

if ($generator instanceof ApiDocGenerator) {
$generator->setArea($area);
}

/** @var OpenApi $spec */
$spec = $this->generatorLocator->get($area)->generate();
$spec = $generator->generate();
$tmpServers = $spec->servers;
try {
$spec->servers = $this->getServersFromOptions($spec, $options);
Expand Down

0 comments on commit a2422e1

Please sign in to comment.