Skip to content

Commit

Permalink
MetaResolver: conflicting field name errors use relative property names
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Nov 26, 2024
1 parent 5b31785 commit 18ca492
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
22 changes: 20 additions & 2 deletions src/Meta/MetaResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Orisai\ObjectMapper\Modifiers\RequiresDependenciesModifier;
use Orisai\ObjectMapper\Processing\ObjectCreator;
use Orisai\ObjectMapper\Rules\RuleManager;
use Orisai\ReflectionMeta\Structure\PropertyStructure;
use Orisai\SourceMap\ClassSource;
use Orisai\SourceMap\PropertySource;
use ReflectionClass;
Expand Down Expand Up @@ -480,10 +481,12 @@ private function checkFieldNames(ReflectionClass $rootClass, CompileMeta $meta):

$collidingPropertyStructure = $map[$fieldName] ?? null;
if ($collidingPropertyStructure !== null) {
$propertyName = $this->getRelativePropertyName($propertyStructure, $rootClass);
$collidingPropertyName = $this->getRelativePropertyName($collidingPropertyStructure, $rootClass);

$message = Message::create()
->withContext("Resolving metadata of mapped object '{$rootClass->getName()}'.")
->withProblem("Properties '{$propertyStructure->getSource()->toString()}'"
. " and '{$collidingPropertyStructure->getSource()->toString()}'"
->withProblem("Properties '$propertyName' and '$collidingPropertyName'"
. " have conflicting field name '$fieldName'.")
->withSolution('Define unique field name for each mapped property.');

Expand All @@ -495,4 +498,19 @@ private function checkFieldNames(ReflectionClass $rootClass, CompileMeta $meta):
}
}

/**
* @param ReflectionClass<MappedObject> $rootClass
*/
private function getRelativePropertyName(PropertyStructure $propertyStructure, ReflectionClass $rootClass): string
{
$property = $propertyStructure->getSource()->getReflector();
$class = $property->getDeclaringClass();

if ($class->getName() === $rootClass->getName()) {
return '$' . $property->getName();
}

return $class->getName() . '->$' . $property->getName();
}

}
18 changes: 5 additions & 13 deletions tests/Unit/Meta/MetaResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ public function testMultipleIdenticalFieldNames(): void
<<<'TXT'
Context: Resolving metadata of mapped object
'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO'.
Problem: Properties
'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO->$property2'
and
'Tests\Orisai\ObjectMapper\Doubles\Invalid\MultipleIdenticalFieldNamesVO->$property1'
have conflicting field name 'field'.
Problem: Properties '$property2' and '$property1' have conflicting field name
'field'.
Solution: Define unique field name for each mapped property.
TXT,
);
Expand All @@ -149,11 +146,8 @@ public function testFieldNameIdenticalWithAnotherPropertyName(): void
<<<'TXT'
Context: Resolving metadata of mapped object
'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO'.
Problem: Properties
'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO->$property'
and
'Tests\Orisai\ObjectMapper\Doubles\Invalid\FieldNameIdenticalWithAnotherPropertyNameVO->$field'
have conflicting field name 'field'.
Problem: Properties '$property' and '$field' have conflicting field name
'field'.
Solution: Define unique field name for each mapped property.
TXT,
);
Expand All @@ -171,9 +165,7 @@ public function testMultipleIdenticalPropertyNames(): void
<<<'TXT'
Context: Resolving metadata of mapped object
'Tests\Orisai\ObjectMapper\Doubles\Invalid\ChildCollidingFieldVO'.
Problem: Properties
'Tests\Orisai\ObjectMapper\Doubles\Invalid\ChildCollidingFieldVO->$property'
and
Problem: Properties '$property' and
'Tests\Orisai\ObjectMapper\Doubles\FieldNames\ParentFieldVO->$property'
have conflicting field name 'property'.
Solution: Define unique field name for each mapped property.
Expand Down

0 comments on commit 18ca492

Please sign in to comment.