Skip to content

Commit

Permalink
Add support for @JsonbNillable
Browse files Browse the repository at this point in the history
Fixes #2253
  • Loading branch information
jmini authored and jmartisk committed Jan 14, 2025
1 parent c790847 commit 50db01b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import java.util.Optional;

import jakarta.json.bind.annotation.JsonbNillable;
import jakarta.json.bind.annotation.JsonbProperty;

import org.eclipse.microprofile.graphql.Name;
Expand All @@ -25,11 +26,16 @@ public class FieldInfo {
FieldInfo(TypeInfo container, Field field) {
this.container = container;
this.field = field;
JsonbProperty jsonbPropertyAnnotation = field.getAnnotation(JsonbProperty.class);
if (jsonbPropertyAnnotation != null) {
includeIfNull = jsonbPropertyAnnotation.nillable();
JsonbNillable jsonbNillableAnnotation = field.getAnnotation(JsonbNillable.class);
if (jsonbNillableAnnotation != null) {
this.includeIfNull = true;
} else {
includeIfNull = false;
JsonbProperty jsonbPropertyAnnotation = field.getAnnotation(JsonbProperty.class);
if (jsonbPropertyAnnotation != null) {
this.includeIfNull = jsonbPropertyAnnotation.nillable();
} else {
this.includeIfNull = false;
}
}
this.name = computeName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tck.graphql.typesafe;

import static org.assertj.core.api.BDDAssertions.then;

import org.eclipse.microprofile.graphql.Mutation;
import org.junit.jupiter.api.Test;

import io.smallrye.graphql.client.typesafe.api.GraphQLClientApi;

class NillableBehavior {
private final TypesafeGraphQLClientFixture fixture = TypesafeGraphQLClientFixture.load();

@GraphQLClientApi
interface StringMutationApi {
@Mutation
String createSome(SomeInput s);
}

@Test
void nillableFieldsAreSet() {
fixture.returnsData("'createSome':'foo'");
StringMutationApi api = fixture.build(StringMutationApi.class);

String response = api.createSome(new SomeInput("all", "a", "b", "c"));

then(fixture.query()).isEqualTo("mutation createSome($s: SomeInput) { createSome(s: $s) }");
then(fixture.variables()).isEqualTo("{'s':{'name':'all','first':'a','second':'b','third':'c'}}");
then(response).isEqualTo("foo");
}

@Test
void nillableFieldsAreNull() {
fixture.returnsData("'createSome':'bar'");
StringMutationApi api = fixture.build(StringMutationApi.class);

String response = api.createSome(new SomeInput("none"));

then(fixture.query()).isEqualTo("mutation createSome($s: SomeInput) { createSome(s: $s) }");
then(fixture.variables()).isEqualTo("{'s':{'name':'none','second':null,'third':null}}");
then(response).isEqualTo("bar");
}
}
47 changes: 47 additions & 0 deletions client/tck/src/main/java/tck/graphql/typesafe/SomeInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tck.graphql.typesafe;

import java.util.Objects;

import jakarta.json.bind.annotation.JsonbNillable;
import jakarta.json.bind.annotation.JsonbProperty;

import org.eclipse.microprofile.graphql.Input;

@Input("SomeInput")
public class SomeInput {
private String name;

@JsonbProperty("first")
private String firstAtrribute;

@JsonbProperty(value = "second", nillable = true)
private String secondAtrribute;

@JsonbNillable
private String third;

public SomeInput() {
}

public SomeInput(String name) {
this.name = name;
}

public SomeInput(String name, String first, String second, String third) {
this.name = name;
this.firstAtrribute = first;
this.secondAtrribute = second;
this.third = third;
}

@Override
public int hashCode() {
return Objects.hash(name, firstAtrribute, secondAtrribute, third);
}

@Override
public String toString() {
return "SomeInput [name=" + name + ", firstAtrribute=" + firstAtrribute + ", secondAtrribute=" + secondAtrribute
+ ", third=" + third + "]";
}
}

0 comments on commit 50db01b

Please sign in to comment.