Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnyToEnumConverter also triggers on enum > enum mapping #135

Closed
robert-bor opened this issue Jul 17, 2021 · 0 comments
Closed

AnyToEnumConverter also triggers on enum > enum mapping #135

robert-bor opened this issue Jul 17, 2021 · 0 comments
Labels

Comments

@robert-bor
Copy link
Contributor

Problem

When mapping an enum to an enum of the same class, the AnyToEnumConverter is found as a suitable converter.

When AnyToEnumConverter.doConvert kicks in for converting an enum to its own instance, it takes the toString() value of the enum and then applies that value to a valueOf.

When the enum class has a toString method, this conversion will fail.

Diagnosis

See:

    @Override
    protected Enum<?> doConvert(Object source, Class<? extends Enum<?>> targetClass) {
        if (source == null) {
            return null;
        }
        String sourceText = source.toString();
        if (isNotEmpty(sourceText)) {
            return valueOf(targetClass, sourceText);
        }
        return null;
    }

The doConvert always does the conversion, even if both sides are of the same class type.

Suggestion solution

Make sure the source class is checked against the targetClass. When they are the same immediately return the source and abandon the conversion process.

@robert-bor robert-bor added the bug label Jul 17, 2021
robert-bor added a commit that referenced this issue Jul 17, 2021
When performing a conversion from an enum to the same enum, the
AnyToEnumConverter is triggered, which performs a full cycle conversion,
using the toString() method of source and the valueOf() of the target
enum.

When the source enum is the same as the target enum and the source enum
has an overridden toString() method, this fails because the value can
not be mapped to the enum.

Instead, the AnyToEnumConverter now checks whether the source is of the
same enum class as the target. If this is the case, the conversion task
is skipped and the source is returned literally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant