Skip to content

Commit

Permalink
added fix for non-necessary json transformation for ObjectId argument…
Browse files Browse the repository at this point in the history
… value
  • Loading branch information
mskacelik authored and jmartisk committed Nov 6, 2023
1 parent 53ed74a commit f861a69
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ private Object correctObjectClass(Object argumentValue, Field field, DataFetchin
if (Map.class.isAssignableFrom(argumentValue.getClass())) {
return correctComplexObjectFromMap((Map) argumentValue, field, dfe);
} else if (receivedClassName.equals(String.class.getName())) {
// Edge case for ObjectId: If the field is of type org.bson.types.ObjectId, return the argument value.
// We need to handle ObjectId separately to avoid JSON-B calling its no-arg constructor, which would
// generate a new, random ID. By returning the argument value as is, we preserve the original ObjectId.
if (field.getReference().getClassName().equals("org.bson.types.ObjectId")) {
return argumentValue;
}
// We got a String, but not expecting one. Lets bind to Pojo with JsonB
// This happens with @DefaultValue and Transformable (Passthrough) Scalars
return correctComplexObjectFromJsonString(argumentValue.toString(), field);
Expand Down
5 changes: 5 additions & 0 deletions server/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@
<version>${version.smallrye-stork}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-api-maven</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.smallrye.graphql.tests.objectid;

import org.bson.types.ObjectId;

import io.smallrye.graphql.api.Adapter;

public class ObjectIdAdapter implements Adapter<ObjectId, String> {
@Override
public ObjectId from(String o) {
return new ObjectId(o);
}

@Override
public String to(ObjectId a) throws Exception {
return a.toHexString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.smallrye.graphql.tests.objectid;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;

import org.bson.types.ObjectId;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.smallrye.graphql.tests.GraphQLAssured;

@RunWith(Arquillian.class)
@RunAsClient
public class ObjectIdTest {

@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class, "objectId-test.war")
.addClasses(SomeApi.class, ObjectIdAdapter.class);
}

@ArquillianResource
URL testingURL;

@Test
public void queryWithObjectIdArgumentTest() {
final String id = ObjectId.get().toHexString();
System.err.println(id);
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL);

String response = graphQLAssured
.post("{ returnObjectId(id: \"" + id + "\") }");
assertThat(response).contains("{\"data\":{\"returnObjectId\":\"" + id + "\"}}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.smallrye.graphql.tests.objectid;

import org.bson.types.ObjectId;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.graphql.api.AdaptWith;

@GraphQLApi
public class SomeApi {
@Query
public @AdaptWith(ObjectIdAdapter.class) ObjectId returnObjectId(@AdaptWith(ObjectIdAdapter.class) ObjectId id) {
return id;
}

}

0 comments on commit f861a69

Please sign in to comment.