-
-
Notifications
You must be signed in to change notification settings - Fork 865
Add areas support #1169
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
Add areas support #1169
Conversation
a58314a
to
85fa9f2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very interesting!
can you elaborate the documentation a bit, to add some more examples what exactly this does?
for reviewing it would be easier to split this up into at least the code cleanups and the actual feature. ideally, even the thing about refactoring how tagged services are integrated would have been separate, but that would now be a large effort to separate.
composer.json
Outdated
@@ -16,27 +16,26 @@ | |||
], | |||
"require": { | |||
"php": "^7.0", | |||
"symfony/framework-bundle": "^3.2.5|^4.0", | |||
"symfony/property-info": "^3.1|^4.0", | |||
"symfony/framework-bundle": "^3.4|^4.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would do this cleanup in a separate pull request that does not add new features.
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; | ||
|
||
use JMS\Serializer\Annotation as Serializer; | ||
|
||
/** | ||
* Class JMSComplex | ||
* @package Nelmio\ApiDocBundle\Tests\Functional\Entity | ||
* Class JMSComplex. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not tell anything the code does not already say. i'd remove the text line completely. (and these things part of the cleanup pull request rather than the new feature.)
]) | ||
->addTag(sprintf('nelmio_api_doc.describer.%s', $area), ['priority' => -400]); | ||
|
||
$container->register('nelmio_api_doc.describers.swagger_php', SwaggerPhpDescriber::class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find it an antipattern to define services in the DI extension instead of in the services xml files. its much harder to read what is going on imho. i would rather have additional services xml files for specific features that are only loaded if that feature is to be enabled. but i guess its a question of taste as well...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to agree but these services are dynamically generated and we can't put them in a file unfortunately... Maybe using the php syntax would be clearer though (see symfony/symfony#23834).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its ok, i can live with it as is, if we can't move it to the xml :-)
->children() | ||
->arrayNode('documentation') | ||
->useAttributeAsKey('key') | ||
->info('The documentation used as base') | ||
->example(['info' => ['title' => 'My App']]) | ||
->prototype('variable')->end() | ||
->end() | ||
->arrayNode('routes') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will need to be documented in the changelog
}) | ||
->then(function ($v) { | ||
$v['areas'] = $v['routes']; | ||
unset($v['routes']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should also trigger a deprecation warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
@@ -21,20 +21,49 @@ public function getConfigTreeBuilder() | |||
$treeBuilder = new TreeBuilder(); | |||
$treeBuilder | |||
->root('nelmio_api_doc') | |||
->beforeNormalization() | |||
->ifTrue(function ($v) { | |||
return !isset($v['areas']) && isset($v['routes']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if both areas and routes are set, we should throw an invalid configuration exception explaining to use only the new areas
section. otherwise the user will just see an error that routes is not known, which will be confusing when before, routes did work.
README.md
Outdated
@@ -77,9 +77,9 @@ routes that are documented by configuring the bundle: | |||
```yml | |||
# app/config/config.yml | |||
nelmio_api_doc: | |||
routes: | |||
areas: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the areas model allow to specify other criteria than path patterns? and how to specify more than one distinct area?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the areas model allow to specify other criteria than path patterns?
For now it's only based on path patterns but we could easily add more criteria if we want to, I'd rather do this later though to not complicate this PR. The best imo would be to allow to add a custom routes filter.
and how to specify more than one distinct area?
You can define as many areas as you want:
nelmio_api_doc:
areas:
default: [ path_patterns: [ ^/api ] ]
internal: [ path_patterns: [ ^/internal ] ]
commercial: [ path_patterns: [ ^/commercial ] ]
# ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a doc section for configuring several areas? so if you specify what we have here, you implicitly create a default area, and if you would explicitly create a list of areas, you get several?
47b6464
to
f72ce77
Compare
I added tests and docs. Much bigger diff :/ |
a67d6e1
to
e3d787f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a features is also its test and doc, and this is no tiny thing ;-) great job!
i think there is something unfinished with the cache (that seems to be not tested if travis-ci is green) but otherwise looks good to me!
ApiDocGenerator.php
Outdated
@@ -46,7 +46,7 @@ public function generate(): Swagger | |||
} | |||
|
|||
if ($this->cacheItemPool) { | |||
$item = $this->cacheItemPool->getItem('swagger_doc'); | |||
$item = $this->cacheItemPool->getItem($cacheItemId ?? 'swagger_doc'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this will never do anything. ?? is like isset, not telling you that the variable does not exist. but cacheItemId will never exist in this context unless i am missing something. did you mean to store the constructor parameter andin the class and use $this->cacheItemId?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I added tests to make sure it works. Will have to use it one day to have better perfs...
if (!$generatorLocator instanceof ContainerInterface) { | ||
@trigger_error(sprintf('Providing an instance of "%s" to "%s()" is deprecated since version 3.1. Provide it an instance of "%s" instead.', ApiDocGenerator::class, __METHOD__, ContainerInterface::class), E_USER_DEPRECATED); | ||
$generatorLocator = new ServiceLocator(['default' => function () use ($generatorLocator) { | ||
return $generatorLocator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will only work if $generatorLocator is the ApiDocGenerator, right? i would typehint both and explain that the generator is still supported for BC. and do a typecheck before the deprecation to fail loudly if its not something we can work with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type hint added :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great! looks good now, feel free to squash and merge.
Thanks for all your reviews! :) |
Should fix #1105 (review).