Skip to content

Commit

Permalink
fix: Use serializedName in property mapping exception
Browse files Browse the repository at this point in the history
  • Loading branch information
autaut03 committed Aug 12, 2022
1 parent 79b95b8 commit c62274a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"scripts": {
"test": "./vendor/bin/phpunit",
"test": "./vendor/bin/pest",
"cs-fix": "./vendor/bin/php-cs-fixer fix -v --show-progress=dots",
"phpstan": "./vendor/bin/phpstan analyse"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function serialize(mixed $value): mixed
$this->properties,
true,
fn (BoundClassProperty $property) => PropertyMappingException::rethrow(
$property->reflection,
$property,
fn () => $property->serialize($value)
)
);
Expand All @@ -47,7 +47,7 @@ public function deserialize(mixed $value): mixed
MultipleMappingException::map(
$this->properties,
false,
fn (BoundClassProperty $property) => PropertyMappingException::rethrow($property->reflection, function () use ($object, $property, $value) {
fn (BoundClassProperty $property) => PropertyMappingException::rethrow($property, function () use ($object, $property, $value) {
$property->deserialize($value, $object);
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ public function create(string $typeAdapterType, Type $type, array $attributes, S
return new ClassPropertiesPrimitiveTypeAdapter(
fn () => $this->objectClassFactory->create($reflection->qualifiedName()),
$reflection->properties()->map(function (PropertyReflection $property) use ($serializer, $typeAdapterType, $attributes) {
$attributes = $property->attributes()->toArray();
$attributes = $property->attributes()->all();
$serializedName = $this->namingStrategy->translate($property->name(), $attributes);

return PropertyMappingException::rethrow($property, fn () => new BoundClassProperty(
return PropertyMappingException::rethrow($serializedName, fn () => new BoundClassProperty(
reflection: $property,
typeAdapter: $serializer->adapter(
$typeAdapterType,
$property->type(),
$attributes
),
serializedName: $this->namingStrategy->translate($property->name(), $attributes),
serializedName: $serializedName,
optional: $property->type() instanceof NamedType && $property->type()->name === Optional::class,
hasDefaultValue: $property->hasDefaultValue(),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace GoodPhp\Serialization\TypeAdapter\Primitive\ClassProperties;

use Exception;
use GoodPhp\Reflection\Reflector\Reflection\PropertyReflection;
use GoodPhp\Serialization\TypeAdapter\Exception\CollectionItemMappingException;
use RuntimeException;
use Throwable;
Expand All @@ -17,16 +16,18 @@ public function __construct(
parent::__construct("Could not map property at path '{$path}': {$previous->getMessage()}", 0, $previous);
}

public static function rethrow(PropertyReflection $property, callable $callback): mixed
public static function rethrow(BoundClassProperty|string $serializedName, callable $callback): mixed
{
$serializedName = $serializedName instanceof BoundClassProperty ? $serializedName->serializedName : $serializedName;

try {
return $callback();
} catch (PropertyMappingException $e) {
throw new self($property->name() . '.' . $e->path, $e->getPrevious());
throw new self($serializedName . '.' . $e->path, $e->getPrevious());
} catch (CollectionItemMappingException $e) {
throw new self($property->name() . '.' . $e->key, $e->getPrevious());
throw new self($serializedName . '.' . $e->key, $e->getPrevious());
} catch (Exception $e) {
throw new self($property->name(), $e);
throw new self($serializedName, $e);
}
}
}

0 comments on commit c62274a

Please sign in to comment.