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

Unwrapping of null value objects [DATAREST-1175] #1541

Open
spring-projects-issues opened this issue Jan 12, 2018 · 0 comments
Open

Unwrapping of null value objects [DATAREST-1175] #1541

spring-projects-issues opened this issue Jan 12, 2018 · 0 comments
Assignees
Labels

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jan 12, 2018

Patrik Mihalcin opened DATAREST-1175 and commented

Hi..
Let's say I have value object:

@Data
public class Description {
    @Column(name = "DESCRIPTION")
    private final String description;
}

and entity:

@Entity
public class Product extends AbstractEntity<Long> {

    @JsonUnwrapped
    private final Description description; 

    public Product (Description description) {
        this.description = description;
    }

    protected Specification() {
        this.description = new Description(null);
    }
}

and custom Description serializer:

static class DescriptionSerializer extends StdSerializer<Description> {
    DescriptionSerializer() {
        super(Description.class);
    }

    @Override
    public void serialize(Description value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        if (value != null) {
            jgen.writeString(value.getDescription());
        } else {
            jgen.writeNull();
        }
    }
}

In a database, I have null in DESCRIPTION column of PRODUCT table

When I put breakpoint here:

Iterable<?> results = pageable.getPageable() != null ? invoker.invokeFindAll(pageable.getPageable())

and inside:

protected Specification() {
        this.description = new Description(null);
}

I can see that JPA/Hibernate/Spring Data invokes this empty constructor and Description value object is initialized with {{null}, i.e. Description(null).

When I step forward and RepositoryInvoker finds all results using Pageable, description value object fields in Product entity are set to null instead of Description(null).

Why is that?
Is is possible to have it as I describe above?

The side effect of this is that my custom DescriptionSerializer
is never invoked because of how Jackson works internally, UnwrappingBeanPropertyWriter doesn't unwrap null value objects.

See here for the reference: https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java#L89

So to sum it up:
null value objects are not unwrapped properly using Jackson and I don't see expected JSON content:

{"description" : null}

what leads to inconsistent REST API where description is omitted completely.

Frontend clients written in React are sensitive to this behaviour.


No further details from DATAREST-1175

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

2 participants