diff --git a/Mapping/MappingCollection.php b/Mapping/MappingCollection.php index 01d2573..3f2567d 100644 --- a/Mapping/MappingCollection.php +++ b/Mapping/MappingCollection.php @@ -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; } } diff --git a/Tests/Fixtures/Models/ChildClass.php b/Tests/Fixtures/Models/ChildClass.php new file mode 100644 index 0000000..c9ba618 --- /dev/null +++ b/Tests/Fixtures/Models/ChildClass.php @@ -0,0 +1,19 @@ +title; + } + + public function setTitle(string $title): void + { + $this->title = $title; + } + +} \ No newline at end of file diff --git a/Tests/Fixtures/Models/DummyClass.php b/Tests/Fixtures/Models/DummyClass.php new file mode 100644 index 0000000..249b74d --- /dev/null +++ b/Tests/Fixtures/Models/DummyClass.php @@ -0,0 +1,30 @@ +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; + } +} diff --git a/Tests/Fixtures/OtherModels/ChildClass.php b/Tests/Fixtures/OtherModels/ChildClass.php new file mode 100644 index 0000000..4d51496 --- /dev/null +++ b/Tests/Fixtures/OtherModels/ChildClass.php @@ -0,0 +1,19 @@ +title; + } + + public function setTitle(?string $title): void + { + $this->title = $title; + } + +} \ No newline at end of file diff --git a/Tests/Serializer/JsonLDNormalizerTest.php b/Tests/Serializer/JsonLDNormalizerTest.php index c8a240e..e2c1abd 100644 --- a/Tests/Serializer/JsonLDNormalizerTest.php +++ b/Tests/Serializer/JsonLDNormalizerTest.php @@ -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; @@ -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 [