Skip to content

Commit 3d5fbc8

Browse files
authored
Merge 2cc2ce4 into dcdea86
2 parents dcdea86 + 2cc2ce4 commit 3d5fbc8

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/Hal/Serializer/ItemNormalizer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,29 @@ private function populateRelation(array $data, $object, string $format = null, a
174174
continue;
175175
}
176176

177+
$relationName = $relation['name'];
178+
if ($this->nameConverter) {
179+
$relationName = $this->nameConverter->normalize($relationName);
180+
}
181+
177182
if ('one' === $relation['cardinality']) {
178183
if ('links' === $type) {
179-
$data[$key][$relation['name']]['href'] = $this->getRelationIri($attributeValue);
184+
$data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
180185
continue;
181186
}
182187

183-
$data[$key][$relation['name']] = $attributeValue;
188+
$data[$key][$relationName] = $attributeValue;
184189
continue;
185190
}
186191

187192
// many
188-
$data[$key][$relation['name']] = [];
193+
$data[$key][$relationName] = [];
189194
foreach ($attributeValue as $rel) {
190195
if ('links' === $type) {
191196
$rel = ['href' => $this->getRelationIri($rel)];
192197
}
193198

194-
$data[$key][$relation['name']][] = $rel;
199+
$data[$key][$relationName][] = $rel;
195200
}
196201
}
197202

tests/Hal/Serializer/ItemNormalizerTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2525
use PHPUnit\Framework\TestCase;
2626
use Prophecy\Argument;
27+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2728
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2829
use Symfony\Component\Serializer\SerializerInterface;
2930

@@ -41,12 +42,15 @@ public function testDonTSupportDenormalization()
4142
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
4243
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
4344
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
45+
$nameConverter = $this->prophesize(NameConverterInterface::class);
4446

4547
$normalizer = new ItemNormalizer(
4648
$propertyNameCollectionFactoryProphecy->reveal(),
4749
$propertyMetadataFactoryProphecy->reveal(),
4850
$iriConverterProphecy->reveal(),
49-
$resourceClassResolverProphecy->reveal()
51+
$resourceClassResolverProphecy->reveal(),
52+
null,
53+
$nameConverter->reveal()
5054
);
5155

5256
$this->assertFalse($normalizer->supportsDenormalization('foo', ItemNormalizer::FORMAT));
@@ -67,11 +71,15 @@ public function testSupportNormalization()
6771
$resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class)->shouldBeCalled();
6872
$resourceClassResolverProphecy->getResourceClass($std)->willThrow(new InvalidArgumentException())->shouldBeCalled();
6973

74+
$nameConverter = $this->prophesize(NameConverterInterface::class);
75+
7076
$normalizer = new ItemNormalizer(
7177
$propertyNameCollectionFactoryProphecy->reveal(),
7278
$propertyMetadataFactoryProphecy->reveal(),
7379
$iriConverterProphecy->reveal(),
74-
$resourceClassResolverProphecy->reveal()
80+
$resourceClassResolverProphecy->reveal(),
81+
null,
82+
$nameConverter->reveal()
7583
);
7684

7785
$this->assertTrue($normalizer->supportsNormalization($dummy, 'jsonhal'));
@@ -103,11 +111,16 @@ public function testNormalize()
103111
$serializerProphecy->willImplement(NormalizerInterface::class);
104112
$serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled();
105113

114+
$nameConverter = $this->prophesize(NameConverterInterface::class);
115+
$nameConverter->normalize('name')->shouldBeCalled()->willReturn('name');
116+
106117
$normalizer = new ItemNormalizer(
107118
$propertyNameCollectionFactoryProphecy->reveal(),
108119
$propertyMetadataFactoryProphecy->reveal(),
109120
$iriConverterProphecy->reveal(),
110-
$resourceClassResolverProphecy->reveal()
121+
$resourceClassResolverProphecy->reveal(),
122+
null,
123+
$nameConverter->reveal()
111124
);
112125
$normalizer->setSerializer($serializerProphecy->reveal());
113126

@@ -146,11 +159,16 @@ public function testNormalizeWithoutCache()
146159
$serializerProphecy->willImplement(NormalizerInterface::class);
147160
$serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled();
148161

162+
$nameConverter = $this->prophesize(NameConverterInterface::class);
163+
$nameConverter->normalize('name')->shouldBeCalled()->willReturn('name');
164+
149165
$normalizer = new ItemNormalizer(
150166
$propertyNameCollectionFactoryProphecy->reveal(),
151167
$propertyMetadataFactoryProphecy->reveal(),
152168
$iriConverterProphecy->reveal(),
153-
$resourceClassResolverProphecy->reveal()
169+
$resourceClassResolverProphecy->reveal(),
170+
null,
171+
$nameConverter->reveal()
154172
);
155173
$normalizer->setSerializer($serializerProphecy->reveal());
156174

0 commit comments

Comments
 (0)