Skip to content

Commit

Permalink
Fix Double conversion and add BigDecimal support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Feb 25, 2020
1 parent e684d43 commit d425ee1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,6 +191,10 @@ public static java.util.Optional<Double> doubleValue(final HttpServerExchange ex
return string(exchange, name).map(Double::parseDouble);
}

public static java.util.Optional<BigDecimal> bigDecimalValue(final HttpServerExchange exchange, final String name)
{
return string(exchange, name).map(BigDecimal::new);
}

public static java.util.Optional<Long> longValue(final HttpServerExchange exchange, final String name)
{
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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<Long> $L = $T.floatValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalDoubleType("$T<Integer> $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalFloatType("$T<Float> $L = $T.floatValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalDoubleType("$T<Double> $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalBigDecimalType("$T<BigDecimal> $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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -305,6 +306,29 @@ public ServerResponse<ByteBuffer> timestampConversion( ServerRequest request, @Q


}


@GET
@Path("response/parse/double")
@Blocking
@Produces(MediaType.TEXT_PLAIN)
public ServerResponse<ByteBuffer> 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<ByteBuffer> bigDecimalConversion( ServerRequest request, @QueryParam("value") BigDecimal value ) throws Exception
{
assert (value instanceof BigDecimal);

return response().body(value.toString()).textPlain();
}

@GET
@Path("response/parse/instant")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ else if (StringUtils.isBlank(httpMethod) && subResource != null)

openAPI.setPaths(this.paths);

LOGGER.debug("Completed paths");

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions proteus-openapi/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<logger name="io.netty" level="ERROR" />
<logger name="io.netty.handler" level="ERROR" />
<logger name="io.sinistral.proteus.server.handlers" level="ERROR" />
<logger name="io.sinistral.proteus.services" level="ERROR" />
<logger name="io.sinistral.proteus.openapi" level="ERROR" />
<logger name="io.sinistral.proteus.services" level="DEBUG" />
<logger name="io.sinistral.proteus.openapi" level="DEBUG" />

<logger name="org.javamoney.moneta" level="ERROR" />"

Expand Down

0 comments on commit d425ee1

Please sign in to comment.