spring-framework: Empty or blank required UUID header validation results in 200 response status

Affects: Spring MVC 5.1.9.RELEASE


Hi,

My REST API takes as input a mandatory X-Request-ID header that is transformed to a UUID object.

So my REST controller method has this parameter: @RequestHeader(value="X-Request-ID", required=true) UUID xRequestID

However, if the X-Request-ID HTTP header:

  • is absent : 400 Bad Request (OK)
  • is empty (i.e. the empty string) : 200 OK (NOK, should be 400)
  • is blank (i.e. a string with whitespace only) : 200 OK (NOK, should be 400)
  • is not a UUID (for instance "foobar") : 400 Bad Request (OK)

It seems that the problem comes from:

  1. ‘is empty’ case: StringToUUIDConverter returns null
  2. ‘is blank’ case: StringToUUIDConverter fails, then Spring MVC defaults to UUIDEditor that returns null.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

see pr#23950

Hi @maxdewil, I’ve created a workaround for this problem using an annotation @RequestHeaderNonNull that verifies if the method property is annotated with the @RequestHeader and after that check if the content is null or empty.

https://github.com/armandoalmeida/request-header-required-non-null

I hope it helps you!

@maxdewil, is my assumption correct that you would expect argument resolution for an empty or blank UUID header to result in a MethodArgumentConversionNotSupportedException instead of null?

If I pass the header value “foobar”, the following exception is thrown : org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.UUID’; nested exception is java.lang.IllegalArgumentException: Invalid UUID string: foobar

I think the behavior should be the same for an empty/blank string.