Skip to content

Bean Validation for array + item constraints is incomplete when mapping array to List #480

Closed

Description

@Razvanmatis

Hello,

when I generate code from the following OpenAPI schema, I get inconsistent Bean Validation annotations depending on the array mapping.

OpenAPI schema

ValidationRequest:
  type: object
  required:
    - openApiFile
  additionalProperties: false
  properties:
    openApiFile:
      type: object
      additionalProperties: true
      description: OpenAPI file content.
      required:
        - openapi
    environment:
      type: string
      enum: [ local, dev]
      default: "dev"
      description: Target environment for the validation.
    rules:
      type: array
      description: Optional list of specific rules to validate against. If not provided, the default ruleset will be used.
      maxItems: 1000
      items:
        type: string
        maxLength: 500
        pattern: "^[a-zA-Z0-9_-]+$"

mapping.yaml

openapi-processor-mapping: v18

options:
  package-name: service_api
  model-name-suffix: Model
  model-type: record
  bean-validation: true
  generated: true
  generated-date: true
  format-code: false
  javadoc: true
  json-property-annotation: auto
  package-name-from-path: true
  model-unreferenced: true

map:
  types:
    - type: array => java.util.List
    - type: string:uuid => java.util.UUID
    - type: ValidationRequestOpenApiFile => java.util.Map<java.lang.String, java.lang.Object>
    - type: ValidationRegexRequestOpenApiFile => java.util.Map<java.lang.String, java.lang.Object>

logging:
  mapping: true
  mapping-target: stdout

Actual result (with type: array => java.util.List)

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * @param openApiFile OpenAPI file content.
 * @param environment Target environment for the validation.
 * @param rules Optional list of specific Spectral rules to validate against. If not provided, the default ruleset will be used.
 */
@Generated(value = "openapi-processor-spring", version = "2026.3", date = "2026-06-30T13:01:10.636288300+02:00")
public record ValidationRequestModel(
    @Valid
    @NotNull
    Map<String, Object> openApiFile,

    ValidationRequestEnvironmentModel environment,

    List<@Size(max = 500) @Pattern(regexp = "^[a-zA-Z0-9_-]+$") String> rules
) {}

Issue: the container constraint @SiZe(max = 1000) (from maxItems) is missing.

Actual result (without type: array => java.util.List)

import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @param openApiFile OpenAPI file content.
 * @param environment Target environment for the validation.
 * @param rules Optional list of specific Spectral rules to validate against. If not provided, the default ruleset will be used.
 */
@Generated(value = "openapi-processor-spring", version = "2026.3", date = "2026-06-30T13:10:50.099920200+02:00")
public record ValidationRequestModel(
    @Valid
    @NotNull
    Map<String, Object> openApiFile,

    ValidationRequestEnvironmentModel environment,

    @Size(max = 1000)
    String[] rules
) {}

Issue: now the item-level string constraints are missing (@SiZe(max = 500) and @pattern(...)).

Expected result

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * @param openApiFile OpenAPI file content.
 * @param environment Target environment for the validation.
 * @param rules Optional list of specific Spectral rules to validate against. If not provided, the default ruleset will be used.
 */
@Generated(value = "openapi-processor-spring", version = "2026.3", date = "2026-06-30T13:11:52.615243800+02:00")
public record ValidationRequestModel(
    @Valid
    @NotNull
    Map<String, Object> openApiFile,

    ValidationRequestEnvironmentModel environment,

    @Size(max = 1000)
    List<@Size(max = 500) @Pattern(regexp = "^[a-zA-Z0-9_-]+$") String> rules
) {}

Question

Is this currently supported (possibly via configuration), or is this a bug/limitation in annotation generation for mapped arrays (List)?

Activity

  1. hauner commented on Jun 30, 2026

    @hauner
    Member

    I have to look at this.

    I don't remember any specific option for it. It may be possible to use annotation mapping as workaround, just a guess though.

    This stuff is a bit tricky so it is probably a limitation of the current implementation.

  2. Razvanmatis commented on Jul 2, 2026

    @Razvanmatis
    Author

    @hauner thanks for your reply! then i will wait for your next comment as i'm not really aware of how to use annotation mapping to solve this issue

  3. hauner commented on Jul 5, 2026

    @hauner
    Member

    here is a (somewhat obscure ;-) workaround by adding an extension property x-maxItems to the property

            rules:
              type: array
              description: Optional list of specific rules to validate against. If not provided, the default ruleset will be used.
              maxItems: 1000
              x-maxItems: max-1000 # ADDED
              items:
                type: string
                maxLength: 500
                pattern: "^[a-zA-Z0-9_-]+$"

    and mapping the x-tension property to the wanted annotation:

    map:
      extensions:
        x-maxItems: max-1000 @ javax.validation.constraints.Size(max = 1000)

    Let's see If I can make it work without that workaround.

  4. added this to the 2026.3.2 milestone on Jul 5, 2026
  5. hauner commented on Jul 7, 2026

    @hauner
    Member

    fixed in openapi-processor-spring 2026.3.2

  6. Razvanmatis commented on Jul 15, 2026

    @Razvanmatis
    Author

    thanks a lot for your support @hauner ! works as expected now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions