Skip to content

Validation of deprecated non-nullable non-default argument is not correct #4182

@Blemicek

Description

@Blemicek

Describe the bug
GraphQL specification prohibits deprecation directive on non-nullable arguments without a default value, but graphql-java allows creation of such schema programmatically although there is validation (DeprecatedInputObjectAndArgumentsAreValid) that should prevent that.

Let say a new argument is created like this:

GraphQLArgument deprecatedArg = GraphQLArgument.newArgument()
  .name("argument")
  .type(GraphQLNonNull.nonNull(Scalars.GraphQLString))
  .deprecate("Some very good reason")
  .build();

The validation then tries to find deprecated applied directive:

GraphQLAppliedDirective deprecatedDirective = argument.getAppliedDirective(Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName());

But deprecatedDirective is null, and so it's reported as valid schema. The fix seems to be calling of argument.isDeprecated(), or combination of both approaches.

To Reproduce

GraphQLArgument deprecatedArg = GraphQLArgument.newArgument()
        .name("input")
        .type(GraphQLNonNull.nonNull(Scalars.GraphQLString))
        .deprecate("Some very good reason")
        .build();

GraphQLFieldDefinition field = GraphQLFieldDefinition.newFieldDefinition()
        .name("field")
        .type(Scalars.GraphQLString)
        .argument(deprecatedArg)
        .dataFetcher(env -> env.getArgument("input")) // for simplicity
        .build();

GraphQLObjectType queryType = GraphQLObjectType.newObject()
        .name("Query")
        .field(field)
        .build();

GraphQLSchema.newSchema()
        .query(queryType)
        .build(); // this method internally do the validation and should throw exception

Equivalent schema (implicit parts are omitted):

type Query {
  field(input: String! @deprecated(reason : "Some very good reason")): String
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions