Skip to content

Constructor property promotion not working correctly with properties that start with the same characters as the json property #242

@Gerben-T

Description

@Gerben-T

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": "messaging-user@externalservice.com",
	"idType": "email"
}

When running

$mapper->map($json, MyClass::class);

it tries to put "id": "messaging-user@externalservice.com" into $idType which will fail because "messaging-user@externalservice.com" 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions