Skip to content

Commit

Permalink
Merge pull request #2344 from Soullivaneuh/advanced-name-converter
Browse files Browse the repository at this point in the history
Manage advanced name converters for documentation generation
  • Loading branch information
dunglas committed Dec 14, 2018
2 parents 88ed3c6 + 781222d commit f177cfc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function normalize($object, $format = null, array $context = [])
$prefixedShortName = $resourceMetadata->getIri() ?? "#$shortName";

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

return $this->computeDoc($object, $this->getClasses($entrypointProperties, $classes));
Expand Down Expand Up @@ -125,14 +125,14 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
/**
* Gets a Hydra class.
*/
private function getClass(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array
private function getClass(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array $context): array
{
$class = [
'@id' => $prefixedShortName,
'@type' => 'hydra:Class',
'rdfs:label' => $shortName,
'hydra:title' => $shortName,
'hydra:supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName),
'hydra:supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context),
'hydra:supportedOperation' => $this->getHydraOperations($resourceClass, $resourceMetadata, $prefixedShortName, false),
];

Expand Down Expand Up @@ -175,7 +175,7 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
/**
* Gets Hydra properties.
*/
private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array
private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array $context): array
{
$properties = [];
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $this->getPropertyNameCollectionFactoryContext($resourceMetadata)) as $propertyName) {
Expand All @@ -185,7 +185,7 @@ private function getHydraProperties(string $resourceClass, ResourceMetadata $res
}

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

$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);
Expand Down
5 changes: 4 additions & 1 deletion src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ private function getDefinitionSchema(string $resourceClass, ResourceMetadata $re
$options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : [];
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
$normalizedPropertyName = $this->nameConverter
? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $serializerContext)
: $propertyName
;

if ($propertyMetadata->isRequired()) {
$definitionSchema['required'][] = $normalizedPropertyName;
Expand Down
4 changes: 2 additions & 2 deletions tests/Swagger/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ public function testNormalizeWithNameConverter()
$operationMethodResolverProphecy->getItemOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET');

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

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

Expand Down

0 comments on commit f177cfc

Please sign in to comment.