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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement experimental type-info describers
DjordyKoert committed Oct 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 99c86c441eadf4e3dacf47cdd23e3309fcc42e2e
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -25,6 +25,10 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->booleanNode('experimental_type_info')
->info('Use the symfony/type-info component for determining types. This is experimental and could be changed at any time without prior notice.')
->defaultFalse()
->end()
->booleanNode('use_validation_groups')
->info('If true, `groups` passed to @Model annotations will be used to limit validation constraints')
->defaultFalse()
34 changes: 34 additions & 0 deletions src/SchemaDescriber/BoolDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<Type\BuiltinType>
*
* @experimental
*/
final class BoolDescriber implements SchemaDescriberInterface
{
public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = 'boolean';
}

public function supports(Type $type, array $context = []): bool
{
return $type->isA(TypeIdentifier::BOOL);
}
}
63 changes: 63 additions & 0 deletions src/SchemaDescriber/ChainDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;

/**
* @implements SchemaDescriberInterface<Type>
*
* @experimental
*/
final class ChainDescriber implements SchemaDescriberInterface
{
/** @var iterable<SchemaDescriberInterface> */
private iterable $describers;

/**
* @param iterable<SchemaDescriberInterface> $describers
*/
public function __construct(
iterable $describers
) {
$this->describers = $describers;
}

public function describe(Type $type, Schema $schema, array $context = []): void
{
foreach ($this->describers as $describer) {
/* BC layer for Symfony < 6.3 @see https://symfony.com/doc/6.3/service_container/tags.html#reference-tagged-services */
if ($describer instanceof self) {
continue;
}

// TODO: Implement proper dependency injection
// if ($describer instanceof ModelRegistryAwareInterface) {
// $describer->setModelRegistry($this->modelRegistry);
// }

if ($describer instanceof SchemaDescriberAwareInterface) {
$describer->setDescriber($this);
}

if ($describer->supports($type, $context)) {
$describer->describe($type, $schema, $context);
}
}
}

public function supports(Type $type, array $context = []): bool
{
return true;
}
}
43 changes: 43 additions & 0 deletions src/SchemaDescriber/DictionaryDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<CollectionType>
*
* @experimental
*/
final class DictionaryDescriber implements SchemaDescriberInterface, SchemaDescriberAwareInterface
{
use SchemaDescriberAwareTrait;

public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = 'object';
$additionalProperties = Util::getChild($schema, OA\AdditionalProperties::class);

$this->describer->describe($type->getCollectionValueType(), $additionalProperties, $context);
}

public function supports(Type $type, array $context = []): bool
{
return $type instanceof CollectionType
&& $type->getCollectionKeyType()->isA(TypeIdentifier::STRING);
}
}
35 changes: 35 additions & 0 deletions src/SchemaDescriber/FloatDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<Type\BuiltinType>
*
* @experimental
*/
final class FloatDescriber implements SchemaDescriberInterface
{
public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = 'number';
$schema->format = 'float';
}

public function supports(Type $type, array $context = []): bool
{
return $type->isA(TypeIdentifier::FLOAT);
}
}
34 changes: 34 additions & 0 deletions src/SchemaDescriber/IntegerDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<Type\BuiltinType>
*
* @experimental
*/
final class IntegerDescriber implements SchemaDescriberInterface
{
public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = 'integer';
}

public function supports(Type $type, array $context = []): bool
{
return $type->isA(TypeIdentifier::INT);
}
}
45 changes: 45 additions & 0 deletions src/SchemaDescriber/IntersectionDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\IntersectionType;

/**
* @implements SchemaDescriberInterface<IntersectionType>
*
* @experimental
*/
final class IntersectionDescriber implements SchemaDescriberInterface, SchemaDescriberAwareInterface
{
use SchemaDescriberAwareTrait;

public function describe(Type $type, Schema $schema, array $context = []): void
{
$weakContext = Util::createWeakContext($schema->_context);

foreach ($type->getTypes() as $innerType) {
$schema->oneOf[] = $childSchema = new Schema([
'_context' => $weakContext
]);

$this->describer->describe($innerType, $childSchema, $context);
}
}

public function supports(Type $type, array $context = []): bool
{
return $type instanceof IntersectionType;
}
}
43 changes: 43 additions & 0 deletions src/SchemaDescriber/ListDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<CollectionType>
*
* @experimental
*/
final class ListDescriber implements SchemaDescriberInterface, SchemaDescriberAwareInterface
{
use SchemaDescriberAwareTrait;

public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = 'array';
$item = Util::getChild($schema, OA\Items::class);

$this->describer->describe($type->getCollectionValueType(), $item, $context);
}

public function supports(Type $type, array $context = []): bool
{
return $type instanceof CollectionType
&& $type->getCollectionKeyType()->isA(TypeIdentifier::INT);
}
}
35 changes: 35 additions & 0 deletions src/SchemaDescriber/MixedDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use OpenApi\Generator;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @implements SchemaDescriberInterface<Type\BuiltinType>
*
* @experimental
*/
final class MixedDescriber implements SchemaDescriberInterface
{
public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->type = Generator::UNDEFINED;
}

public function supports(Type $type, array $context = []): bool
{
return $type->isA(TypeIdentifier::MIXED);
}
}
35 changes: 35 additions & 0 deletions src/SchemaDescriber/NullableDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* 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.
*/

namespace Nelmio\ApiDocBundle\SchemaDescriber;

use OpenApi\Annotations\Schema;
use Symfony\Component\TypeInfo\Type;

/**
* @implements SchemaDescriberInterface<Type>
*
* @experimental
*/
final class NullableDescriber implements SchemaDescriberInterface, SchemaDescriberAwareInterface
{
use SchemaDescriberAwareTrait;

public function describe(Type $type, Schema $schema, array $context = []): void
{
$schema->nullable = true;
}

public function supports(Type $type, array $context = []): bool
{
return $type->isNullable();
}
}
Loading