Skip to content

FieldNameResolver.resolve() returns empty string for @RequestParam validation errors #90

@rammrain

Description

@rammrain

When a validation constraint (e.g., @NotEmpty) fails on a @RequestParam method parameter, FieldNameResolver.resolve() returns an empty string instead of the parameter name. This results in validation error responses with blank field names.

Environment

  • Library version: ee.bitweb:spring-core:3.4.0
  • Java version: 21
  • Spring Boot: 3.3.x

Root Cause

In FieldNameResolver.java, line 21:

private static final EnumSet<ElementKind> IGNORED_ELEMENTS = EnumSet.of(ElementKind.METHOD, ElementKind.PARAMETER);

For @RequestParam validation errors, the constraint violation path contains only METHOD and PARAMETER nodes. Since both element kinds are in IGNORED_ELEMENTS, the resolveFieldName() method skips all nodes and returns an empty string.

The fallback method resolveWithRegex() handles this correctly, but it's never called because the condition in resolve() succeeds (the error is a ConstraintViolationImpl with PathImpl).

Reproduction

Controller:

@Validated
@RestController
public class ExampleController {

    @GetMapping("/example")
    public Response<?> example(
            @RequestParam @NotEmpty List<String> items) {
        return new Response<>(items);
    }
}

Request:

GET /example?items=

Expected response:

{
  "errors": [
    {
      "field": "items",
      "reason": "NotEmpty",
      "message": "must not be empty"
    }
  ]
}

Actual response:

{
  "errors": [
    {
      "field": "",
      "reason": "NotEmpty",
      "message": "must not be empty"
    }
  ]
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions