jsonmapper
jsonmapper copied to clipboard
Constructor property promotion not working correctly with properties that start with the same characters as the json property
Running netresearch/jsonmapper: 4.5.0
Consider the following class
/**
* @param string $firstName First name of the recipient.
* @param string $lastName Last name of the recipient.
* @param IdType $idType The recipient ID type. This is used to indicate the format used for the ID.
* @param string $nickname Nickname or display name of the recipient.
* @param string $id The recipient ID specific to the provider.
*/
public function __construct(
public string $firstName,
public string $lastName,
public IdType $idType,
public string $nickname,
public string $id
) {}
And this is my json payload
{
"id": "[email protected]",
"idType": "email"
}
When running
$mapper->map($json, MyClass::class);
it tries to put "id": "[email protected]" into $idType which will fail because "[email protected]" is not a valid value for enum IdType.
This is because of the following check https://github.com/cweiske/jsonmapper/blob/v4.5.0/src/JsonMapper.php#L580-L588
It seems to match $id to $idType because it starts with $id which is wrong in this case.
It should check if the whole variable name matches, so that
"$id" does not match "$idType".
Another thing is if my docblock contains $id somewhere in the description it would still match
@param string $firstName First name of the recipient. And some reference to $id maybe?
This would match for the json key id.