Skip to content

Commit

Permalink
Merge pull request #2043 from nelmio/REVERT
Browse files Browse the repository at this point in the history
Revert "Use the same root context everywhere"
  • Loading branch information
GuilhemN committed Oct 18, 2022
2 parents 1ddd3f3 + 6cab5c5 commit 52921ac
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 57 deletions.
1 change: 1 addition & 0 deletions ApiDocGenerator.php
Expand Up @@ -105,6 +105,7 @@ public function generate(): OpenApi

$describer->describe($this->openApi);
}

$analysis = new Analysis([], $context);
$analysis->addAnnotation($this->openApi, $context);

Expand Down
2 changes: 1 addition & 1 deletion Model/ModelRegistry.php
Expand Up @@ -68,7 +68,7 @@ public function register(Model $model): string
}

// 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];
}
Expand Down
9 changes: 3 additions & 6 deletions ModelDescriber/Annotations/OpenApiAnnotationsReader.php
Expand Up @@ -18,6 +18,7 @@
use Nelmio\ApiDocBundle\Util\SetsContextTrait;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;

/**
Expand All @@ -38,15 +39,11 @@ public function __construct(Reader $annotationsReader, ModelRegistry $modelRegis

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()));

Expand All @@ -72,12 +69,12 @@ public function updateProperty($reflection, OA\Property $property, array $serial
// 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)) {
Expand Down
11 changes: 5 additions & 6 deletions ModelDescriber/ApplyOpenApiDiscriminatorTrait.php
Expand Up @@ -13,7 +13,6 @@

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;

Expand Down Expand Up @@ -44,18 +43,18 @@ protected function applyOpenApiDiscriminator(
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;
}
}
}
2 changes: 1 addition & 1 deletion ModelDescriber/FormModelDescriber.php
Expand Up @@ -150,7 +150,7 @@ private function findFormType(FormConfigInterface $config, OA\Schema $property)
$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;
}
Expand Down
2 changes: 1 addition & 1 deletion ModelDescriber/JMSModelDescriber.php
Expand Up @@ -292,7 +292,7 @@ public function describeItem(array $type, OA\Schema $property, Context $context)
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;
Expand Down
4 changes: 2 additions & 2 deletions OpenApiPhp/ModelRegister.php
Expand Up @@ -160,11 +160,11 @@ private function createContentForMediaType(
) {
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:
Expand Down
2 changes: 1 addition & 1 deletion OpenApiPhp/Util.php
Expand Up @@ -84,7 +84,7 @@ public static function getPath(OA\OpenApi $api, $path): OA\PathItem
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);
Expand Down
3 changes: 1 addition & 2 deletions PropertyDescriber/ObjectPropertyDescriber.php
Expand Up @@ -14,7 +14,6 @@
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;

Expand All @@ -38,7 +37,7 @@ public function describe(array $types, OA\Schema $property, array $groups = null

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;
}
Expand Down
2 changes: 1 addition & 1 deletion RouteDescriber/FosRestDescriber.php
Expand Up @@ -145,7 +145,7 @@ private function getContentSchemaForType(OA\RequestBody $requestBody, string $ty
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,
]
Expand Down
36 changes: 0 additions & 36 deletions Tests/Functional/SwaggerPHPApiComplianceTest.php

This file was deleted.

0 comments on commit 52921ac

Please sign in to comment.