Skip to content

Commit

Permalink
Merge pull request #28 from bookboon/hotfix/classFinderFix
Browse files Browse the repository at this point in the history
fix; Ensure classname in namespace takes precedence over mapping
  • Loading branch information
lkm authored Feb 28, 2022
2 parents 5656c28 + 157e749 commit cb5290e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Mapping/MappingCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public function findClassByShortNameOrDefault(string $shortClass, string $defaul
return "Bookboon\JsonLDClient\Models\\$shortClass";
}

$classFromMapping = null;
foreach ($this->mappings as $mapping) {
if ($mapping->matchesShortName($shortClass)) {
return $mapping->getType();
$classFromMapping = $mapping->getType();
}
}

return $defaultNamespace . '\\' . $shortClass;
$guessedClassName = $defaultNamespace . '\\' . $shortClass;
return class_exists($guessedClassName) || $classFromMapping === null ? $guessedClassName : $classFromMapping;
}
}
19 changes: 19 additions & 0 deletions Tests/Fixtures/Models/ChildClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bookboon\JsonLDClient\Tests\Fixtures\Models;

class ChildClass
{
private string $title = '';

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): void
{
$this->title = $title;
}

}
30 changes: 30 additions & 0 deletions Tests/Fixtures/Models/DummyClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Bookboon\JsonLDClient\Tests\Fixtures\Models;

class DummyClass
{
private string $name = '';

private ?ChildClass $childClass = null;

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getChildClass(): ?ChildClass
{
return $this->childClass;
}

public function setChildClass(?ChildClass $childClass): void
{
$this->childClass = $childClass;
}
}
19 changes: 19 additions & 0 deletions Tests/Fixtures/OtherModels/ChildClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bookboon\JsonLDClient\Tests\Fixtures\OtherModels;

class ChildClass
{
private ?string $title = null;

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): void
{
$this->title = $title;
}

}
34 changes: 34 additions & 0 deletions Tests/Serializer/JsonLDNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Bookboon\JsonLDClient\Mapping\MappingEndpoint;
use Bookboon\JsonLDClient\Serializer\JsonLDEncoder;
use Bookboon\JsonLDClient\Serializer\JsonLDNormalizer;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\ChildClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\CircularChild;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\CircularParent;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\CircularParentWithId;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\ClassWithObjectProperty;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\DatedClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\DummyClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\DynamicArrayClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\NestedArrayClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\NestedClass;
Expand Down Expand Up @@ -430,6 +432,38 @@ public function testClassWithObjectProperty(): void
$this->assertJsonStringEqualsJsonString($testJson, $serializedJson);
}

public function testPrefersClassInOwnNamespaceRatherThanMapping(): void
{
$dummyClass = new DummyClass;
$dummyClass->setName("dummyclass");
$childClass = new ChildClass;
$childClass->setTitle("childclass");

$dummyClass->setChildClass($childClass);

$serializer = SerializerHelper::create([
new MappingEndpoint(DummyClass::class, '/api/v1/dummies', [
'value' => '@DummyClass'
]),
new MappingEndpoint(DummyClass::class, '/api/v1/childclasses', [
'value' => '@ChildClass'
])
]);
$serializedJson = $serializer->serialize($dummyClass, JsonLDEncoder::FORMAT);

/** @var DummyClass $dummyClass */
$dummyClass = $serializer->deserialize(
$serializedJson,
'',
JsonLDEncoder::FORMAT,
$this->getContextWithMapping(ClassWithObjectProperty::class)
);

$childClass = $dummyClass->getChildClass();
$this->assertNotNull($childClass);
$this->assertEquals('childclass', $childClass->getTitle());
}

private function getContextWithMapping(string $className): array
{
return [
Expand Down

0 comments on commit cb5290e

Please sign in to comment.