Skip to content

Request locale not respected when validating enumeration #3779

@replikonio

Description

@replikonio

In the graphql.validation.rules.ArgumentsOfCorrectType class, Locale.getDefault() is passed to the isValidLiteralValue function, which is why the error response is returned in the system locale, not the client's. It would be more appropriate to pass getValidationContext().getI18n().getLocale()

Play code:

GraphQL schema = GraphQL.newGraphQL(GraphQLSchema.newSchema()
			.query(GraphQLObjectType.newObject().name("Query")
					.field(GraphQLFieldDefinition.newFieldDefinition()
								.name("test")
								.argument(GraphQLArgument.newArgument().name("a")
											.type(GraphQLTypeReference.typeRef("testEnum")))
								.type(Scalars.GraphQLString)))
			.codeRegistry(GraphQLCodeRegistry.newCodeRegistry().dataFetcher(FieldCoordinates.coordinates("Query", "test"), new DataFetcher<>() {
				@Override
				public Object get(DataFetchingEnvironment environment) throws Exception {
					return "success";
				}
			}).build())
			.additionalType(GraphQLEnumType.newEnum().name("testEnum").value("VAL1").value("VAL2").build())
			.build()).build();
		ExecutionResult res = schema.execute(
				ExecutionInput.newExecutionInput().query("query { test(a: VAL3) }")
				.locale(Locale.of("en", "US")));
		System.out.print(res.getErrors().toString());

My default locale is ru_RU and I get:
[ValidationError{validationErrorType=WrongType, queryPath=[test], message=Validation error (WrongType@[test]) : argument 'a' with value 'EnumValue{name='VAL3'}' is not a valid 'testEnum' - Литеральное значение не входит в допустимые значения для перечисления 'testEnum' - 'EnumValue{name='VAL3'}', locations=[SourceLocation{line=1, column=14}], description='Validation error (WrongType@[test]) : argument 'a' with value 'EnumValue{name='VAL3'}' is not a valid 'testEnum' - Литеральное значение не входит в допустимые значения для перечисления 'testEnum' - 'EnumValue{name='VAL3'}'', extensions=[]}]

After correcting Locale.getDefault() to getValidationContext().getI18n().getLocale() I get the expected result:
[ValidationError{validationErrorType=WrongType, queryPath=[test], message=Validation error (WrongType@[test]) : argument 'a' with value 'EnumValue{name='VAL3'}' is not a valid 'testEnum' - Literal value not in allowable values for enum 'testEnum' - 'EnumValue{name='VAL3'}', locations=[SourceLocation{line=1, column=14}], description='Validation error (WrongType@[test]) : argument 'a' with value 'EnumValue{name='VAL3'}' is not a valid 'testEnum' - Literal value not in allowable values for enum 'testEnum' - 'EnumValue{name='VAL3'}'', extensions=[]}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    keep-openTells Stale Bot to keep PRs and issues open

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions