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:
- ‘is empty’ case:
StringToUUIDConverterreturnsnull - ‘is blank’ case:
StringToUUIDConverterfails, then Spring MVC defaults toUUIDEditorthat returnsnull.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 15 (9 by maintainers)
Commits related to this issue
- Test status quo for @RequestHeader UUID conversion support See gh-23939 — committed to spring-projects/spring-framework by sbrannen 7 years ago
see pr#23950
Hi @maxdewil, I’ve created a workaround for this problem using an annotation
@RequestHeaderNonNullthat verifies if the method property is annotated with the@RequestHeaderand after that check if the content is null or empty.https://github.com/armandoalmeida/request-header-required-non-null
I hope it helps you!
@DavidZemon actually your question is related to another issue: https://github.com/spring-projects/spring-framework/issues/11041 (and also this one : https://github.com/spring-projects/spring-framework/issues/16519)
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.