Skip to content

Commit

Permalink
Allow generics deserialization by JacksonJrDecoder (#2298)
Browse files Browse the repository at this point in the history
* Allow generics deserialization by JacksonJrDecoder
Fixes #2296

* Update JacksonJrDecoder.java

* Update JacksonCodecTest.java

---------

Co-authored-by: Marvin <[email protected]>
  • Loading branch information
yvasyliev and velo authored Jan 22, 2024
1 parent 6b59a73 commit 4cce0ae
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ public Object decode(Response response, Type type) throws IOException {
}

protected Transformer findTransformer(Response response, Type type) {
if (type instanceof Class) {
return (mapper, reader) -> mapper.beanFrom((Class<?>) type, reader);
}
if (type instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) type).getRawType();
Type[] parameterType = ((ParameterizedType) type).getActualTypeArguments();
Expand All @@ -103,6 +100,11 @@ protected Transformer findTransformer(Response response, Type type) {
if (rawType.equals(Map.class)) {
return (mapper, reader) -> mapper.mapOfFrom((Class<?>) parameterType[1], reader);
}
type = rawType;
}
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
return (mapper, reader) -> mapper.beanFrom(clazz, reader);
}
throw new DecodeException(500, "Cannot decode type: " + type.getTypeName(), response.request());
}
Expand Down
59 changes: 57 additions & 2 deletions jackson-jr/src/test/java/feign/jackson/jr/JacksonCodecTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -16,7 +16,6 @@
import static feign.Util.UTF_8;
import static feign.assertj.FeignAssertions.assertThat;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.jr.ob.JSON;
Expand All @@ -26,6 +25,7 @@
import feign.Response;
import feign.Util;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Arrays;
Expand All @@ -37,7 +37,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class JacksonCodecTest {

Expand Down Expand Up @@ -307,4 +311,55 @@ void notFoundDecodesToEmpty() throws Exception {
.build();
assertThat((byte[]) new JacksonJrDecoder().decode(response, byte[].class)).isEmpty();
}

@ParameterizedTest
@MethodSource("decodeGenericsArguments")
void decodeGenerics(Response response, Type responseType, DataWrapper<?> expectedDataWrapper)
throws IOException {
assertThat(new JacksonJrDecoder().decode(response, responseType))
.isEqualTo(expectedDataWrapper);
}

static class DataWrapper<T> {
private T data;

DataWrapper() {}

DataWrapper(T data) {
this.data = data;
}

public void setData(T data) {
this.data = data;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DataWrapper<?> that = (DataWrapper<?>) o;
return Objects.equals(data, that.data);
}
}

static Stream<Arguments> decodeGenericsArguments() {
Response.Builder responseBuilder =
Response.builder()
.request(
Request.create(
HttpMethod.GET,
"/v1/dummy",
Collections.emptyMap(),
Request.Body.empty(),
null));
return Stream.of(
Arguments.of(
responseBuilder.body("{\"data\":2024}", StandardCharsets.UTF_8).build(),
new TypeReference<DataWrapper<Integer>>() {}.getType(),
new DataWrapper<>(2024)),
Arguments.of(
responseBuilder.body("{\"data\":\"Hello, World!\"}", StandardCharsets.UTF_8).build(),
new TypeReference<DataWrapper<String>>() {}.getType(),
new DataWrapper<>("Hello, World!")));
}
}

0 comments on commit 4cce0ae

Please sign in to comment.