question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java validation errors in graphql

See original GitHub issue

Describe the bug

I’m experiensing validation in GraphQL inputs with the Java Bean Validation API and I tried to customize message errors by using the expression language (EL) as below :

@Size(value = 5, message = "{ '${validatedValue}' must be at least {value} characters long. Length found : '${validatedValue.length()}'}")
private String civility;

Then in the graphql playground if we added a wrong civility with an invalid charachter long, here must be at least 5 we get this message error :

"message": "validation failed: updateAdherent.adherent.civility : 'TESTEST' must be at least 5 characters long. Length found : '${validatedValue.length()}'

Expected behavior

In the message error should be :

"message": "validation failed: updateAdherent.adherent.civility : 'TESTEST' must be at least 5 characters long. Length found : '7'

Actual behavior

"message": "validation failed: updateAdherent.adherent.civility : 'TESTEST' must be at least 5 characters long. Length found : '${validatedValue.length()}'

How to Reproduce?

As a reproducer we start by the GraphQL input

import java.time.LocalDate;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Past;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreatePersonUseCaseInput {
    @NotBlank(message="'${validatedValue}' cannot be null")
    private String name;
    @Past(message="Date must be in past. found '${validatedValue}'")
    @JsonbDateFormat(value = "dd/MM/yyyy")
    private LocalDate dateOfBirth;
    @Email(message="email should be valid")
    private String email;
  @Size(value = 5, message = "{ '${validatedValue}' must be at least {value} characters long. Length found : '${validatedValue.length()}'}")
  private String civility;
}

then the mutation code:

@Mutation
    @Description("create person")
    public String createPerson(@Valid @Name("person")CreatePersonUseCaseInput input) {
        /*
         * do something
         * */
        return "successful creation";
    }

Lastly the query in the graphql playground is

mutation{
  createPerson(
    person:{
      name:"test"
      dateOfBirth: "12/03/2002"
      email: "test.test@gg"
      civility: "TESTEST"
    }
  )
}

Output of uname -a or ver

No response

Output of java -version

“1.8.0_265”

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.5.1.Final

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yrodierecommented, Feb 22, 2022

I wonder if we should have some sort of allow list allowing some of the harmless but very useful methods. @yrodiere any opinion on that? It makes things a bit blurry though.

Meh. Not a fan of things that needs to be updated continuously, and that list probably would. Maybe if it can be customized by users somehow…

Regardless, I think the main problem here is that users are not warned that their attempt to call a method is being ignored, and they are not provided with a helpful message telling them what to change in their application to make it work. But I suppose we don’t have a way to warn something every time a method call is ignored? It doesn’t work that way?

Also, for now, you cannot configure the default expression language level in Quarkus, we need to fix it.

👍

1reaction
gsmetcommented, Feb 22, 2022

Yes exactly.

The default allows you to use bean properties but not call methods on beans. Thus why you cannot use the length() method.

I wonder if we should have some sort of allow list allowing some of the harmless but very useful methods. @yrodiere any opinion on that? It makes things a bit blurry though.

Also, for now, you cannot configure the default expression language level in Quarkus, we need to fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Handling with GraphQL & Java Tutorial
At the highest level, graphql-java-servlet exposes a method (called isClientError ) that decides whether an error's message is to be sent to the...
Read more >
Exceptions | GraphQL Java
graphql.schema.validation.InvalidSchemaException. is thrown if the schema is not valid when built via graphql.schema.GraphQLSchema.Builder#build() · graphql.
Read more >
Extended Validation for graphql-java - GitHub
The validation library aims to offer Internationalisation (18N) of the error messages. When the validation rules run they are passed in a java.util.Locale...
Read more >
How to handle GraphQL query validation error in Spring Boot
First, create a custom exception class that implements GraphQLError. import graphql.GraphQLError; import graphql.language.SourceLocation; import ...
Read more >
Spring for GraphQL request validation | by Ivan Polovyi
Validation errors in a REST Spring Boot application can be customized by creating a class annotated with @RestControllerAdvice, and creating a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found