Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

415 for invalid Content-Type header, remove redundant Token Filter #1298

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public enum ErrorCode {

INVALID_USAGE_OF_VECTORIZE("`$vectorize` and `$vector` can't be used together"),

INVALID_CONTENT_TYPE_HEADER("Invalid Content-Type header"),

UNSUPPORTED_PROJECTION_PARAM("Unsupported projection parameter"),

UNSUPPORTED_UPDATE_DATA_TYPE("Unsupported update data type"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.core.Response;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -69,6 +70,14 @@ public final class ThrowableToErrorMapper {
return handleDriverException((DriverException) throwable, message);
}

// handle an invalid Content-Type header
if (throwable instanceof NotSupportedException) {
// validate the Content-Type header, 415 if failed
return ErrorCode.INVALID_CONTENT_TYPE_HEADER
.toApiException()
.getCommandResultError(Response.Status.UNSUPPORTED_MEDIA_TYPE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too bad actual incorrect Content-Type seems not available from NotSupportedException -- if it was, would be good to include in exception message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is not included, unfortunately.

}

// handle all other exceptions
return handleUnrecognizedException(throwable, message);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import jakarta.ws.rs.NotAllowedException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.WebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
Expand Down Expand Up @@ -38,6 +39,11 @@ public RestResponse<CommandResult> webApplicationExceptionMapper(WebApplicationE
if (e instanceof NotFoundException) {
return RestResponse.status(RestResponse.Status.NOT_FOUND, commandResult);
}
// Return 415 for invalid Content-Type
if (e instanceof NotSupportedException) {
return RestResponse.status(RestResponse.Status.UNSUPPORTED_MEDIA_TYPE, commandResult);
}

return RestResponse.ok(commandResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ public void regularError() {
.body("errors[0].exceptionClass", is("JsonApiException"));
}

@Test
public void invalidContentType() {
given()
.headers(getHeaders())
.contentType(ContentType.HTML)
.body(
"""
{
"findCollections": { }
}
""")
.when()
.post(NamespaceResource.BASE_PATH, namespaceName)
.then()
.statusCode(415)
.body("errors", is(notNullValue()))
.body("errors[0].message", is("Invalid Content-Type header"))
.body("errors[0].errorCode", is("INVALID_CONTENT_TYPE_HEADER"))
.body("errors[0].exceptionClass", is("JsonApiException"));
}

@Test
public void resourceNotFound() {
String json =
Expand Down