Skip to content

Commit

Permalink
Improve Optional bean parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed May 11, 2021
1 parent aa7f82a commit 24f076f
Show file tree
Hide file tree
Showing 7 changed files with 1,442 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Proteus Changelog.
## Unreleased
### No issue

**Bump dependency versions. Update favicon.**


[4777bce4c516e99](https://github.com/noboomu/proteus/commit/4777bce4c516e99) Joshua Bauer *2021-04-02 18:19:35*

**Update README.md**


[8915888e8190521](https://github.com/noboomu/proteus/commit/8915888e8190521) JL Bauer *2021-03-20 05:19:59*

**Rollback swagger ui version.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,24 @@ public void buildServer()
for (Class<?> controllerClass : registeredControllers)
{

handlerExecutor.execute(() -> {
handlerExecutor.submit(() -> {

try
{

//log.debug("Compiling {}...", controllerClass);
// log.debug("Generating {}...", controllerClass);

HandlerGenerator generator = new HandlerGenerator("io.sinistral.proteus.controllers.handlers", controllerClass);

injector.injectMembers(generator);

routerClasses.add(generator.compileClass());
// log.debug("Compiling {}...", controllerClass);

//log.debug("Compiled {}", controllerClass);
Class<? extends Supplier<RoutingHandler>> routerClass = generator.compileClass();

routerClasses.add(routerClass);

// log.debug("Compiled {}", controllerClass);

} catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public HandlerGenerator(String packageName, Class<?> controllerClass)
* Compiles the generated source into a new {@link Class}
* @return a new {@code Supplier<RoutingHandler>} class
*/
public Class<? extends Supplier<RoutingHandler>> compileClass()
public Class<? extends Supplier<RoutingHandler>> compileClass() throws Exception
{

try
Expand All @@ -137,12 +137,14 @@ public Class<? extends Supplier<RoutingHandler>> compileClass()

log.debug("\n\nGenerated Class Source:\n\n{}", this.sourceString);

return new CachedCompiler(null, null).loadFromJava(packageName + "." + className, this.sourceString);
CachedCompiler cachedCompiler = new CachedCompiler(null, null);

return cachedCompiler.loadFromJava(packageName + "." + className, this.sourceString);

} catch (Exception e)
{
log.error("Failed to compile {}\nSource:\n{}", packageName + "." + className, this.sourceString, e);
return null;
throw e;
}
}

Expand Down Expand Up @@ -711,9 +713,26 @@ else if (handler.equals(TypeHandler.FromStringType))
typeName = typeName.replace("$", ".");
}

String pType = interfaceType != null ? interfaceType + "TypeReference" : typeName + ".class";
if (t.equals(TypeHandler.OptionalModelType))
{
ParameterizedType pType = (ParameterizedType) type;

methodBuilder.addStatement(t.statement, type, p.getName(), pType);
if (type instanceof ParameterizedType)
{
pType = (ParameterizedType) type;
type = pType.getActualTypeArguments()[0];
}

String pTypeName = type.getTypeName() + ".class";

methodBuilder.addStatement(t.statement, type.getTypeName(), p.getName(), io.sinistral.proteus.server.Extractors.Optional.class, pTypeName);

}
else
{
String pType = interfaceType != null ? interfaceType + "TypeReference" : typeName + ".class";
methodBuilder.addStatement(t.statement, type, p.getName(), pType);
}

}
else if (t.equals(TypeHandler.OptionalNamedModelType) || t.equals(TypeHandler.NamedModelType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public enum TypeHandler {
OptionalZonedDateTimeType("$T<$T> $L = $T.zonedDateTime(exchange,$S)", false, Optional.class, java.time.ZonedDateTime.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalOffsetDateTimeType("$T<$T> $L = $T.offsetDateTime(exchange,$S)", false, Optional.class, java.time.OffsetDateTime.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),

OptionalModelType("java.util.Optional<$L> $L = $T.model(exchange,$L)", false, StatementParameterType.LITERAL, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.LITERAL,StatementParameterType.STRING),
OptionalModelType("java.util.Optional<$L> $L = $T.model(exchange,$L)", false, StatementParameterType.LITERAL, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.LITERAL),

OptionalNamedJsonNodeType("$T<$T> $L = $T.namedJsonNode(exchange,$s)", true, Optional.class, com.fasterxml.jackson.databind.JsonNode.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class,StatementParameterType.STRING),
OptionalNamedModelType("java.util.Optional<$L> $L = $T.namedModel(exchange,$L,$S)", false, StatementParameterType.LITERAL, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.LITERAL),
Expand Down Expand Up @@ -588,14 +588,20 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
} else {
try {



Class<?> erasedType = (Class<?>) HandlerGenerator.extractErasedType(type);

if (HandlerGenerator.hasValueOfMethod(erasedType)) {
return OptionalValueOfType;

} else if (HandlerGenerator.hasFromStringMethod(erasedType)) {
return OptionalFromStringType;
}

if(isBeanParam)
{
return OptionalModelType;
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ public ServerResponse<User> responseInnerClassTest(ServerRequest request, @BeanP
{
return response(user).applicationJson();
}

@POST
@Path("response/json/beanparam-optional")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_JSON)
public ServerResponse<User> responseInnerClassOptionalTest(ServerRequest request, @BeanParam Optional<User> user ) throws Exception
{
return response(user.orElse(null)).applicationJson();
}


@GET
Expand Down Expand Up @@ -1028,4 +1037,6 @@ public ServerResponse<Map<String,Object>> multipartUploadMultipleFiles(ServerReq


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ public void responseBeanParam()

}

@Test
public void responseOptionalBeanParam()
{

User model = new User();
model.setId(101L);

given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).when().post("v1/tests/response/json/beanparam-optional").then().statusCode(200).and().body(containsString("101"));

}


@Test
public void responseFutureUser()
{
Expand Down
Loading

0 comments on commit 24f076f

Please sign in to comment.