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

feat(#1461): filter JMS properties by route area #2407

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

* Add filtering model properties by route area to `JMSModelDescriber`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add filtering model properties by route area to `JMSModelDescriber`
* Added filtering model properties by route area to `JMSModelDescriber`


## 4.33.5
* Added new optional parameter `$context` to` PropertyDescriberInterface::supports()`

Expand All @@ -25,7 +27,7 @@
## 4.32.3

* Deprecated `Nelmio\ApiDocBundle\Annotation` namespace in favor of `Nelmio\ApiDocBundle\Attribute` namespace in preparation for 5.x. Consider upgrading to the new attribute syntax.
```diff
```diff
- use Nelmio\ApiDocBundle\Annotation\Areas;
- use Nelmio\ApiDocBundle\Annotation\Model;
- use Nelmio\ApiDocBundle\Annotation\Operation;
Expand Down Expand Up @@ -84,7 +86,7 @@ nelmio_api_doc:

## 4.24.0

* Added support for some integer ranges (https://phpstan.org/writing-php-code/phpdoc-types#integer-ranges).
* Added support for some integer ranges (https://phpstan.org/writing-php-code/phpdoc-types#integer-ranges).
Annotations attached to integer properties like:
```php
/**
Expand All @@ -109,9 +111,9 @@ Dropped support for PHP 7.2 and PHP 7.3. PHP 7.4 is the minimum required version
pool: app.cache
item_id: nelmio_api_doc.docs
areas:
default:
default:
...
area1:
area1:
...
```
Result in cache keys: `nelmio_api_doc.docs.default` & `nelmio_api_doc.docs.area1` to be used respectively.
Expand All @@ -124,7 +126,7 @@ Dropped support for PHP 7.2 and PHP 7.3. PHP 7.4 is the minimum required version
pool: app.cache
item_id: nelmio_api_doc.docs.default
...
area1:
area1:
cache:
pool: app.cache
item_id: nelmio_api_doc.docs.area1
Expand Down Expand Up @@ -186,7 +188,7 @@ doc-api:
pool: app.cache
item_id: nelmio_api_doc.docs
```

## 4.20.0

* Added Redocly as an alternative to Swagger UI. https://github.com/Redocly/redoc.
Expand Down
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 @@
}
}

$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 @@ -11,6 +11,7 @@

namespace Nelmio\ApiDocBundle\Render;

use Nelmio\ApiDocBundle\ApiDocGenerator;
use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
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
Loading