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(#2212): support type info component #2349

Merged
merged 83 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
915ae64
Add type-info
DjordyKoert Oct 4, 2024
99c86c4
Implement experimental type-info describers
DjordyKoert Oct 4, 2024
95dfbe9
Register services for experimental type-info describers
DjordyKoert Oct 4, 2024
4c68692
Also check instanceof type
DjordyKoert Oct 4, 2024
58ddc64
Implement experimental behaviour to ObjectModelDescriber
DjordyKoert Oct 4, 2024
2c97df7
Implement class property describers as model describers
DjordyKoert Oct 4, 2024
41d5c1c
Fix union & intersection using wrong property
DjordyKoert Oct 4, 2024
c462cbe
Implement free-form object describer
DjordyKoert Oct 4, 2024
d538707
Implement enum describer
DjordyKoert Oct 4, 2024
99f1491
Fix union & intersection generating invalid spec
DjordyKoert Oct 4, 2024
b8c68c7
Remove enum describer
DjordyKoert Oct 4, 2024
d41df52
Remove unnecessary result class
DjordyKoert Oct 4, 2024
f1b4bef
Deprecate RequiredPropertyDescriber
DjordyKoert Oct 8, 2024
29bc887
Update tests for new required property behaviour
DjordyKoert Oct 8, 2024
074bf30
Use getVersion method
DjordyKoert Oct 8, 2024
9d61909
Remove type-info component (comes with symfony/property-info)
DjordyKoert Oct 8, 2024
0f00b18
Merge branch 'master' into 2212-type-info
DjordyKoert Oct 8, 2024
7eaa240
bump phpstan symfony version
DjordyKoert Oct 8, 2024
f19b9b5
Merge branch 'master' into 2212-type-info
DjordyKoert Oct 17, 2024
b37214f
style fix
DjordyKoert Oct 17, 2024
6c76df2
remove phpstan ignore
DjordyKoert Oct 18, 2024
183eeff
baseline update
DjordyKoert Oct 18, 2024
54599b8
Model new model describers
DjordyKoert Oct 18, 2024
48df032
Rename to TypeDescriber
DjordyKoert Oct 18, 2024
7c045d7
Mark experimental describers as internal
DjordyKoert Oct 18, 2024
79b1bdc
Remove deprecation warning
DjordyKoert Oct 18, 2024
223954d
Remove call to markRequiredProperties
DjordyKoert Oct 18, 2024
0327105
fix invalid context being passed
DjordyKoert Oct 18, 2024
0a679fa
Merge branch 'master' into 2212-type-info
DjordyKoert Jan 7, 2025
4fe66c9
baseline
DjordyKoert Jan 7, 2025
90db746
update methods to 7.2
DjordyKoert Jan 7, 2025
cde4e64
simplify config
DjordyKoert Jan 7, 2025
7608790
fix unknown method call
DjordyKoert Jan 7, 2025
3b69f62
update bool check to also document true and false
DjordyKoert Jan 7, 2025
6417521
document generic arrays
DjordyKoert Jan 7, 2025
1efbf26
Revert "document generic arrays"
DjordyKoert Jan 7, 2025
93236dc
remove @internal from trait & interfaces
DjordyKoert Jan 7, 2025
9c7909e
remove trait usage
DjordyKoert Jan 7, 2025
bed716e
add functional tests for class to schema
DjordyKoert Jan 8, 2025
f52013f
Merge branch 'master' into 2212-type-info
DjordyKoert Jan 8, 2025
f22d702
fix ChainDescriber always returning true
DjordyKoert Jan 8, 2025
cc6c42f
prevent list & dictionary from merging results
DjordyKoert Jan 8, 2025
2f37e2c
Describe generic array keys
DjordyKoert Jan 8, 2025
e1c886b
Describe generic array keys
DjordyKoert Jan 8, 2025
e76be96
Schema tests
DjordyKoert Jan 8, 2025
bea1021
ignore fixtures for phpcs
DjordyKoert Jan 8, 2025
dd54133
test scalar properties
DjordyKoert Jan 8, 2025
227bf3b
test mixed property
DjordyKoert Jan 8, 2025
efdf2b6
Merge branch 'master' into 2212-type-info
DjordyKoert Jan 17, 2025
58ffc76
add test method for coverage
DjordyKoert Jan 17, 2025
e9dbd52
fix coverage
DjordyKoert Jan 17, 2025
152a695
skip tests if not available
DjordyKoert Jan 17, 2025
44d7629
remove unit test
DjordyKoert Jan 17, 2025
b953c20
fix version compare
DjordyKoert Jan 17, 2025
296b3c9
remove BC layer
DjordyKoert Jan 17, 2025
98413ea
add intersection & plain object test
DjordyKoert Jan 17, 2025
5f5d78a
fix intersection test on <php 8.1
DjordyKoert Jan 17, 2025
c3a9193
fix plain object not describing
DjordyKoert Jan 17, 2025
668f091
fix plain object not describing
DjordyKoert Jan 17, 2025
b38e8f0
remove intersection logic for handling non-objects
DjordyKoert Jan 17, 2025
e8f7dc2
remove $type check
DjordyKoert Jan 17, 2025
71fd87e
add ArrayDescriberTest
DjordyKoert Jan 17, 2025
b6bdb8e
test uuid & datetime
DjordyKoert Jan 17, 2025
e97908a
phpstan
DjordyKoert Jan 17, 2025
37a4aa8
fix phpunit
DjordyKoert Jan 17, 2025
a3692c8
test invalid types
DjordyKoert Jan 17, 2025
c6f3bde
simplify object model describer
DjordyKoert Jan 17, 2025
14c2b0f
update config description
DjordyKoert Jan 17, 2025
9fb0a63
changelog
DjordyKoert Jan 17, 2025
3756775
move faq to bottom
DjordyKoert Jan 17, 2025
46aa7f3
bump versionadded
DjordyKoert Jan 17, 2025
54dd767
docs
DjordyKoert Jan 17, 2025
19730ae
expand test cases
DjordyKoert Jan 18, 2025
9c06418
update service definition
DjordyKoert Jan 18, 2025
3271f35
add priority to allow better customization
DjordyKoert Jan 18, 2025
e3a5dcd
remove null type handling from IntersectionDescriber
DjordyKoert Jan 18, 2025
3bc93d7
increase nullable priority
DjordyKoert Jan 18, 2025
87f5643
Do not check for object type in union
DjordyKoert Jan 18, 2025
eb17f11
test nullable array
DjordyKoert Jan 18, 2025
52792ee
ensure refs are valid
DjordyKoert Jan 18, 2025
5589601
rename to ClassDescriber
DjordyKoert Jan 18, 2025
3851753
fix non-existing uuid 7 & 8 in Symfony 5.4
DjordyKoert Jan 18, 2025
5a76a85
remove @experimental
DjordyKoert Jan 18, 2025
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
8 changes: 4 additions & 4 deletions .doctor-rst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ rules:

# master
versionadded_directive_major_version:
major_version: 6
major_version: 7

versionadded_directive_min_version:
min_version: '6.0'
min_version: '7.0'

deprecated_directive_major_version:
major_version: 5
major_version: 6

deprecated_directive_min_version:
min_version: '5.0'
min_version: '6.0'

# do not report as violation
whitelist:
Expand Down
22 changes: 16 additions & 6 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@

declare(strict_types=1);

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude('var')
->exclude('tests/Functional/cache');
->exclude('tests/Functional/cache')
->exclude('tests/Functional/ModelDescriber/Fixtures');

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'@PHP84Migration' => true,
'header_comment' => [
'header' => <<<HEADER
This file is part of the NelmioApiDocBundle package.
This file is part of the NelmioApiDocBundle package.

(c) Nelmio
(c) Nelmio

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
HEADER
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
HEADER
],
'trailing_comma_in_multiline' => false,
])
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 4.35.0
* Added support for the symfony/type-info component
```yaml
nelmio_api_doc:
type_info: true
```

