Skip to content

@Pattern on @RequestParam List<@Pattern String> leaks to sibling array parameters (incomplete fix from #3259) #3318

Description

@vikas72878

Describe the bug

  • When multiple @RequestParam parameters of type List<@pattern String> are declared on a controller method, and each has a different @pattern regex, springdoc applies the last parameter's pattern to all sibling array parameters in the generated OpenAPI spec.

  • This was partially fixed for @ParameterObject DTOs in PR #3259, but the same bug still occurs for @RequestParam parameters declared directly on controller methods.

To Reproduce

Spring Boot: 4.0.7
springdoc-openapi: 3.0.3 (springdoc-openapi-starter-webmvc-ui)
Minimal controller:

@RestController
@RequestMapping("/demo")
public class DemoController {
    @GetMapping
    public List<String> search(
        @RequestParam(required = false) List<@Pattern(regexp = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\S*") String> versions,
        @RequestParam(required = false) List<@Pattern(regexp = "^[a-zA-Z0-9]{1,255}$") String> storeNbrs,
        @RequestParam(required = false) List<@Pattern(regexp = "^[a-zA-Z]+[a-z_A-Z]*[a-zA-Z]+$") String> locationNames
    ) {
        return List.of();
    }
}

Actual result (/v3/api-docs):

All three parameters get the same pattern (from locationNames):

"parameters": [
    {
        "name": "versions",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-zA-Z]+[a-z_A-Z]*[a-zA-Z]+$"
            }
        }
    },
    {
        "name": "storeNbrs",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-zA-Z]+[a-z_A-Z]*[a-zA-Z]+$"
            }
        }
    },
    {
        "name": "locationNames",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-zA-Z]+[a-z_A-Z]*[a-zA-Z]+$"
            }
        }
    }
]

Expected result:

Each parameter should have its own pattern:

"parameters": [
    {
        "name": "versions",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\S*"
            }
        }
    },
    {
        "name": "storeNbrs",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-zA-Z0-9]{1,255}$"
            }
        }
    },
    {
        "name": "locationNames",
        "in": "query",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "^[a-zA-Z]+[a-z_A-Z]*[a-zA-Z]+$"
            }
        }
    }
]

Expected behavior

Each @RequestParam List<@pattern String> parameter should have its own @pattern regex applied to the items schema independently. The pattern from one parameter should not leak to siblings.

Additional context

PR #3259 fixed this for @ParameterObject DTOs by addressing how annotated generic types are resolved. The same root cause (shared/mutated AnnotatedType state) appears to still affect @RequestParam parameters declared directly on controller methods.

Swagger UI rejects valid input with Value must follow pattern ... because it validates against the wrong (leaked) pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions