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

Allow generics deserialization by JacksonJrDecoder #2298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,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 @@ -105,6 +102,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
62 changes: 60 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,8 +16,8 @@
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 java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Arrays;
Expand All @@ -29,6 +29,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.jr.ob.JSON;
Expand All @@ -37,6 +39,9 @@
import feign.RequestTemplate;
import feign.Response;
import feign.Util;
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 @@ -289,4 +294,57 @@ 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!")
)
);
}
}