## 4.34.0
* Changed minimum Symfony version for 7.x from 7.0 to 7.1

Expand Down
59 changes: 59 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,65 @@
<service id="nelmio_api_doc.swagger.processor.nullable_property" class="Nelmio\ApiDocBundle\Processor\NullablePropertyProcessor">
<tag name="nelmio_api_doc.swagger.processor" />
</service>

<!-- Type describers (symfony/type-info) -->
<service id="nelmio_api_doc.type_describer.chain" class="Nelmio\ApiDocBundle\TypeDescriber\ChainDescriber" public="false">
<argument type="tagged" tag="nelmio_api_doc.type_describer" />

<tag name="nelmio_api_doc.type_describer" priority="1000" />
</service>

<service id="nelmio_api_doc.type_describer.array" class="Nelmio\ApiDocBundle\TypeDescriber\ArrayDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.bool" class="Nelmio\ApiDocBundle\TypeDescriber\BoolDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.class" class="Nelmio\ApiDocBundle\TypeDescriber\ClassDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.dictionary" class="Nelmio\ApiDocBundle\TypeDescriber\DictionaryDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.float" class="Nelmio\ApiDocBundle\TypeDescriber\FloatDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.integer" class="Nelmio\ApiDocBundle\TypeDescriber\IntegerDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.intersection" class="Nelmio\ApiDocBundle\TypeDescriber\IntersectionDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.list" class="Nelmio\ApiDocBundle\TypeDescriber\ListDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.mixed" class="Nelmio\ApiDocBundle\TypeDescriber\MixedDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.nullable" class="Nelmio\ApiDocBundle\TypeDescriber\NullableDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-950" />
</service>

<service id="nelmio_api_doc.type_describer.object" class="Nelmio\ApiDocBundle\TypeDescriber\ObjectDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.string" class="Nelmio\ApiDocBundle\TypeDescriber\StringDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>

<service id="nelmio_api_doc.type_describer.union" class="Nelmio\ApiDocBundle\TypeDescriber\UnionDescriber" public="false">
<tag name="nelmio_api_doc.type_describer" priority="-1000" />
</service>
</services>

