Skip to content
This repository has been archived by the owner on Jan 16, 2020. It is now read-only.

Added support for generics value in response #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -12,6 +12,7 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.type.AnnotationMetadata;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class ApiClientMethodInterceptor implements MethodInterceptor, Initializi
@Setter
private AnnotationMetadata annotationMetadata;

@Autowired
private ObjectMapper objectMapper;

private List<ApiBodyResolver> bodyResolvers;

private WebClient webClient;
Expand All @@ -67,7 +71,7 @@ public void afterPropertiesSet() throws Exception {
}

private void prepareAttribute() {
metadata = new RequestMappingMetadataBuilder(applicationContext, type, name, annotationMetadata)
metadata = new RequestMappingMetadataBuilder(applicationContext, type, name, annotationMetadata, objectMapper)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.blibli.oss.apiclient.properties.ApiClientProperties;
import com.blibli.oss.apiclient.properties.PropertiesHelper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.type.AnnotationMetadata;
Expand Down Expand Up @@ -53,11 +54,18 @@ public class RequestMappingMetadataBuilder {

private AnnotationMetadata annotationMetadata;

public RequestMappingMetadataBuilder(ApplicationContext applicationContext, Class<?> type, String name, AnnotationMetadata annotationMetadata) {
private ObjectMapper objectMapper;

public RequestMappingMetadataBuilder(ApplicationContext applicationContext,
Class<?> type,
String name,
AnnotationMetadata annotationMetadata,
ObjectMapper objectMapper) {
this.applicationContext = applicationContext;
this.type = type;
this.name = name;
this.annotationMetadata = annotationMetadata;
this.objectMapper = objectMapper;
}

private void prepareProperties() {
Expand Down Expand Up @@ -124,7 +132,9 @@ private void prepareRequestBodyClasses() {
}

try {
responseBodyClasses.put(methodName, Class.forName(typeArguments[0].getTypeName()));
Type type = typeArguments[0];
String className = objectMapper.constructType(type).getRawClass().getCanonicalName();
responseBodyClasses.put(methodName, Class.forName(className));
} catch (ClassNotFoundException e) {
throw new BeanCreationException(e.getMessage(), e);
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/blibli/oss/apiclient/ExampleClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.blibli.oss.apiclient.client.ExampleClient;
import com.blibli.oss.apiclient.client.model.FirstRequest;
import com.blibli.oss.apiclient.client.model.FirstResponse;
import com.blibli.oss.apiclient.client.model.GenericResponse;
import com.blibli.oss.apiclient.client.model.SecondResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -19,6 +20,9 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Collections;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -30,6 +34,9 @@ public class ExampleClientTest {
public static final FirstResponse FIRST_RESPONSE = FirstResponse.builder()
.hello("Hello Eko Kurniawan")
.build();
public static final GenericResponse<String> GENERIC_RESPONSE = GenericResponse.<String>builder()
.value("Test Generics")
.build();
public static final SecondResponse SECOND_RESPONSE = SecondResponse.builder().hello("Hello").build();
private static WireMockServer wireMockServer;

Expand Down Expand Up @@ -161,6 +168,42 @@ void testSixth() throws JsonProcessingException {
assertEquals(FIRST_RESPONSE, response);
}

@Test
void testGenerics() throws JsonProcessingException {
wireMockServer.stubFor(
post(urlPathEqualTo("/generics"))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE))
.withHeader(HttpHeaders.ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE))
.withRequestBody(equalTo("testing"))
.willReturn(
aResponse()
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody(objectMapper.writeValueAsString(GENERIC_RESPONSE))
)
);

GenericResponse<String> response = exampleClient.generic("testing").block();
assertEquals(GENERIC_RESPONSE, response);
}

@Test
void testGenericsTwo() throws JsonProcessingException {
wireMockServer.stubFor(
post(urlPathEqualTo("/generics-two"))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE))
.withHeader(HttpHeaders.ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE))
.withRequestBody(equalTo("testing"))
.willReturn(
aResponse()
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody(objectMapper.writeValueAsString(Collections.singletonList("testing")))
)
);

List<String> response = exampleClient.genericTwo("testing").block();
assertEquals(Collections.singletonList("testing"), response);
}

@AfterAll
static void afterAll() {
wireMockServer.stop();
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/blibli/oss/apiclient/client/ExampleClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.blibli.oss.apiclient.annotation.ApiClient;
import com.blibli.oss.apiclient.client.model.FirstRequest;
import com.blibli.oss.apiclient.client.model.FirstResponse;
import com.blibli.oss.apiclient.client.model.GenericResponse;
import com.blibli.oss.apiclient.client.model.SecondResponse;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@ApiClient(
name = "exampleClient",
fallback = ExampleClientFallback.class
Expand Down Expand Up @@ -64,4 +67,20 @@ Mono<FirstResponse> forth(@PathVariable("userId") String userId,
)
Mono<FirstResponse> sixth(@RequestPart("file") Resource file);

@RequestMapping(
method = RequestMethod.POST,
path = "/generics",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
Mono<GenericResponse<String>> generic(@RequestBody String test);

@RequestMapping(
method = RequestMethod.POST,
path = "/generics-two",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
Mono<List<String>> genericTwo(@RequestBody String test);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.blibli.oss.apiclient.client.model.FirstRequest;
import com.blibli.oss.apiclient.client.model.FirstResponse;
import com.blibli.oss.apiclient.client.model.GenericResponse;
import com.blibli.oss.apiclient.client.model.SecondResponse;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

@Component
public class ExampleClientFallback implements ExampleClient {

Expand Down Expand Up @@ -52,4 +56,16 @@ public Mono<FirstResponse> sixth(Resource file) {
.hello("Ups Sixth")
.build());
}

@Override
public Mono<GenericResponse<String>> generic(String test) {
return Mono.just(GenericResponse.<String>builder()
.value("Ups Generic")
.build());
}

@Override
public Mono<List<String>> genericTwo(String test) {
return Mono.just(Collections.singletonList("Ups Generic Two"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.blibli.oss.apiclient.client.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GenericResponse<T> {

private T value;
}