Skip to content

Commit

Permalink
Ensure buffer is not null
Browse files Browse the repository at this point in the history
It helps to identify whether the request payload was not sent by the client or not.
  • Loading branch information
miere committed Mar 6, 2022
1 parent 2147ead commit 60c75ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kos-core/source/kos/api/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Buffer serialize(Object target) {
}

@Override
public <T> T deserialize(Buffer buffer, Class<T> type) {
public <T> T deserialize(@NonNull Buffer buffer, @NonNull Class<T> type) {
return Json.decodeValue( buffer, type );
}
}
Expand Down
10 changes: 10 additions & 0 deletions kos-core/source/kos/core/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.vertx.core.buffer.*;
import io.vertx.ext.web.*;
import kos.api.KosContext;
import kos.core.exception.KosException;
import lombok.*;
import lombok.experimental.*;

Expand All @@ -44,6 +45,8 @@ public <T> T readHeader(KosContext kosContext, RoutingContext context, String na

public <T> T readBody(KosContext kosContext, RoutingContext context, String name, Class<T> type) {
val buffer = context.getBody();
if (buffer == null)
throw new BadRequestException("Cannot read body content: is empty.");
if (Buffer.class.equals(type))
return (T) buffer;
val serializer = kosContext.getPayloadSerializationStrategy().serializerFor(context.request());
Expand All @@ -53,4 +56,11 @@ public <T> T readBody(KosContext kosContext, RoutingContext context, String name
public <T> T readContext(KosContext kosContext, RoutingContext context, String name, Class<T> type) {
return (T) context.get(name);
}

public static class BadRequestException extends KosException {

private BadRequestException(String message) {
super(message);
}
}
}

0 comments on commit 60c75ab

Please sign in to comment.