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

Enum values can not be read from single-element array even with DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS #3655

Closed
AndrejMitrovic opened this issue Nov 8, 2022 · 6 comments
Labels
enum Related to handling of Enum values
Milestone

Comments

@AndrejMitrovic
Copy link

AndrejMitrovic commented Nov 8, 2022

Using Jackson 2.9.9.3.

This issue was carried over from micronaut-core: micronaut-projects/micronaut-core#8215

Example test-case:

package arrayissue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonCreator;

enum MyEnum {
    FOO("FOO"),
    BAR("BAR");

    private String value;

    MyEnum(String value) {
        this.value = value;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        om.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
        System.out.println(om.readValue("\"FOO\"", MyEnum.class));
        System.out.println(om.readValue("[\"FOO\"]", MyEnum.class));
    }

    @JsonCreator
    public static MyEnum fromValue(String text) {
        System.out.println("-- CONVERTING FROM: " + text);
        return MyEnum.FOO;
    }
}

Result:

-- CONVERTING FROM: FOO
FOO
-- CONVERTING FROM: null
FOO
@AndrejMitrovic AndrejMitrovic added the to-evaluate Issue that has been received but not yet evaluated label Nov 8, 2022
@AndrejMitrovic AndrejMitrovic reopened this Nov 8, 2022
@AndrejMitrovic
Copy link
Author

Added version info (2.9.9.3).

@cowtowncoder cowtowncoder added 2.14 and removed to-evaluate Issue that has been received but not yet evaluated labels Nov 8, 2022
@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 8, 2022

2.9.x is ancient version, will need to test against later (2.14.0). For now assuming problem still exists.

Root cause likely due to code not checking there is legit String value; nor considering unwrapping, so would need to add latter at least (and probably otherwise fail if String / number expected, Object/Array encountered).

@cowtowncoder cowtowncoder added the enum Related to handling of Enum values label Nov 8, 2022
@yawkat
Copy link
Member

yawkat commented Nov 14, 2022

@cowtowncoder i think the same happens without UNWRAP_SINGLE_VALUE_ARRAYS, what should be the behavior then? i can work on this since it came up with micronaut.

@cowtowncoder
Copy link
Member

@yawkat sounds good, I appreciate your help here -- it is/was on my short TODO-NEXT list :)

yawkat added a commit to yawkat/jackson-databind that referenced this issue Nov 15, 2022
- it seems that the DoS fix in `BeanDeserializer` incorrectly advanced the parser. I've fixed this, but this is potentially security-sensitive, so please take a careful look if this is right :)
- I also changed `FactoryBasedEnumDeserializer` to handle unwrapping of strings properly. I haven't touched  the error handling when there is another token, that is handled by the branch with the comment `Could argue we should throw an exception but...`. imo that should throw an exception, can we change that in a minor release?

Fixes FasterXML#3655
@cowtowncoder cowtowncoder changed the title Array received as null when expecting an enum value Enum values can not be read from single-element array even with DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS Nov 15, 2022
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Nov 15, 2022
cowtowncoder added a commit that referenced this issue Nov 15, 2022
@yawkat
Copy link
Member

yawkat commented Feb 13, 2023

@cowtowncoder i think you can close this issue

@cowtowncoder
Copy link
Member

Thank you @yawkat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enum Related to handling of Enum values
Projects
None yet
Development

No branches or pull requests

3 participants