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

error out with invalid option when createCollection #832

Merged
merged 5 commits into from
Jan 25, 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package io.stargate.sgv2.jsonapi.api.configuration;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import java.io.IOException;

public class CommandObjectMapperHandler extends DeserializationProblemHandler {

@Override
public boolean handleUnknownProperty(
DeserializationContext ctxt,
JsonParser p,
JsonDeserializer<?> deserializer,
Object beanOrClass,
String propertyName)
throws IOException {
if (deserializer.handledType().toString().endsWith("CreateCollectionCommand$Options")) {
throw ErrorCode.INVALID_CREATE_COLLECTION_OPTIONS.toApiException(
"No option \"%s\" found as createCollectionCommand option (known options: \"indexing\", \"vector\")",
propertyName);
}
// false means if not matched by above handle logic, object mapper will
// FAIL_ON_UNKNOWN_PROPERTIES.
return false;
}

@Override
public JavaType handleUnknownTypeId(
DeserializationContext ctxt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum ErrorCode {
/** Command error codes. */
COUNT_READ_FAILED("Unable to count documents"),
COMMAND_NOT_IMPLEMENTED("The provided command is not implemented."),
INVALID_CREATE_COLLECTION_OPTIONS("The provided options are invalid"),
NO_COMMAND_MATCHED("Unable to find the provided command"),
COMMAND_ACCEPTS_NO_OPTIONS("Command accepts no options"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,36 @@ public void failWithInvalidNameInIndexingDeny() {
.body("errors[0].errorCode", is("INVALID_INDEXING_DEFINITION"))
.body("errors[0].exceptionClass", is("JsonApiException"));
}

@Test
public void failWithInvalidOption() {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"createCollection": {
"name": "collection_with_invalid_option",
"options" : {
"InDex" : {}
}
}
}
""")
.when()
.post(NamespaceResource.BASE_PATH, namespaceName)
.then()
.statusCode(200)
.body("status", is(nullValue()))
.body("data", is(nullValue()))
.body(
"errors[0].message",
startsWith(
"The provided options are invalid: No option \"InDex\" found as createCollectionCommand option (known options: \"indexing\", \"vector\")"))
.body("errors[0].errorCode", is("INVALID_CREATE_COLLECTION_OPTIONS"))
.body("errors[0].exceptionClass", is("JsonApiException"));
}
}

private void deleteCollection(String collectionName) {
Expand Down
Loading