Skip to content

Commit

Permalink
Support for inner class model parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 18, 2018
1 parent 05084d1 commit 7c2cc52
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/sinistral/proteus/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ public ServerResponse<User> 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<User.InnerUserModel> responseInnerClassTest(ServerRequest request, @BeanParam User.InnerUserModel userInnerModel ) throws Exception
{
return response(userInnerModel).applicationJson();
}


@GET
@Path("/generic/set")
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/sinistral/proteus/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
public class User
{

public static class InnerUserModel {
public Long id;
}

public enum UserType
{
GUEST,MEMBER,ADMIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7c2cc52

Please sign in to comment.