Skip to content

AbstractJackson2Decoder breaks Custom Deserializers [SPR-15975] #20526

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

Closed
spring-projects-issues opened this issue Sep 18, 2017 · 1 comment
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Johannes Edmeier opened SPR-15975 and commented

When using a custom Jackson Deserializer the following test throws a java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization

Imho the AbstractJackson2Decoder should call tokenBuffer.asParser(getObjectMapper()) instead of tokenBuffer.asParser() since the used NonBlockingJsonParser will always return null for getCodec().
(This solution does work for me)

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.io.IOException;
import org.junit.Test;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import static org.assertj.core.api.Assertions.assertThat;

public class Jackson2DecoderTest {

    @Test
    public void test() {
        DataBuffer buffer = new DefaultDataBufferFactory().wrap("{\"test\": 1}".getBytes());

        Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(new ObjectMapper());
        Flux<TestObject> decoded = decoder.decode(Mono.just(buffer), ResolvableType.forClass(TestObject.class), null,
                null).cast(TestObject.class);

        StepVerifier.create(decoded).assertNext(v -> assertThat(v.getTest()).isEqualTo(1)).verifyComplete();
    }

    @JsonDeserialize(using = Deserializer.class)
    public static class TestObject {
        private int test;
        public int getTest() {
            return test;
        }
        public void setTest(int test) {
            this.test = test;
        }
    }
    
    public static class Deserializer extends StdDeserializer<TestObject> {
        protected Deserializer() {
            super(TestObject.class);
        }

        @Override
        public TestObject deserialize(JsonParser p,
                                      DeserializationContext ctxt) throws IOException, JsonProcessingException {
            JsonNode node = p.readValueAsTree();
            TestObject result = new TestObject();
            result.setTest(node.get("test").asInt());
            return result;
        }
    }
}

Affects: 5.0 RC4

Referenced from: commits 74120ef

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Thanks for this great issue with fix and test :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants