From 7c2cc52e1e0a75066a81965a23c09b7ff5e8d4e1 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Wed, 18 Jul 2018 13:51:21 -0700 Subject: [PATCH] Support for inner class model parsing. --- .../server/handlers/HandlerGenerator.java | 27 ++++++++++++++----- .../sinistral/proteus/controllers/Tests.java | 10 +++++++ .../io/sinistral/proteus/models/User.java | 4 +++ .../server/TestControllerEndpoints.java | 17 +++++++++--- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java b/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java index 83a9514..1b3e34b 100644 --- a/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java +++ b/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java @@ -890,7 +890,6 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class)) }).distinct().collect(Collectors.toMap(java.util.function.Function.identity(), HandlerGenerator::typeReferenceNameForType)); - log.debug("parameterizedLiteralsNameMap: " + parameterizedLiteralsNameMap); parameterizedLiteralsNameMap.forEach((t, n) -> initBuilder.addStatement("final $T<$L> $LTypeReference = new $T<$L>(){}", TypeReference.class, t, n, TypeReference.class, t)); literalsNameMap.forEach((t, n) -> initBuilder.addStatement("final $T<$T> $LTypeReference = new $T<$T>(){}", TypeReference.class, t, n, TypeReference.class, t)); @@ -1103,6 +1102,8 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class)) } } + log.debug("parameterizedLiteralsNameMap: " + parameterizedLiteralsNameMap); + Arrays.stream(m.getParameters()).forEachOrdered(p -> { Type type = p.getParameterizedType(); @@ -1191,24 +1192,36 @@ else if (handler.equals(TypeHandler.FromStringType)) TypeHandler t = TypeHandler.forType(type, isBeanParameter); + log.debug("beanParam handler: " + t); + if (t.equals(TypeHandler.OptionalModelType) || t.equals(TypeHandler.ModelType)) { String interfaceType = parameterizedLiteralsNameMap.get(type); - String pType = interfaceType != null ? interfaceType + "TypeReference" : type.getTypeName() + ".class"; + String typeName = type.getTypeName(); + + if(typeName.indexOf("$") > -1) + { + typeName = typeName.replace("$", "."); + } + + String pType = interfaceType != null ? interfaceType + "TypeReference" : typeName + ".class"; methodBuilder.addStatement(t.statement, type, p.getName(), pType); } - - - - else if (t.equals(TypeHandler.BeanListFromStringType) || t.equals(TypeHandler.BeanListValueOfType)) { String interfaceType = parameterizedLiteralsNameMap.get(type); - String pType = interfaceType != null ? interfaceType + "TypeReference" : type.getTypeName() + ".class"; + String typeName = type.getTypeName(); + + if(typeName.indexOf("$") > -1) + { + typeName = typeName.replace("$", "."); + } + + String pType = interfaceType != null ? interfaceType + "TypeReference" : typeName + ".class"; methodBuilder.addStatement(t.statement, type, p.getName(), pType); diff --git a/src/test/java/io/sinistral/proteus/controllers/Tests.java b/src/test/java/io/sinistral/proteus/controllers/Tests.java index 4e6a76c..fd4b76f 100644 --- a/src/test/java/io/sinistral/proteus/controllers/Tests.java +++ b/src/test/java/io/sinistral/proteus/controllers/Tests.java @@ -193,6 +193,16 @@ public ServerResponse responseEchoJson(ServerRequest request, @FormParam(" return response(user).applicationJson(); } + @POST + @Path("/response/json/innerClass") + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Consumes("*/*") + @ApiOperation(value = "Echo json inner class endpoint", httpMethod = "POST" ) + public ServerResponse responseInnerClassTest(ServerRequest request, @BeanParam User.InnerUserModel userInnerModel ) throws Exception + { + return response(userInnerModel).applicationJson(); + } + @GET @Path("/generic/set") diff --git a/src/test/java/io/sinistral/proteus/models/User.java b/src/test/java/io/sinistral/proteus/models/User.java index d9cbb78..1a2b66d 100644 --- a/src/test/java/io/sinistral/proteus/models/User.java +++ b/src/test/java/io/sinistral/proteus/models/User.java @@ -11,6 +11,10 @@ public class User { + public static class InnerUserModel { + public Long id; + } + public enum UserType { GUEST,MEMBER,ADMIN diff --git a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java index b4ad69d..55532c8 100644 --- a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java +++ b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java @@ -137,13 +137,24 @@ public void responsePlaintext() } @Test - public void responseEchoUser() + public void responseEchoModel() { - User user = new User(101L,UserType.ADMIN); + User model = new User(101L,UserType.ADMIN); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(user).log().all().when().post("tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().all().when().post("tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); } + + @Test + public void responseInnerClassModel() + { + User.InnerUserModel model = new User.InnerUserModel(); + model.id = 101L; + + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).log().all().when().post("tests/response/json/innerClass").then().statusCode(200).and().body(containsString("101")); + + } + @Test public void responseFutureUser()