</container>
83 changes: 82 additions & 1 deletion docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,85 @@ Note, however, that a ``type="object"`` will still read all a models properties.
class SomeCollection implements \IteratorAggregate
{
// ...
}
}

PropertyInfo component was unable to guess the type
---------------------------------------------------

Q: I have a property that is not recognized. How can I specify the type?

.. tip::

Enable the `TypeInfo component`_ in your configuration to improve automatic type guessing:

.. code-block:: yaml

nelmio_api_doc:
type_info: true
# ...

.. versionadded:: 7.2

The `TypeInfo component`_ was introduced as a stable feature in Symfony 7.2.

A: If you want to customize the documentation of an object's property, you can use the ``#[OA\Property]`` attribute or annotate the property with ``@var``::

.. configuration-block::

.. code-block:: php-annotations

use Nelmio\ApiDocBundle\Attribute\Model;
use OpenApi\Annotations as OA;

class User
{
/**
* @var int
* @OA\Property(description="The unique identifier of the user.")
*/
public $id;

/**
* @OA\Property(type="string", maxLength=255)
*/
public $username;

/**
* @OA\Property(ref=@Model(type=User::class))
*/
public $friend;

/**
* @OA\Property(description="This is my coworker!")
*/
public setCoworker(User $coworker) {
// ...
}
}

.. code-block:: php-attributes

use Nelmio\ApiDocBundle\Attribute\Model;
use OpenApi\Attributes as OA;

class User
{
/**
* @var int
*/
#[OA\Property(description: 'The unique identifier of the user.')]
public $id;

#[OA\Property(type: 'string', maxLength: 255)]
public $username;

#[OA\Property(ref: new Model(type: User::class))]
public $friend;

#[OA\Property(description: 'This is my coworker!')]
public setCoworker(User $coworker) {
// ...
}
}

.. _`TypeInfo component`: https://symfony.com/doc/current/components/type_info.html
17 changes: 16 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ General PHP objects
When using the JMS serializer combined with `willdurand/Hateoas`_ (and the `BazingaHateoasBundle`_),
HATEOAS metadata are automatically extracted

.. tip::

Enable the `TypeInfo component`_ in your configuration to improve automatic type guessing:

.. code-block:: yaml

nelmio_api_doc:
type_info: true
# ...

.. versionadded:: 7.2

The `TypeInfo component`_ was introduced as a stable feature in Symfony 7.2.

If you want to customize the documentation of an object's property, you can use ``#[OA\Property]``::


Expand Down Expand Up @@ -601,12 +615,13 @@ If you need more complex features, take a look at:
alternative_names
customization
commands
faq
security
symfony_attributes
faq

.. _`SwaggerPHP examples`: https://github.com/zircote/swagger-php/tree/master/Examples
.. _`Symfony PropertyInfo component`: https://symfony.com/doc/current/components/property_info.html
.. _`TypeInfo component`: https://symfony.com/doc/current/components/type_info.html
.. _`willdurand/Hateoas`: https://github.com/willdurand/Hateoas
.. _`BazingaHateoasBundle`: https://github.com/willdurand/BazingaHateoasBundle
.. _`JMS serializer`: https://jmsyst.com/libs/serializer
Expand Down
14 changes: 0 additions & 14 deletions docs/symfony_attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ MapQueryString

Using the `Symfony MapQueryString`_ attribute allows NelmioApiDocBundle to automatically generate your query parameter documentation for your endpoint from your object.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapQueryString` attribute was introduced in Symfony 6.3.

Modify generated documentation
~~~~~~~

Expand All @@ -35,11 +31,6 @@ MapQueryParameter

Using the `Symfony MapQueryParameter`_ attribute allows NelmioApiDocBundle to automatically generate your query parameter documentation for your endpoint.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapQueryParameter` attribute was introduced in Symfony 6.3.


Modify generated documentation
~~~~~~~

Expand All @@ -59,11 +50,6 @@ MapRequestPayload

Using the `Symfony MapRequestPayload`_ attribute allows NelmioApiDocBundle to automatically generate your request body documentation for your endpoint.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapRequestPayload` attribute was introduced in Symfony 6.3.


Modify generated documentation
~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ parameters:
count: 1
path: src/Describer/ExternalDocDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/ModelDescriber/ObjectModelDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\ArrayPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->booleanNode('type_info')
->info('Use the symfony/type-info component for determining types.')
->defaultFalse()
->end()
->booleanNode('use_validation_groups')
->info('If true, `groups` passed to @Model annotations will be used to limit validation constraints')
->defaultFalse()
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public function load(array $configs, ContainerBuilder $container): void
array_map(function ($area) { return new Reference(sprintf('nelmio_api_doc.generator.%s', $area)); }, array_keys($config['areas']))
));

if (true === $config['type_info']) {
$container->getDefinition('nelmio_api_doc.model_describers.object')
->setArgument(2, new Reference('nelmio_api_doc.type_describer.chain'));
}

$container->getDefinition('nelmio_api_doc.model_describers.object')
->setArgument(3, $config['media_types']);

Expand Down
Loading
Loading