Skip to content

Commit eed3d6e

Browse files
Refactor changes to use $context
Add values from the Symfony Context attribute to property context, for use in property describers.
1 parent 29aab02 commit eed3d6e

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

src/ModelDescriber/Annotations/AnnotationsReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public function getPropertyName($reflection, string $default): string
6161
* @param \ReflectionProperty|\ReflectionMethod $reflection
6262
* @param string[]|null $serializationGroups
6363
*/
64-
public function updateProperty($reflection, OA\Property $property, ?array $serializationGroups = null): void
64+
public function updateProperty($reflection, OA\Property $property, array &$context, ?array $serializationGroups = null): void
6565
{
6666
$this->openApiAnnotationsReader->updateProperty($reflection, $property, $serializationGroups);
6767
$this->phpDocReader->updateProperty($reflection, $property);
6868
$this->reflectionReader->updateProperty($reflection, $property);
69-
$this->symfonyAnnotationReader->updateProperty($reflection, $property, $serializationGroups);
69+
$this->symfonyAnnotationReader->updateProperty($reflection, $property, $context, $serializationGroups);
7070
}
7171

7272
/**

src/ModelDescriber/Annotations/SymfonyAnnotationReader.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(bool $useValidationGroups = false)
4444
* @param \ReflectionProperty|\ReflectionMethod $reflection
4545
* @param string[]|null $validationGroups
4646
*/
47-
public function updateProperty($reflection, OA\Property $property, ?array $validationGroups = null): void
47+
public function updateProperty($reflection, OA\Property $property, array &$context=[], ?array $validationGroups = null): void
4848
{
4949
// Handle constraints
5050
foreach ($this->getConstraintAttributes($property->_context, $reflection, $validationGroups) as $outerAttribute) {
@@ -58,10 +58,7 @@ public function updateProperty($reflection, OA\Property $property, ?array $valid
5858
// Handle context
5959
$context = $reflection->getAttributes(\Symfony\Component\Serializer\Attribute\Context::class);
6060
if (1 === \count($context)) {
61-
$contextArgs = $context[0]->getArguments()[0];
62-
if ('Y-m-d' === ($contextArgs['datetime_format'] ?? null)) {
63-
$property->format = 'date';
64-
}
61+
$context['symfony_context'] = $context[0]->getArguments()[0];
6562
}
6663
}
6764

src/ModelDescriber/JMSModelDescriber.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function describe(Model $model, OA\Schema $schema): void
120120
$context = $this->getSerializationContext($model);
121121
$context->pushClassMetadata($metadata);
122122
foreach ($metadata->propertyMetadata as $item) {
123+
$propertyContext = $model->getSerializationContext();
123124
// filter groups
124125
if (null !== $context->getExclusionStrategy() && $context->getExclusionStrategy()->shouldSkipProperty($item, $context)) {
125126
continue;
@@ -176,7 +177,7 @@ public function describe(Model $model, OA\Schema $schema): void
176177
$property = Util::getProperty($schema, $name);
177178

178179
foreach ($reflections as $reflection) {
179-
$annotationsReader->updateProperty($reflection, $property, $groups);
180+
$annotationsReader->updateProperty($reflection, $property, $propertyContext, $groups);
180181
}
181182

182183
if (Generator::UNDEFINED !== $property->type || Generator::UNDEFINED !== $property->ref) {
@@ -197,7 +198,7 @@ public function describe(Model $model, OA\Schema $schema): void
197198
continue;
198199
}
199200

200-
$this->describeItem($item->type, $property, $context, $model->getSerializationContext());
201+
$this->describeItem($item->type, $property, $context, $propertyContext);
201202
$context->popPropertyMetadata();
202203
}
203204
$context->popClassMetadata();

src/ModelDescriber/ObjectModelDescriber.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function describe(Model $model, OA\Schema $schema): void
113113
$propertyInfoProperties = array_intersect($propertyInfoProperties, $this->propertyInfo->getProperties($class, []) ?? []);
114114

115115
foreach ($propertyInfoProperties as $propertyName) {
116+
$propertyContext = [];
116117
$serializedName = null !== $this->nameConverter ? $this->nameConverter->normalize($propertyName, $class, null, $model->getSerializationContext()) : $propertyName;
117118

118119
$reflections = $this->getReflections($reflClass, $propertyName);
@@ -134,7 +135,7 @@ public function describe(Model $model, OA\Schema $schema): void
134135
$groups = $model->getGroups()[$propertyName];
135136
}
136137
foreach ($reflections as $reflection) {
137-
$annotationsReader->updateProperty($reflection, $property, $groups);
138+
$annotationsReader->updateProperty($reflection, $property, $propertyContext, $groups);
138139
}
139140

140141
// If type manually defined
@@ -152,7 +153,7 @@ public function describe(Model $model, OA\Schema $schema): void
152153
throw new \LogicException(\sprintf('The PropertyInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `#[OA\Property(type="")]` to make its type explicit.', $class, $propertyName));
153154
}
154155

155-
$this->describeProperty($types, $model, $property, $propertyName);
156+
$this->describeProperty($types, $model, $property, $propertyName, $propertyContext);
156157
}
157158

158159
$this->markRequiredProperties($schema);
@@ -189,13 +190,14 @@ private function camelize(string $string): string
189190
/**
190191
* @param LegacyType[]|Type $types
191192
*/
192-
private function describeProperty(array|Type $types, Model $model, OA\Schema $property, string $propertyName): void
193+
private function describeProperty(array|Type $types, Model $model, OA\Schema $property, string $propertyName, array $context): void
193194
{
194195
if ($this->propertyDescriber instanceof ModelRegistryAwareInterface) {
195196
$this->propertyDescriber->setModelRegistry($this->modelRegistry);
196197
}
197-
if ($this->propertyDescriber->supports($types, $model->getSerializationContext())) {
198-
$this->propertyDescriber->describe($types, $property, $model->getSerializationContext());
198+
$context = array_merge($context, $model->getSerializationContext());
199+
if ($this->propertyDescriber->supports($types, $context)) {
200+
$this->propertyDescriber->describe($types, $property, $context);
199201

200202
return;
201203
}

src/PropertyDescriber/DateTimePropertyDescriber.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Nelmio\ApiDocBundle\PropertyDescriber;
1313

1414
use OpenApi\Annotations as OA;
15-
use OpenApi\Generator;
1615
use Symfony\Component\PropertyInfo\Type;
1716

1817
final class DateTimePropertyDescriber implements PropertyDescriberInterface
@@ -23,8 +22,12 @@ final class DateTimePropertyDescriber implements PropertyDescriberInterface
2322
public function describe(array $types, OA\Schema $property, array $context = []): void
2423
{
2524
$property->type = 'string';
26-
if (Generator::UNDEFINED === $property->format) {
27-
$property->format = 'date-time';
25+
$property->format = 'date-time';
26+
if (
27+
\array_key_exists('symfony_context', $context)
28+
&& 'Y-m-d' === ($context['symfony_context']['datetime_format'] ?? null)
29+
) {
30+
$property->format = 'date';
2831
}
2932
}
3033

tests/ModelDescriber/Annotations/SymfonyAnnotationReaderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,12 @@ public function testReaderWithValidationGroupsEnabledReadsOnlyConstraintsWithGro
367367
$reader = new SymfonyAnnotationReader(true);
368368
$reader->setSchema($schema);
369369

370+
$context = [];
370371
// no serialization groups passed here
371372
$reader->updateProperty(
372373
new \ReflectionProperty($entity, 'property1'),
373374
$schema->properties[0],
375+
$context,
374376
['other']
375377
);
376378

@@ -388,10 +390,12 @@ public function testReaderWithValidationGroupsEnabledCanReadFromMultipleValidati
388390
$reader = new SymfonyAnnotationReader(true);
389391
$reader->setSchema($schema);
390392

393+
$context = [];
391394
// no serialization groups passed here
392395
$reader->updateProperty(
393396
new \ReflectionProperty($entity, 'property1'),
394397
$schema->properties[0],
398+
$context,
395399
['other', Constraint::DEFAULT_GROUP]
396400
);
397401

0 commit comments

Comments
 (0)