Skip to content

Commit f177cfc

Browse files
authored
Merge pull request #2344 from Soullivaneuh/advanced-name-converter
Manage advanced name converters for documentation generation
2 parents 88ed3c6 + 781222d commit f177cfc

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function normalize($object, $format = null, array $context = [])
7676
$prefixedShortName = $resourceMetadata->getIri() ?? "#$shortName";
7777

7878
$this->populateEntrypointProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties);
79-
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName);
79+
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context);
8080
}
8181

8282
return $this->computeDoc($object, $this->getClasses($entrypointProperties, $classes));
@@ -125,14 +125,14 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
125125
/**
126126
* Gets a Hydra class.
127127
*/
128-
private function getClass(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array
128+
private function getClass(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array $context): array
129129
{
130130
$class = [
131131
'@id' => $prefixedShortName,
132132
'@type' => 'hydra:Class',
133133
'rdfs:label' => $shortName,
134134
'hydra:title' => $shortName,
135-
'hydra:supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName),
135+
'hydra:supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context),
136136
'hydra:supportedOperation' => $this->getHydraOperations($resourceClass, $resourceMetadata, $prefixedShortName, false),
137137
];
138138

@@ -175,7 +175,7 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
175175
/**
176176
* Gets Hydra properties.
177177
*/
178-
private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array
178+
private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array $context): array
179179
{
180180
$properties = [];
181181
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $this->getPropertyNameCollectionFactoryContext($resourceMetadata)) as $propertyName) {
@@ -185,7 +185,7 @@ private function getHydraProperties(string $resourceClass, ResourceMetadata $res
185185
}
186186

187187
if ($this->nameConverter) {
188-
$propertyName = $this->nameConverter->normalize($propertyName);
188+
$propertyName = $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $context);
189189
}
190190

191191
$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ private function getDefinitionSchema(string $resourceClass, ResourceMetadata $re
430430
$options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : [];
431431
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) {
432432
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
433-
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
433+
$normalizedPropertyName = $this->nameConverter
434+
? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $serializerContext)
435+
: $propertyName
436+
;
434437

435438
if ($propertyMetadata->isRequired()) {
436439
$definitionSchema['required'][] = $normalizedPropertyName;

tests/Swagger/Serializer/DocumentationNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ public function testNormalizeWithNameConverter()
332332
$operationMethodResolverProphecy->getItemOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET');
333333

334334
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
335-
$nameConverterProphecy->normalize('name')->willReturn('name')->shouldBeCalled();
336-
$nameConverterProphecy->normalize('nameConverted')->willReturn('name_converted')->shouldBeCalled();
335+
$nameConverterProphecy->normalize('name', Dummy::class, DocumentationNormalizer::FORMAT, null)->willReturn('name')->shouldBeCalled();
336+
$nameConverterProphecy->normalize('nameConverted', Dummy::class, DocumentationNormalizer::FORMAT, null)->willReturn('name_converted')->shouldBeCalled();
337337

338338
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
339339

0 commit comments

Comments
 (0)