Skip to content

Commit

Permalink
Fixed wrapper nonnull types
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik authored and jmartisk committed Jul 3, 2024
1 parent ba3cf91 commit fa02098
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,10 @@ private static Map<DotName, AnnotationInstance> getTypeUseAnnotations(org.jboss.

private static Map<DotName, AnnotationInstance> getAnnotations(org.jboss.jandex.Type type) {
Map<DotName, AnnotationInstance> annotationMap = new HashMap<>();

if (type.kind().equals(org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE)) {
org.jboss.jandex.Type typeInCollection = type.asParameterizedType().arguments().get(0);
annotationMap.putAll(getAnnotations(typeInCollection));
} else {
List<AnnotationInstance> annotations = type.annotations();
for (AnnotationInstance annotationInstance : annotations) {
annotationMap.put(annotationInstance.name(), annotationInstance);
}
List<AnnotationInstance> annotations = type.annotations();
for (AnnotationInstance annotationInstance : annotations) {
annotationMap.put(annotationInstance.name(), annotationInstance);
}

return annotationMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,16 @@ private static boolean markParameterizedTypeNonNull(Type fieldType, Type methodT
Annotations annotationsInParameterizedType = Annotations.getAnnotationsForArray(typeInCollection,
methodTypeInCollection);

return NonNullHelper.markAsNonNull(typeInCollection, annotationsInParameterizedType, true);
return NonNullHelper.markAsNonNull(typeInCollection, annotationsInParameterizedType, false);
}
return false;
}

private static Type getTypeInCollection(Type type) {
if (Classes.isArray(type)) {
Type typeInArray = type.asArrayType().component();
return getTypeInCollection(typeInArray);
return type.asArrayType().componentType();
} else if (Classes.isParameterized(type)) {
Type typeInCollection = type.asParameterizedType().arguments().get(0);
return getTypeInCollection(typeInCollection);
return type.asParameterizedType().arguments().get(0);
}
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -910,23 +911,25 @@ private GraphQLInputObjectField createGraphQLInputObjectFieldFromField(Field fie
}

private GraphQLInputType createGraphQLInputType(Field field) {
GraphQLInputType graphQLInputType = referenceGraphQLInputType(field);

GraphQLInputType graphQLInputType = getGraphQLInputType(field.getReference());
Wrapper wrapper = dataFetcherFactory.unwrap(field, false);
// Field can have a wrapper, like List<String>
if (wrapper != null && wrapper.isCollectionOrArrayOrMap()) {
Stack<Wrapper> stackOfWrappers = new Stack<>();
for (Wrapper currentWrapper = wrapper; currentWrapper != null; currentWrapper = currentWrapper.getWrapper()) {
stackOfWrappers.add(currentWrapper);
}
// Loop as long as there is a wrapper
do {
wrapper = stackOfWrappers.pop();
if (wrapper.isCollectionOrArrayOrMap()) {
if (wrapper.isWrappedTypeNotNull()) {
graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
}
graphQLInputType = list(graphQLInputType);
wrapper = wrapper.getWrapper();
} else {
wrapper = null;
}
} while (wrapper != null);
} while (!stackOfWrappers.empty());
}

// Check if field is mandatory
Expand All @@ -943,18 +946,20 @@ private GraphQLOutputType createGraphQLOutputType(Field field, boolean isBatch)
Wrapper wrapper = dataFetcherFactory.unwrap(field, isBatch);
// Field can have a wrapper, like List<String>
if (wrapper != null && wrapper.isCollectionOrArrayOrMap()) {
Stack<Wrapper> stackOfWrappers = new Stack<>();
for (Wrapper currentWrapper = wrapper; currentWrapper != null; currentWrapper = currentWrapper.getWrapper()) {
stackOfWrappers.add(currentWrapper);
}
// Loop as long as there is a wrapper
do {
wrapper = stackOfWrappers.pop();
if (wrapper.isCollectionOrArrayOrMap()) {
if (wrapper.isWrappedTypeNotNull()) {
graphQLOutputType = GraphQLNonNull.nonNull(graphQLOutputType);
}
graphQLOutputType = list(graphQLOutputType);
wrapper = wrapper.getWrapper();
} else {
wrapper = null;
}
} while (wrapper != null);
} while (!stackOfWrappers.empty());
}

// Check if field is mandatory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -24,6 +25,7 @@
import jakarta.annotation.security.RolesAllowed;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.NonNull;
import org.eclipse.microprofile.graphql.Query;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -70,7 +72,7 @@ void testSchemaWithDirectives() throws URISyntaxException, IOException {
assertEquals("intArrayTestDirective", typeDirective.getName());
assertEquals("test-description", typeDirective.getDescription());
assertEquals(1, typeDirective.getArguments().size());
assertEquals("[Int]", typeDirective.getArgument("value").getType().toString());
assertEquals("[Int!]", typeDirective.getArgument("value").getType().toString());

GraphQLDirective fieldDirective = graphQLSchema.getDirective("fieldDirective");
assertEquals("fieldDirective", fieldDirective.getName());
Expand Down Expand Up @@ -425,6 +427,94 @@ void wrongDirectiveLocationInGeneralTest() { // ARGUMENT_DEFINITION -> FIELD_DEF
}
}

@GraphQLApi
static class SomeNonNulLWrapperApi {
@Query
public @NonNull Set<List<@NonNull SomeObject>[]> someOperation(@NonNull Set<List<@NonNull SomeObject>[]> sio) {
return null;
}
}

static class SomeObject {
public Set<Collection<Long>> a;
public Set<Collection<@NonNull Long>> b;
public Set<@NonNull Collection<Long>> c;
@NonNull
public Set<Collection<Long>> d;
public Set<@NonNull Collection<@NonNull Long>> e;
@NonNull
public Set<Collection<@NonNull Long>> f;
@NonNull
public Set<@NonNull Collection<Long>> g;
@NonNull
public Set<@NonNull Collection<@NonNull Long>> h;

public String[] aArray;
public @NonNull String[] bArray;
public List<String[]> cArray;
public Set<int[]> dArray;

public SomeObject() {
}
}

@Test
void nonNullWrapperTest() {
GraphQLSchema graphQLSchema = createGraphQLSchema(SomeObject.class, SomeNonNulLWrapperApi.class);

GraphQLFieldDefinition someOperation = graphQLSchema.getQueryType().getField("someOperation");
assertNotNull(someOperation);
assertEquals("[[[SomeObject!]]]!", someOperation.getType().toString());
assertEquals("[[[SomeObjectInput!]]]!", someOperation.getArgument("sio").getType().toString());

GraphQLObjectType graphQLObjectType = graphQLSchema.getTypeAs("SomeObject");
assertNotNull(graphQLObjectType);

GraphQLInputObjectType graphQLInputObjectType = graphQLSchema.getTypeAs("SomeObjectInput");
assertNotNull(graphQLInputObjectType);

assertEquals(12, graphQLObjectType.getFields().size());
assertEquals(12, graphQLInputObjectType.getFields().size());

assertEquals("[[BigInteger]]", graphQLObjectType.getField("a").getType().toString());
assertEquals("[[BigInteger]]", graphQLInputObjectType.getField("a").getType().toString());

assertEquals("[[BigInteger!]]", graphQLObjectType.getField("b").getType().toString());
assertEquals("[[BigInteger!]]", graphQLInputObjectType.getField("b").getType().toString());

assertEquals("[[BigInteger]!]", graphQLObjectType.getField("c").getType().toString());
assertEquals("[[BigInteger]!]", graphQLInputObjectType.getField("c").getType().toString());

assertEquals("[[BigInteger]]!", graphQLObjectType.getField("d").getType().toString());
assertEquals("[[BigInteger]]!", graphQLInputObjectType.getField("d").getType().toString());

assertEquals("[[BigInteger!]!]", graphQLObjectType.getField("e").getType().toString());
assertEquals("[[BigInteger!]!]", graphQLInputObjectType.getField("e").getType().toString());

assertEquals("[[BigInteger!]]!", graphQLObjectType.getField("f").getType().toString());
assertEquals("[[BigInteger!]]!", graphQLInputObjectType.getField("f").getType().toString());

assertEquals("[[BigInteger]!]!", graphQLObjectType.getField("g").getType().toString());
assertEquals("[[BigInteger]!]!", graphQLInputObjectType.getField("g").getType().toString());

assertEquals("[[BigInteger!]!]!", graphQLObjectType.getField("h").getType().toString());
assertEquals("[[BigInteger!]!]!", graphQLInputObjectType.getField("h").getType().toString());

assertEquals("[String]", graphQLObjectType.getField("aArray").getType().toString());
assertEquals("[String]", graphQLInputObjectType.getField("aArray").getType().toString());

// should be `[String]!`
assertEquals("[String!]!", graphQLObjectType.getField("bArray").getType().toString());
assertEquals("[String!]!", graphQLInputObjectType.getField("bArray").getType().toString());

assertEquals("[[String]]", graphQLObjectType.getField("cArray").getType().toString());
assertEquals("[[String]]", graphQLInputObjectType.getField("cArray").getType().toString());

assertEquals("[[Int!]]", graphQLObjectType.getField("dArray").getType().toString());
assertEquals("[[Int!]]", graphQLInputObjectType.getField("dArray").getType().toString());

}

private void assertRolesAllowedDirective(GraphQLFieldDefinition field, String roleValue) {
assertNotNull(field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ directive @include(
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"test-description"
directive @intArrayTestDirective(value: [Int]) on OBJECT | INTERFACE
directive @intArrayTestDirective(value: [Int!]) on OBJECT | INTERFACE

"Indicates an Input Object is a OneOf Input Object."
directive @oneOf on INPUT_OBJECT
Expand Down

0 comments on commit fa02098

Please sign in to comment.