Skip to content

Use applied directives rather than directives by themselves #72

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

Merged
merged 1 commit into from
Sep 15, 2022
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 @@ -4,18 +4,11 @@
import graphql.GraphQLError;
import graphql.PublicSpi;
import graphql.Scalars;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLNamedInputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.*;
import graphql.validation.rules.ValidationEnvironment;
import graphql.validation.util.DirectivesAndTypeWalker;
import graphql.validation.util.Util;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
Expand All @@ -25,6 +18,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.schema.GraphQLTypeUtil.isList;
import static graphql.validation.rules.ValidationEnvironment.ValidatedElement.FIELD;
import static graphql.validation.util.Util.mkMap;
Expand Down Expand Up @@ -138,17 +132,17 @@ private List<GraphQLError> runValidationImpl(ValidationEnvironment validationEnv
private List<GraphQLError> runConstraintOnDirectives(ValidationEnvironment validationEnvironment) {

List<GraphQLError> errors = new ArrayList<>();
List<GraphQLDirective> directives = validationEnvironment.getDirectives();
directives = Util.sort(directives, GraphQLDirective::getName);
List<GraphQLAppliedDirective> directives = validationEnvironment.getDirectives();
directives = Util.sort(directives, GraphQLAppliedDirective::getName);

for (GraphQLDirective directive : directives) {
for (GraphQLAppliedDirective directive : directives) {
// we get called for arguments and input field and field types which can have multiple directive constraints on them and hence no just for this one
boolean isOurDirective = directive.getName().equals(this.getName());
if (!isOurDirective) {
continue;
}

validationEnvironment = validationEnvironment.transform(b -> b.context(GraphQLDirective.class, directive));
validationEnvironment = validationEnvironment.transform(b -> b.context(GraphQLAppliedDirective.class, directive));
//
// now run the directive rule with this directive instance
List<GraphQLError> ruleErrors = this.runConstrainOnPossibleListElements(validationEnvironment);
Expand Down Expand Up @@ -201,18 +195,15 @@ protected boolean isOneOfTheseTypes(GraphQLInputType inputType, Collection<Graph
* @param argName the argument name
* @return a non null value
*/
protected int getIntArg(GraphQLDirective directive, String argName) {
GraphQLArgument argument = directive.getArgument(argName);
protected int getIntArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "Int");
}

Number value = GraphQLArgument.getArgumentValue(argument);
Number value = argument.getValue();
if (value == null) {
value = GraphQLArgument.getArgumentDefaultValue(argument);
if (value == null) {
return assertExpectedArgType(argName, "Int");
}
return assertExpectedArgType(argName, "Int");
}
return value.intValue();
}
Expand All @@ -224,17 +215,14 @@ protected int getIntArg(GraphQLDirective directive, String argName) {
* @param argName the argument name
* @return a non null value
*/
protected String getStrArg(GraphQLDirective directive, String argName) {
GraphQLArgument argument = directive.getArgument(argName);
protected String getStrArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "String");
}
String value = GraphQLArgument.getArgumentValue(argument);
String value = argument.getValue();
if (value == null) {
value = GraphQLArgument.getArgumentDefaultValue(argument);
if (value == null) {
return assertExpectedArgType(argName, "String");
}
return assertExpectedArgType(argName, "String");
}
return value;
}
Expand All @@ -246,17 +234,14 @@ protected String getStrArg(GraphQLDirective directive, String argName) {
* @param argName the argument name
* @return a non null value
*/
protected boolean getBoolArg(GraphQLDirective directive, String argName) {
GraphQLArgument argument = directive.getArgument(argName);
protected boolean getBoolArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "Boolean");
}
Object value = GraphQLArgument.getArgumentValue(argument);
Object value = argument.getValue();
if (value == null) {
value = GraphQLArgument.getArgumentDefaultValue(argument);
if (value == null) {
return assertExpectedArgType(argName, "Boolean");
}
return assertExpectedArgType(argName, "Boolean");
}
return Boolean.parseBoolean(String.valueOf(value));
}
Expand All @@ -268,14 +253,11 @@ protected boolean getBoolArg(GraphQLDirective directive, String argName) {
* @param directive the directive to check
* @return a non null value
*/
protected String getMessageTemplate(GraphQLDirective directive) {
protected String getMessageTemplate(GraphQLAppliedDirective directive) {
String msg = null;
GraphQLArgument arg = directive.getArgument("message");
GraphQLAppliedDirectiveArgument arg = directive.getArgument("message");
if (arg != null) {
msg = GraphQLArgument.getArgumentValue(arg);
if (msg == null) {
msg = GraphQLArgument.getArgumentDefaultValue(arg);
}
msg = arg.getValue();
}
if (msg == null) {
msg = "graphql.validation." + getName() + ".message";
Expand Down Expand Up @@ -310,14 +292,14 @@ protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationE
* @param msgParams the map of parameters
* @return a list of a single error
*/
protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment, GraphQLDirective directive, Map<String, Object> msgParams) {
protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment, GraphQLAppliedDirective directive, Map<String, Object> msgParams) {
String messageTemplate = getMessageTemplate(directive);
GraphQLError error = validationEnvironment.getInterpolator().interpolate(messageTemplate, msgParams, validationEnvironment);
return singletonList(error);
}

protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment, Object... messageParameters) {
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
String messageTemplate = getMessageTemplate(directive);
Object validatedValue = validationEnvironment.getValidatedValue();
GraphQLError error = validationEnvironment.getInterpolator().interpolate(messageTemplate, mkMessageParams(validatedValue, validationEnvironment, messageParameters), validationEnvironment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLScalarType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.rules.ValidationEnvironment;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import static graphql.validation.constraints.GraphQLScalars.GRAPHQL_NUMBER_AND_STRING_TYPES;

abstract class AbstractDecimalMinMaxConstraint extends AbstractDirectiveConstraint {
Expand All @@ -29,7 +31,7 @@ protected boolean appliesToType(GraphQLInputType inputType) {
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
String value = getStrArg(directive, "value");
boolean inclusive = getBoolArg(directive, "inclusive");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLScalarType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.constraints.GraphQLScalars;
import graphql.validation.rules.ValidationEnvironment;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
Expand All @@ -29,7 +30,7 @@ protected boolean appliesToType(GraphQLInputType inputType) {
@Override
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
int value = getIntArg(directive, "value");

boolean isOK;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLInputType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.rules.ValidationEnvironment;

import java.util.Collections;
import java.util.List;

Expand All @@ -18,7 +19,7 @@ final protected List<GraphQLError> runConstraint(ValidationEnvironment validatio
Object validatedValue = validationEnvironment.getValidatedValue();
GraphQLInputType argType = validationEnvironment.getValidatedType();

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
int min = getIntArg(directive, "min");
int max = getIntArg(directive, "max");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLInputType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.constraints.Documentation;
import graphql.validation.rules.ValidationEnvironment;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import static graphql.validation.constraints.GraphQLScalars.GRAPHQL_NUMBER_AND_STRING_TYPES;

public class DigitsConstraint extends AbstractDirectiveConstraint {
Expand Down Expand Up @@ -39,7 +41,7 @@ public boolean appliesToType(GraphQLInputType inputType) {
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
int maxIntegerLength = getIntArg(directive, "integer");
int maxFractionLength = getIntArg(directive, "fraction");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputType;
import graphql.schema.*;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.constraints.Documentation;
import graphql.validation.el.ELSupport;
Expand Down Expand Up @@ -50,7 +47,7 @@ public boolean appliesTo(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsCo

@Override
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
String expression = helpWithCurlyBraces(getStrArg(directive, "value"));

Map<String, Object> variables = StandardELVariables.standardELVars(validationEnvironment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import graphql.GraphQLError;
import graphql.Scalars;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLInputType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
Expand Down Expand Up @@ -47,7 +48,7 @@ protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvir

String strValue = String.valueOf(validatedValue);

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);

String patternArg = getStrArg(directive, "regexp");
Pattern pattern = cachedPattern(patternArg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package graphql.validation.constraints.standard;

import graphql.GraphQLError;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLInputType;
import graphql.validation.constraints.AbstractDirectiveConstraint;
import graphql.validation.constraints.Documentation;
import graphql.validation.rules.ValidationEnvironment;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import static graphql.validation.constraints.GraphQLScalars.GRAPHQL_NUMBER_AND_STRING_TYPES;

public class RangeConstraint extends AbstractDirectiveConstraint {
Expand Down Expand Up @@ -40,7 +42,7 @@ public boolean appliesToType(GraphQLInputType inputType) {
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
BigDecimal min = asBigDecimal(getIntArg(directive, "min"));
BigDecimal max = asBigDecimal(getIntArg(directive, "max"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.execution.ResultPath;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLAppliedDirective;
import graphql.validation.el.StandardELVariables;
import graphql.validation.rules.ValidationEnvironment;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import jakarta.validation.Constraint;
import jakarta.validation.Path;
import jakarta.validation.Payload;
import org.hibernate.validator.internal.engine.MessageInterpolatorContext;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
Expand All @@ -27,12 +16,15 @@
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel;
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;

import jakarta.validation.Constraint;
import jakarta.validation.Path;
import jakarta.validation.Payload;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annoys me - please turn on expand all imports


import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand Down Expand Up @@ -70,7 +62,7 @@ public class ResourceBundleMessageInterpolator implements MessageInterpolator {
@SuppressWarnings("unused")
protected ErrorClassification buildErrorClassification(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
ResultPath fieldOrArgumentPath = validationEnvironment.getValidatedPath();
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
return new ValidationErrorType(fieldOrArgumentPath, directive);
}

Expand Down Expand Up @@ -183,9 +175,9 @@ private org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterp

private static class ValidationErrorType implements ErrorClassification {
private final ResultPath fieldOrArgumentPath;
private final GraphQLDirective directive;
private final GraphQLAppliedDirective directive;

ValidationErrorType(ResultPath fieldOrArgumentPath, GraphQLDirective directive) {
ValidationErrorType(ResultPath fieldOrArgumentPath, GraphQLAppliedDirective directive) {
this.fieldOrArgumentPath = fieldOrArgumentPath;
this.directive = directive;
}
Expand Down
Loading