Merge pull request #2043 from nelmio/REVERT

Revert "Use the same root context everywhere"
This commit is contained in:
Guilhem Niot 2022-10-18 21:55:47 +02:00 committed by GitHub
commit 52921ac56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 17 additions and 57 deletions

View File

@ -105,6 +105,7 @@ final class ApiDocGenerator
$describer->describe($this->openApi);
}
$analysis = new Analysis([], $context);
$analysis->addAnnotation($this->openApi, $context);

View File

@ -68,7 +68,7 @@ final class ModelRegistry
}
// Reserve the name
$s = Util::getSchema($this->api, $this->names[$hash]);
Util::getSchema($this->api, $this->names[$hash]);
return OA\Components::SCHEMA_REF.$this->names[$hash];
}

View File

@ -18,6 +18,7 @@ use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use Nelmio\ApiDocBundle\Util\SetsContextTrait;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
/**
@ -38,15 +39,11 @@ class OpenApiAnnotationsReader
public function updateSchema(\ReflectionClass $reflectionClass, OA\Schema $schema): void
{
$this->setContext(Util::createContext([], $schema->_context));
/** @var OA\Schema|null $oaSchema */
if (!$oaSchema = $this->getAnnotation($reflectionClass, OA\Schema::class)) {
return;
}
$this->setContext(null);
// Read @Model annotations
$this->modelRegister->__invoke(new Analysis([$oaSchema], Util::createContext()));
@ -72,12 +69,12 @@ class OpenApiAnnotationsReader
// In order to have nicer errors
$declaringClass = $reflection->getDeclaringClass();
$this->setContext(Util::createContext([
$this->setContext(new Context([
'namespace' => $declaringClass->getNamespaceName(),
'class' => $declaringClass->getShortName(),
'property' => $reflection->name,
'filename' => $declaringClass->getFileName(),
], $property->_context));
]));
/** @var OA\Property|null $oaProperty */
if (!$oaProperty = $this->getAnnotation($reflection, OA\Property::class)) {

View File

@ -13,7 +13,6 @@ namespace Nelmio\ApiDocBundle\ModelDescriber;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
@ -44,18 +43,18 @@ trait ApplyOpenApiDiscriminatorTrait
array $typeMap
): void {
$schema->oneOf = [];
$discriminator = Util::getChild($schema, OA\Discriminator::class);
$discriminator->propertyName = $discriminatorProperty;
$discriminator->mapping = [];
$schema->discriminator = new OA\Discriminator([]);
$schema->discriminator->propertyName = $discriminatorProperty;
$schema->discriminator->mapping = [];
foreach ($typeMap as $propertyValue => $className) {
$oneOfSchema = Util::createChild($schema, OA\Schema::class);
$oneOfSchema = new OA\Schema([]);
$oneOfSchema->ref = $modelRegistry->register(new Model(
new Type(Type::BUILTIN_TYPE_OBJECT, false, $className),
$model->getGroups(),
$model->getOptions()
));
$schema->oneOf[] = $oneOfSchema;
$discriminator->mapping[$propertyValue] = $oneOfSchema->ref;
$schema->discriminator->mapping[$propertyValue] = $oneOfSchema->ref;
}
}
}

View File

@ -150,7 +150,7 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
$ref = $this->modelRegistry->register($model);
// We need to use allOf for description and title to be displayed
if ($config->hasOption('documentation') && !empty($config->getOption('documentation'))) {
$property->allOf = [Util::createChild($property, OA\Schema::class, ['ref' => $ref])];
$property->allOf = [new OA\Schema(['ref' => $ref])];
} else {
$property->ref = $ref;
}

View File

@ -292,7 +292,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
if (empty($customFields)) { // no custom fields
$property->ref = $modelRef;
} else {
$property->allOf = [Util::createChild($property, OA\Schema::class, ['ref' => $modelRef])];
$property->allOf = [new OA\Schema(['ref' => $modelRef])];
}
$this->contexts[$model->getHash()] = $context;

View File

@ -160,11 +160,11 @@ final class ModelRegister
) {
switch ($type) {
case 'json':
$modelAnnotation = Util::createChild($annotation, OA\JsonContent::class, $properties);
$modelAnnotation = new OA\JsonContent($properties);
break;
case 'xml':
$modelAnnotation = Util::createChild($annotation, OA\XmlContent::class, $properties);
$modelAnnotation = new OA\XmlContent($properties);
break;
default:

View File

@ -84,7 +84,7 @@ final class Util
public static function getSchema(OA\OpenApi $api, $schema): OA\Schema
{
if (!$api->components instanceof OA\Components) {
$api->components = self::createChild($api, OA\Components::class, []);
$api->components = new OA\Components([]);
}
return self::getIndexedCollectionItem($api->components, OA\Schema::class, $schema);

View File

@ -14,7 +14,6 @@ namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
@ -38,7 +37,7 @@ class ObjectPropertyDescriber implements PropertyDescriberInterface, ModelRegist
if ($types[0]->isNullable()) {
$property->nullable = true;
$property->allOf = [Util::createChild($property, OA\Schema::class, ['ref' => $this->modelRegistry->register(new Model($type, $groups))])];
$property->allOf = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups))])];
return;
}

View File

@ -145,7 +145,7 @@ final class FosRestDescriber implements RouteDescriberInterface
throw new \InvalidArgumentException('Unsupported media type');
}
if (!isset($requestBody->content[$contentType])) {
$requestBody->content[$contentType] = Util::createChild($requestBody, OA\MediaType::class,
$requestBody->content[$contentType] = new OA\MediaType(
[
'mediaType' => $contentType,
]

View File

@ -1,36 +0,0 @@
<?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\Tests\Functional;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis;
class SwaggerPHPApiComplianceTest extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
static::createClient([], ['HTTP_HOST' => 'api.example.com']);
}
public function testAllContextsHaveSameRoot()
{
$openApi = $this->getOpenApiDefinition();
$root = $openApi->_context;
$counter = 0;
foreach ((new Analysis([$openApi], Util::createContext()))->annotations as $annotation) {
$this->assertSame($annotation->_context->root(), $root);
}
}
}