Skip to content

Commit

Permalink
Fixed that the "java.lang.deprecated" annotation can work with: Opera…
Browse files Browse the repository at this point in the history
…tions (high level fields), enum values, output type fields. Also fixed that while using generate-schema goal, there is no NPE. + minor refactoring of the code.
  • Loading branch information
mskacelik authored and jmartisk committed Sep 1, 2023
1 parent 7410793 commit 77ea567
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ public Optional<Argument> createArgument(Operation operation, MethodInfo methodI
}
}
if (deprecatedHelper != null && directives != null) {
List<DirectiveInstance> deprecatedDirectives = deprecatedHelper
.transformDeprecatedToDirectives(annotationsForThisArgument,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")));
if (!deprecatedDirectives.isEmpty()) {
logger.debug("Adding deprecated directives " + deprecatedDirectives + " to field '" + argument.getName()
+ "' of of method '" + argument.getMethodName() + "'");
argument.addDirectiveInstances(deprecatedDirectives);
}
deprecatedHelper
.transformDeprecatedToDirective(annotationsForThisArgument,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")))
.ifPresent(deprecatedDirective -> {
logger.debug(
"Adding deprecated directive " + deprecatedDirective + " to argument '"
+ argument.getName()
+ "' of method '" + argument.getMethodName() + "'");
argument.addDirectiveInstance(deprecatedDirective);
});
}

populateField(Direction.IN, argument, argumentType, annotationsForThisArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ public Optional<Field> createFieldForPojo(Direction direction, FieldInfo fieldIn
reference);
if (direction == Direction.IN) {
addDirectivesForBeanValidationConstraints(annotationsForPojo, field, parentObjectReference);
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
}

addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
populateField(direction, field, fieldType, methodType, annotationsForPojo);

return Optional.of(field);
Expand Down Expand Up @@ -166,8 +165,8 @@ public Optional<Field> createFieldForPojo(Direction direction, FieldInfo fieldIn
reference);
if (direction == Direction.IN) {
addDirectivesForBeanValidationConstraints(annotationsForPojo, field, parentObjectReference);
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
}
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
populateField(direction, field, fieldType, annotationsForPojo);

return Optional.of(field);
Expand All @@ -191,14 +190,14 @@ private void addDirectivesForBeanValidationConstraints(Annotations annotationsFo
private void addDirectivesForDeprecated(Annotations annotationsForPojo, Field field,
Reference parentObjectReference) {
if (deprecatedHelper != null && directives != null) {
List<DirectiveInstance> deprecatedDirectives = deprecatedHelper
.transformDeprecatedToDirectives(annotationsForPojo,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")));
if (!deprecatedDirectives.isEmpty()) {
logger.debug("Adding deprecated directives " + deprecatedDirectives + " to field '" + field.getName()
+ "' of parent type '" + parentObjectReference.getName() + "'");
field.addDirectiveInstances(deprecatedDirectives);
}
deprecatedHelper
.transformDeprecatedToDirective(annotationsForPojo,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")))
.ifPresent(deprecatedDirectives -> {
logger.debug("Adding deprecated directives " + deprecatedDirectives + " to field '" + field.getName()
+ "' of parent type '" + parentObjectReference.getName() + "'");
field.addDirectiveInstance(deprecatedDirectives);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.jboss.jandex.AnnotationInstance;
Expand All @@ -13,11 +12,11 @@

import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.SchemaBuilderException;
import io.smallrye.graphql.schema.helper.DeprecatedDirectivesHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.MethodHelper;
import io.smallrye.graphql.schema.helper.RolesAllowedDirectivesHelper;
import io.smallrye.graphql.schema.model.Argument;
import io.smallrye.graphql.schema.model.DirectiveInstance;
import io.smallrye.graphql.schema.model.Execute;
import io.smallrye.graphql.schema.model.Operation;
import io.smallrye.graphql.schema.model.OperationType;
Expand All @@ -41,13 +40,15 @@ public class OperationCreator extends ModelCreator {
private final ArgumentCreator argumentCreator;

private final RolesAllowedDirectivesHelper rolesAllowedHelper;
private final DeprecatedDirectivesHelper deprecatedHelper;

private final Logger logger = Logger.getLogger(OperationCreator.class.getName());

public OperationCreator(ReferenceCreator referenceCreator, ArgumentCreator argumentCreator) {
super(referenceCreator);
this.argumentCreator = argumentCreator;
this.rolesAllowedHelper = new RolesAllowedDirectivesHelper();
this.deprecatedHelper = new DeprecatedDirectivesHelper();
}

/**
Expand Down Expand Up @@ -101,6 +102,7 @@ public Operation createOperation(MethodInfo methodInfo, OperationType operationT
maybeArgument.ifPresent(operation::addArgument);
}
addDirectivesForRolesAllowed(annotationsForMethod, annotationsForClass, operation, reference);
addDirectiveForDeprecated(annotationsForMethod, operation);
populateField(Direction.OUT, operation, fieldType, annotationsForMethod);

if (operation.hasWrapper()) {
Expand Down Expand Up @@ -245,14 +247,28 @@ private Execute getExecution(Annotations annotationsForMethod, Annotations annot
return Execute.DEFAULT;
}

private void addDirectivesForRolesAllowed(Annotations annotationsForPojo, Annotations classAnnotations, Operation operation,
private void addDirectivesForRolesAllowed(Annotations annotationsForOperation, Annotations classAnnotations,
Operation operation,
Reference parentObjectReference) {
DirectiveInstance rolesAllowedDirectives = rolesAllowedHelper
.transformRolesAllowedToDirectives(annotationsForPojo, classAnnotations);
if (!Objects.isNull(rolesAllowedDirectives)) {
logger.debug("Adding rolesAllowed directive " + rolesAllowedDirectives + " to method '" + operation.getName()
+ "' of parent type '" + parentObjectReference.getName() + "'");
operation.addDirectiveInstance(rolesAllowedDirectives);
rolesAllowedHelper
.transformRolesAllowedToDirectives(annotationsForOperation, classAnnotations)
.ifPresent(rolesAllowedDirective -> {
logger.debug("Adding rolesAllowed directive " + rolesAllowedDirective + " to method '" + operation.getName()
+ "'");
operation.addDirectiveInstance(rolesAllowedDirective);
});
}

private void addDirectiveForDeprecated(Annotations annotationsForOperation, Operation operation) {
if (deprecatedHelper != null && directives != null) {
deprecatedHelper
.transformDeprecatedToDirective(annotationsForOperation,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")))
.ifPresent(deprecatedDirective -> {
logger.debug("Adding deprecated directive " + deprecatedDirective + " to method '" + operation.getName()
+ "'");
operation.addDirectiveInstance(deprecatedDirective);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import java.util.Optional;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.Type;
import org.jboss.logging.Logger;

import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.helper.DeprecatedDirectivesHelper;
import io.smallrye.graphql.schema.helper.DescriptionHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.Directives;
Expand All @@ -32,8 +34,11 @@ public class EnumCreator implements Creator<EnumType> {
private final TypeAutoNameStrategy autoNameStrategy;
private Directives directives;

private final DeprecatedDirectivesHelper deprecatedHelper;

public EnumCreator(TypeAutoNameStrategy autoNameStrategy) {
this.autoNameStrategy = autoNameStrategy;
this.deprecatedHelper = new DeprecatedDirectivesHelper();
}

public void setDirectives(Directives directives) {
Expand Down Expand Up @@ -69,8 +74,11 @@ public EnumType create(ClassInfo classInfo, Reference reference) {
if (!field.type().kind().equals(Type.Kind.ARRAY) && !IgnoreHelper.shouldIgnore(annotationsForField, field)) {
String description = annotationsForField.getOneOfTheseAnnotationsValue(Annotations.DESCRIPTION)
.orElse(null);
enumType.addValue(
new EnumValue(description, field.name(), getDirectiveInstances(annotationsForField, true)));
EnumValue enumValue = new EnumValue(description, field.name(),
getDirectiveInstances(annotationsForField, true));
addDirectiveForDeprecated(annotationsForField, enumValue);
enumType.addValue(enumValue);

}
}
}
Expand All @@ -88,4 +96,18 @@ private List<DirectiveInstance> getDirectiveInstances(Annotations annotations, b
return directives.buildDirectiveInstances(annotations, enumValue ? "ENUM_VALUE" : getDirectiveLocation());
}

private void addDirectiveForDeprecated(Annotations annotationsForOperation, EnumValue enumValue) {
if (deprecatedHelper != null && directives != null) {
deprecatedHelper
.transformDeprecatedToDirective(annotationsForOperation,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")))
.ifPresent(deprecatedDirective -> {
LOG.debug(
"Adding deprecated directive " + deprecatedDirective + " to enum value '" + enumValue.getValue()
+ "'");
enumValue.addDirectiveInstance(deprecatedDirective);
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public InputType create(ClassInfo classInfo, Reference reference) {

// Directives
inputType.setDirectiveInstances(getDirectiveInstances(annotations));

// Fields
addFields(inputType, classInfo, reference);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package io.smallrye.graphql.schema.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.jboss.jandex.DotName;
import org.jboss.logging.Logger;

import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.model.DirectiveInstance;
import io.smallrye.graphql.schema.model.DirectiveType;

public class DeprecatedDirectivesHelper {

private static Logger LOGGER = Logger.getLogger(DeprecatedDirectivesHelper.class);

public List<DirectiveInstance> transformDeprecatedToDirectives(Annotations annotations, DirectiveType directiveType) {
List<DirectiveInstance> result = new ArrayList<>();
public Optional<DirectiveInstance> transformDeprecatedToDirective(Annotations annotations, DirectiveType directiveType) {
Set<DotName> annotationNames = annotations.getAnnotationNames();
for (DotName annotationName : annotationNames) {
if (annotationName.equals(DotName.createSimple("java.lang.Deprecated"))) {
DirectiveInstance directive = new DirectiveInstance();
directive.setType(directiveType);
result.add(directive);
return Optional.of(directive);
}
}
return result;
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.smallrye.graphql.schema.helper;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.jboss.jandex.AnnotationValue;
Expand Down Expand Up @@ -34,7 +35,7 @@ private static DirectiveArgument createArgument(String name, Reference reference
return arg;
}

public DirectiveInstance transformRolesAllowedToDirectives(Annotations methodAnnotations,
public Optional<DirectiveInstance> transformRolesAllowedToDirectives(Annotations methodAnnotations,
Annotations classAnnotations) {

Set<DotName> annotationNames = new HashSet<>(methodAnnotations.getAnnotationNames());
Expand All @@ -52,10 +53,10 @@ public DirectiveInstance transformRolesAllowedToDirectives(Annotations methodAnn
value = getStringValue(classAnnotations, annotationName, "value");
}
directive.setValue("value", value);
return directive;
return Optional.of(directive);
}
}
return null;
return Optional.empty();
}

private String getStringValue(Annotations annotations, DotName annotationName, String parameterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ public void setDirectiveInstances(List<DirectiveInstance> directiveInstances) {
this.directiveInstances = directiveInstances;
}

public void addDirectiveInstance(DirectiveInstance directiveInstance) {
this.directiveInstances.add(directiveInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ public void addDirectiveInstances(List<DirectiveInstance> directiveInstances) {
}

public void addDirectiveInstance(DirectiveInstance directiveInstance) {
if (directiveInstances != null) {
this.directiveInstances.add(directiveInstance);
}
this.directiveInstances.add(directiveInstance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ private IndexView createIndex() throws MojoExecutionException {
// even if includeDependencies=false
Predicate<Artifact> isMutiny = a -> a.getGroupId().equals("io.smallrye.reactive") &&
a.getArtifactId().equals("mutiny");
Predicate<Artifact> isSmallRyeGraphQLApi = a -> a.getGroupId().equals("io.smallrye") &&
a.getArtifactId().equals("smallrye-graphql-api");
mavenProject.getArtifacts()
.stream()
.filter(isMutiny)
.findAny()
.ifPresent(a -> {
.filter(a -> isMutiny.test((Artifact) a) || isSmallRyeGraphQLApi.test((Artifact) a))
.forEach(a -> {
Result r = indexJar(((Artifact) a).getFile());
if (r != null) {
indexes.add(r.getIndex());
Expand All @@ -165,7 +166,7 @@ private IndexView createIndex() throws MojoExecutionException {
&& includeDependenciesTypes.contains(artifact.getType())
&& (includeDependenciesGroupIds.isEmpty() ||
includeDependenciesGroupIds.contains(artifact.getGroupId()))
&& !isMutiny.test(artifact)) {
&& !isMutiny.test(artifact) && !isSmallRyeGraphQLApi.test(artifact)) {

Index index = indexArtifact(artifact);
if (index != null) {
Expand Down

0 comments on commit 77ea567

Please sign in to comment.