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

@JsonUnwrapped doesn't work for @JsonCreator objects #3754

Closed
jakub-bochenski opened this issue Jan 24, 2023 · 5 comments
Closed

@JsonUnwrapped doesn't work for @JsonCreator objects #3754

jakub-bochenski opened this issue Jan 24, 2023 · 5 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@jakub-bochenski
Copy link

jakub-bochenski commented Jan 24, 2023

Describe the bug
This is different than #1497

The problem is that apparently you cannot use a @JsonCreator class as a subject of @JsonUnwrapped annotation

Version information
Which Jackson version(s) was this for?
2.14.1

To Reproduce


    public static final class ClassificationCatalog {
        private final String code, catalogId, version;

        @java.beans.ConstructorProperties({"code", "catalogId", "version"})
        public ClassificationCatalog(String code, String catalogId, String version) {
            this.code = code;
            this.catalogId = catalogId;
            this.version = version;
        }

        public String getCode() {
            return this.code;
        }

        public String getCatalogId() {
            return this.catalogId;
        }

        public String getVersion() {
            return this.version;
        }
    }

    public static class ClassificationCatalogMapping {
        String key;
        @JsonUnwrapped
        ClassificationCatalog value;

        public ClassificationCatalogMapping() {
        }

        public String getKey() {
            return this.key;
        }

        public ClassificationCatalog getValue() {
            return this.value;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public void setValue(ClassificationCatalog value) {
            this.value = value;
        }
    }

Trying to deserialize this from

	{
						"key" : "Upc",
						"code": "1095",
						"catalogId": "ElectronicsClassification",
						"version": "1.0"
					}

will result in com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "code"

Expected behavior
Data is deserialized

@jakub-bochenski jakub-bochenski added the to-evaluate Issue that has been received but not yet evaluated label Jan 24, 2023
@pjfanning
Copy link
Member

You need to provide a Java only example. We do not generally accept Lombok examples. Jackson-databind only has a small number of dependencies and there is no plan to add Lombok to the mix.

@cowtowncoder
Copy link
Member

Note: maybe this is the same as #1467 (ultimately), as well as a few issues reported wrt @JsonUnwrapped trying to set Fields -- which I think it is -- the answer really is that @JsonUnwrapped ONLY works with Setter methods and Fields.

And Records being immutable it is currently impossible to use it with Records.

@jakub-bochenski
Copy link
Author

I've updated the example to plain java

@cowtowncoder I think the docs are rather unclear

Also note that annotation only applies if:
Value is serialized as an Object valie (can not unwrap Array values using this mechanism)
Reading/writing is done using Jackson standard BeanDeserializer / BeanSerializer; or custom deserializer/serializer MUST explicitly support similar operation.
Will not work with polymorphic type handling ("polymorphic deserialization")

@yihtserns
Copy link
Contributor

yihtserns commented Jan 31, 2023

@jakub-bochenski maybe you can upload a sample repo to Github to demonstrate the issue - I'm not able to reproduce it on my side.

@jakub-bochenski
Copy link
Author

Hey, you are right. It's failing because somebody had the bright idea to install this modifier:

public class JacksonDeserializerModifier extends BeanDeserializerModifier {

    @Override
    public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
        if (deserializer instanceof BeanDeserializer) {
            return new JobValidationDeserializer((BeanDeserializer) deserializer);
        }
        return deserializer;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

4 participants