Skip to content

[BUG] x-pattern-message ignored for list items in Spring generator #21167

Open
@mkurczuk

Description

@mkurczuk

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating code with the Spring generator using OpenAPI Generator (7.13.0), the x-pattern-message extension works correctly for single strings, but does not work for list items.

openapi-generator version

7.13.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Sample API
  version: 1.0.0
paths:
  /api/v1/endpoint:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SomeRequest'
      responses:
        '204':
          description: Success
components:
  schemas:
    SomeRequest:
      type: object
      description: Request
      properties:
        patternList:
          description: List of patterns
          type: array
          items:
            type: string
            pattern: '^[\w\W]*$'
            x-pattern-message: Custom message for list
        patternString:
            type: string
            description: Pattern string
            pattern: '^[\w\W]*$'
            x-pattern-message: Custom message for string
Generation Details
openapi-generator-cli generate -g spring -c api-config.json -i test-api.yaml

api-config.json:

{
  "interfaceOnly": true,
  "apiTests": false,
  "modelTests": false,
  "hideGenerationTimestamp": true,
  "useSpringBoot3": true,
  "useJakartaEe": true,
  "useTags": true,
  "useEnumCaseInsensitive": true
}
Steps to reproduce

The generated model class should assign the custom message for both patternString and each item in patternList.

Actual Behavior:

  • For patternString, the custom message is correctly applied:
  private @Nullable String patternString;

  @Pattern(regexp = "^[\\w\\W]*$", message="Custom message for string") 
  @Schema(name = "patternString", description = "Pattern string", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("patternString")
  public String getPatternString() {
    return patternString;
  }
  • For patternList, the custom message is missing:
  @Valid
  private List<@Pattern(regexp = "^[\\w\\W]*$") String> patternList;

  @Schema(name = "patternList", description = "List of patterns", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("patternList")
  public List<@Pattern(regexp = "^[\\w\\W]*$")String> getPatternList() {
    return patternList;
  }
Related issues/PRs
Suggest a fix

It would be ideal if the generated annotation for list items was like:

  @Valid
  private List<@Pattern(regexp = "^[\\w\\W]*$", message="Custom message for list") String> patternList;
 
  @Schema(name = "patternList", description = "List of patterns", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("patternList")
  public List<@Pattern(regexp = "^[\\w\\W]*$", message="Custom message for list")String> getPatternList() {
    return patternList;
  }

instead of omitting the message part.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions