From d425ee1da2fa5a7fff9d043f0f3edd07fdede501 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Mon, 24 Feb 2020 16:54:22 -0800 Subject: [PATCH] Fix Double conversion and add BigDecimal support. --- CHANGELOG.md | 5 +++ .../sinistral/proteus/server/Extractors.java | 10 +++++ .../proteus/server/handlers/TypeHandler.java | 20 ++++++--- .../proteus/test/controllers/Tests.java | 24 +++++++++++ .../test/server/TestControllerEndpoints.java | 41 +++++++++++++++++++ .../proteus/openapi/jaxrs2/Reader.java | 2 + .../src/test/resources/logback-test.xml | 4 +- 7 files changed, 98 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf76069..d17760d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Cleanup poms and improve redirect.** + + +[e684d4388b28ed7](https://github.com/noboomu/proteus/commit/e684d4388b28ed7) Joshua Bauer *2020-02-14 00:28:13* + **Redirect improvements.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java index 37ce1c2..5d70ee4 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.Method; +import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; @@ -190,6 +191,10 @@ public static java.util.Optional doubleValue(final HttpServerExchange ex return string(exchange, name).map(Double::parseDouble); } + public static java.util.Optional bigDecimalValue(final HttpServerExchange exchange, final String name) + { + return string(exchange, name).map(BigDecimal::new); + } public static java.util.Optional longValue(final HttpServerExchange exchange, final String name) { @@ -389,6 +394,11 @@ public static Double doubleValue(final HttpServerExchange exchange, final String return Double.parseDouble(string(exchange, name)); } + public static BigDecimal bigDecimalValue(final HttpServerExchange exchange, final String name) + { + return new BigDecimal(string(exchange, name)); + } + public static Long longValue(final HttpServerExchange exchange, final String name) throws IllegalArgumentException { diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java index 8e480ab..e9265ca 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java @@ -13,6 +13,7 @@ import java.lang.reflect.Parameter; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.math.BigDecimal; import java.util.Arrays; import java.util.Optional; @@ -47,8 +48,9 @@ public enum TypeHandler InstantType("$T $L = $T.instant(exchange,$S)", false, java.time.Instant.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), - FloatType("Integer $L = $T.floatValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), - DoubleType("Integer $L = $T.doubleValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), + FloatType("Float $L = $T.floatValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), + DoubleType("Double $L = $T.doubleValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), + BigDecimalType("BigDecimal $L = $T.bigDecimalValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), ValueOfType("$T $L = $T.valueOf($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), FromStringType("$T $L = $T.fromString($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), @@ -97,8 +99,10 @@ public enum TypeHandler OptionalFileType("$T<$T> $L = $T.file(exchange,$S)", true, Optional.class, java.io.File.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), - OptionalFloatType("$T $L = $T.floatValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), - OptionalDoubleType("$T $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), + OptionalFloatType("$T $L = $T.floatValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), + OptionalDoubleType("$T $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), + OptionalBigDecimalType("$T $L = $T.bigDecimalValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), + OptionalDateType("$T<$T> $L = $T.date(exchange,$S)", false, Optional.class, java.util.Date.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), OptionalInstantType("$T<$T> $L = $T.instant(exchange,$S)", false, Optional.class, java.time.Instant.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), @@ -446,7 +450,9 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) return FloatType; } else if (type.equals(Double.class)) { return DoubleType; - } else if (type.equals(java.nio.ByteBuffer.class)) { + }else if (type.equals(BigDecimal.class)) { + return BigDecimalType; + } else if (type.equals(java.nio.ByteBuffer.class)) { return ByteBufferType; } else if (type.equals(Boolean.class)) { return BooleanType; @@ -487,7 +493,9 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) return OptionalFloatType; } else if (type.getTypeName().contains("java.lang.Double")) { return OptionalDoubleType; - } else if (type.getTypeName().contains("java.lang.Integer")) { + } else if (type.getTypeName().contains("java.math.BigDecimal")) { + return OptionalBigDecimalType; + }else if (type.getTypeName().contains("java.lang.Integer")) { return OptionalIntegerType; } else if (type.getTypeName().contains("java.nio.file.Path")) { return OptionalFilePathType; diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index e335af9..4329e3a 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -7,6 +7,7 @@ import static io.sinistral.proteus.test.wrappers.TestWrapper.DEBUG_TEST_KEY; import java.io.File; +import java.math.BigDecimal; import java.nio.ByteBuffer; import java.sql.Timestamp; import java.time.Instant; @@ -305,6 +306,29 @@ public ServerResponse timestampConversion( ServerRequest request, @Q } + + + @GET + @Path("response/parse/double") + @Blocking + @Produces(MediaType.TEXT_PLAIN) + public ServerResponse doubleConversion( ServerRequest request, @QueryParam("value") Double value ) throws Exception + { + assert (value instanceof Double); + + return response().body(value.toString()).textPlain(); + } + + @GET + @Path("response/parse/big-decimal") + @Blocking + @Produces(MediaType.TEXT_PLAIN) + public ServerResponse bigDecimalConversion( ServerRequest request, @QueryParam("value") BigDecimal value ) throws Exception + { + assert (value instanceof BigDecimal); + + return response().body(value.toString()).textPlain(); + } @GET @Path("response/parse/instant") diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java index 15be39a..43569f1 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java @@ -12,6 +12,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; +import java.math.BigDecimal; import java.nio.file.Files; import java.sql.Timestamp; import java.time.Instant; @@ -468,6 +469,46 @@ public void responseParseTimestamp() } + @Test + public void responseParseBigDecimal() + { + + + BigDecimal value = new BigDecimal(23234.34); + + given() + + + + .queryParam("value", value.toString()) + + .when() + + .get("tests/response/parse/big-decimal"). + + then().statusCode(200).and().body(containsString(value.toString())); + } + + @Test + public void responseParseDouble() + { + + + Double value = 23234.34; + + given() + + + + .queryParam("value", Double.toString(value)) + + .when() + + .get("tests/response/parse/double"). + + then().statusCode(200).and().body(containsString(value.toString())); + } + @Test public void notFound() { diff --git a/proteus-openapi/src/main/java/io/sinistral/proteus/openapi/jaxrs2/Reader.java b/proteus-openapi/src/main/java/io/sinistral/proteus/openapi/jaxrs2/Reader.java index f2216c6..b559eb5 100644 --- a/proteus-openapi/src/main/java/io/sinistral/proteus/openapi/jaxrs2/Reader.java +++ b/proteus-openapi/src/main/java/io/sinistral/proteus/openapi/jaxrs2/Reader.java @@ -807,6 +807,8 @@ else if (StringUtils.isBlank(httpMethod) && subResource != null) openAPI.setPaths(this.paths); + LOGGER.debug("Completed paths"); + } } } diff --git a/proteus-openapi/src/test/resources/logback-test.xml b/proteus-openapi/src/test/resources/logback-test.xml index 733e338..41be4d2 100644 --- a/proteus-openapi/src/test/resources/logback-test.xml +++ b/proteus-openapi/src/test/resources/logback-test.xml @@ -17,8 +17,8 @@ - - + + "