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

added fix for non-necessary json transformation for ObjectId argument value #1951

Merged
merged 1 commit into from
Nov 6, 2023
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
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;
}

}
Loading