From 77ea56723c852292f060f54903693b273cef9dc8 Mon Sep 17 00:00:00 2001 From: Marek Skacelik Date: Thu, 31 Aug 2023 11:50:01 +0200 Subject: [PATCH] Fixed that the "java.lang.deprecated" annotation can work with: Operations (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. --- .../schema/creator/ArgumentCreator.java | 18 +++++----- .../graphql/schema/creator/FieldCreator.java | 21 ++++++------ .../schema/creator/OperationCreator.java | 34 ++++++++++++++----- .../schema/creator/type/EnumCreator.java | 26 ++++++++++++-- .../schema/creator/type/InputTypeCreator.java | 1 - .../helper/DeprecatedDirectivesHelper.java | 13 +++---- .../helper/RolesAllowedDirectivesHelper.java | 7 ++-- .../graphql/schema/model/EnumValue.java | 3 ++ .../smallrye/graphql/schema/model/Field.java | 4 +-- .../mavenplugin/GenerateSchemaMojo.java | 9 ++--- 10 files changed, 86 insertions(+), 50 deletions(-) diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/ArgumentCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/ArgumentCreator.java index 63834b371..941848a91 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/ArgumentCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/ArgumentCreator.java @@ -103,14 +103,16 @@ public Optional createArgument(Operation operation, MethodInfo methodI } } if (deprecatedHelper != null && directives != null) { - List 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); diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/FieldCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/FieldCreator.java index a0be34b60..78098f7a3 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/FieldCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/FieldCreator.java @@ -106,9 +106,8 @@ public Optional 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); @@ -166,8 +165,8 @@ public Optional 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); @@ -191,14 +190,14 @@ private void addDirectivesForBeanValidationConstraints(Annotations annotationsFo private void addDirectivesForDeprecated(Annotations annotationsForPojo, Field field, Reference parentObjectReference) { if (deprecatedHelper != null && directives != null) { - List 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); + }); } } diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/OperationCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/OperationCreator.java index 9e1dc5845..becc981e2 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/OperationCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/OperationCreator.java @@ -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; @@ -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; @@ -41,6 +40,7 @@ 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()); @@ -48,6 +48,7 @@ public OperationCreator(ReferenceCreator referenceCreator, ArgumentCreator argum super(referenceCreator); this.argumentCreator = argumentCreator; this.rolesAllowedHelper = new RolesAllowedDirectivesHelper(); + this.deprecatedHelper = new DeprecatedDirectivesHelper(); } /** @@ -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()) { @@ -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); + }); } } diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/EnumCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/EnumCreator.java index 33c6735bc..45e299e3c 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/EnumCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/EnumCreator.java @@ -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; @@ -32,8 +34,11 @@ public class EnumCreator implements Creator { 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) { @@ -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); + } } } @@ -88,4 +96,18 @@ private List 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); + }); + } + } + } diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/InputTypeCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/InputTypeCreator.java index 8f27c9d9b..6d36c2219 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/InputTypeCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/InputTypeCreator.java @@ -70,7 +70,6 @@ public InputType create(ClassInfo classInfo, Reference reference) { // Directives inputType.setDirectiveInstances(getDirectiveInstances(annotations)); - // Fields addFields(inputType, classInfo, reference); diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/DeprecatedDirectivesHelper.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/DeprecatedDirectivesHelper.java index e4baf3c8d..9caf245ea 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/DeprecatedDirectivesHelper.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/DeprecatedDirectivesHelper.java @@ -1,11 +1,9 @@ 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; @@ -13,18 +11,15 @@ public class DeprecatedDirectivesHelper { - private static Logger LOGGER = Logger.getLogger(DeprecatedDirectivesHelper.class); - - public List transformDeprecatedToDirectives(Annotations annotations, DirectiveType directiveType) { - List result = new ArrayList<>(); + public Optional transformDeprecatedToDirective(Annotations annotations, DirectiveType directiveType) { Set 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(); } } diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/RolesAllowedDirectivesHelper.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/RolesAllowedDirectivesHelper.java index fdd61fd38..d00e00ba9 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/RolesAllowedDirectivesHelper.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/helper/RolesAllowedDirectivesHelper.java @@ -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; @@ -34,7 +35,7 @@ private static DirectiveArgument createArgument(String name, Reference reference return arg; } - public DirectiveInstance transformRolesAllowedToDirectives(Annotations methodAnnotations, + public Optional transformRolesAllowedToDirectives(Annotations methodAnnotations, Annotations classAnnotations) { Set annotationNames = new HashSet<>(methodAnnotations.getAnnotationNames()); @@ -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) { diff --git a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/EnumValue.java b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/EnumValue.java index 20171a0e6..5782b4d58 100644 --- a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/EnumValue.java +++ b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/EnumValue.java @@ -59,4 +59,7 @@ public void setDirectiveInstances(List directiveInstances) { this.directiveInstances = directiveInstances; } + public void addDirectiveInstance(DirectiveInstance directiveInstance) { + this.directiveInstances.add(directiveInstance); + } } diff --git a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/Field.java b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/Field.java index aacb07152..9f42304ec 100644 --- a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/Field.java +++ b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/Field.java @@ -206,9 +206,7 @@ public void addDirectiveInstances(List directiveInstances) { } public void addDirectiveInstance(DirectiveInstance directiveInstance) { - if (directiveInstances != null) { - this.directiveInstances.add(directiveInstance); - } + this.directiveInstances.add(directiveInstance); } @Override diff --git a/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java b/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java index df2b2b2fe..9cb2c1b4e 100644 --- a/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java +++ b/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java @@ -147,11 +147,12 @@ private IndexView createIndex() throws MojoExecutionException { // even if includeDependencies=false Predicate isMutiny = a -> a.getGroupId().equals("io.smallrye.reactive") && a.getArtifactId().equals("mutiny"); + Predicate 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()); @@ -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) {