Skip to content

Commit

Permalink
Added max min annotation support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Oct 16, 2019
1 parent b7d17b4 commit 23d28b1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 4 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

**Added travis config.**


[d710f0f986d2a8e](https://github.com/noboomu/proteus/commit/d710f0f986d2a8e) Joshua Bauer *2019-09-17 21:32:50*

**Support for JsonView in OpenAPI.**


Expand Down
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ Proteus Changelog.
<version>1.3.3</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/io/sinistral/proteus/server/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,8 @@ public class MediaType
public static final MediaType APPLICATION_X_RESEARCH_INFO_SYSTEMS = create("application/x-research-info-systems",
"RIS");
public static final MediaType APPLICATION_X_RUBY = create("application/x-ruby", "rb");
public static final MediaType APPLICATION_X_WWW_FORM_URLENCODED = create("application/x-www-form-urlencoded");

public static final MediaType APPLICATION_X_SCILAB = create("application/x-scilab", "sci", "sce");
public static final MediaType APPLICATION_X_SHAR = create("application/x-shar", "shar");
public static final MediaType APPLICATION_X_SHOCKWAVE_FLASH = create("application/x-shockwave-flash", "swf", "swfl");
Expand Down Expand Up @@ -1191,6 +1193,11 @@ public static synchronized MediaType create(String type, String[] attributes, St
{
MediaType mt = new MediaType(type, attributes);

if(fileExtensisons == null)
{
fileExtensisons = new String[]{};
}

if(!Arrays.stream(attributes).anyMatch(a -> a.equals(UTF8_ATTR[0]))) {
for (String ext : fileExtensisons) {
FILE_EXTENSIONS.put(ext, mt);
Expand All @@ -1202,6 +1209,11 @@ public static synchronized MediaType create(String type, String[] attributes, St

public static synchronized MediaType createUTF8(String type, String... fileExtensisons)
{
if(fileExtensisons == null)
{
fileExtensisons = new String[]{};
}

for (String ext : fileExtensisons) {
if(!FILE_EXTENSIONS.containsKey(ext)) {
FILE_EXTENSIONS.put(ext, create(type, fileExtensisons));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
}

} else {

producesContentTypes = Arrays.stream(producesAnnotation.get().value()).flatMap(v -> Arrays.stream((v.split(",")))).collect(Collectors.toList());

producesContentType = producesContentTypes.stream().collect(Collectors.joining(","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.squareup.javapoet.MethodSpec;
import io.sinistral.proteus.server.handlers.HandlerGenerator.StatementParameterType;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -160,10 +163,11 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter,
{
Object[] args = new Object[handler.parameterTypes.length];

String pName = parameter.getName();

for (int i = 0; i < handler.parameterTypes.length; i++) {
if (handler.parameterTypes[i] instanceof StatementParameterType) {
String pName = parameter.getName();


if (parameter.isAnnotationPresent(QueryParam.class)) {
QueryParam qp = parameter.getAnnotation(QueryParam.class);
Expand Down Expand Up @@ -215,6 +219,44 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter,
}

builder.addStatement(handler.statement, args);

Max max = parameter.isAnnotationPresent(Max.class) ? parameter.getAnnotationsByType(Max.class)[0] : null;

Min min = parameter.isAnnotationPresent(Min.class) ? parameter.getAnnotationsByType(Min.class)[0] : null;


if(max != null || min != null)
{
if(max != null && min != null)
{
long maxValue = min.value();
long minValue = min.value();

builder.beginControlFlow("if( $L < $L )", pName, minValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.endControlFlow();
builder.beginControlFlow("else if( $L > $L )", pName, maxValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.endControlFlow();

}
else if(max != null)
{
long maxValue = max.value();

builder.beginControlFlow("if( $L > $L )", pName, maxValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.endControlFlow();
}
else
{
long minValue = min.value();

builder.beginControlFlow("if( $L < $L )", pName, minValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.endControlFlow();
}
}
}

/**
Expand Down Expand Up @@ -298,6 +340,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

}
}

if (isSet && !isOptional) {
try {
Class<?> erasedType = (Class<?>) HandlerGenerator.extractErasedType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
Expand Down Expand Up @@ -390,6 +392,22 @@ public ServerResponse<Void> notFoundError(ServerRequest request, @QueryParam("te

}

@GET
@Path("response/max")
public ServerResponse<ByteBuffer> maxValue(ServerRequest request, @QueryParam("param") @Max(100) Integer param ) throws Exception
{
return response().body(param.toString());

}


@GET
@Path("response/min")
public ServerResponse<ByteBuffer> minValue(ServerRequest request, @QueryParam("param") @Min(10) Integer param ) throws Exception
{
return response().body(param.toString());

}

@GET
@Path("response/error/401")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,34 @@ public void unauthorized()
given().accept(ContentType.JSON).when().get("tests/response/error/401").then().statusCode(401).log().body().content(containsString("Unauthorized"));

}

@Test
public void maxValueError()
{
given().queryParam("param",105).when().get("tests/response/max").then().statusCode(400).log();

}

@Test
public void minValueError()
{
given().queryParam("param",5).when().get("tests/response/min").then().statusCode(400).log();

}
@Test
public void maxValue()
{
given().queryParam("param",50).when().get("tests/response/max").then().statusCode(200).log();

}

@Test
public void minValue()
{
given().queryParam("param",15).when().get("tests/response/min").then().statusCode(200).log();

}


@Test
public void responseComplexParameters()
Expand All @@ -485,7 +513,7 @@ public void responseComplexParameters()
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
String stringValue = "TESTSTRING123!#$";

Map<String, Object> map = given()
Map map = given()



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata:400,700|Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.23.11/swagger-ui.css" rel='stylesheet' />
<link href="{{ basePath }}/swagger-ui.css" rel='stylesheet' />

<style>
Expand Down Expand Up @@ -75,8 +76,8 @@
<div id="swagger-ui"></div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.19.3/swagger-ui-bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.19.3/swagger-ui-standalone-preset.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.23.11/swagger-ui-bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.23.11/swagger-ui-standalone-preset.js"></script>

<script>

Expand Down
Empty file.

0 comments on commit 23d28b1

Please sign in to comment.