OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Deprecated @Valid on DTO getter for an attribute that is a container (List, ...)

Open Beginner friendly
#24,782 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

With recent versions of Hibernate Validator, the @Valid annotation has been deprecated for containers, and now a WARN log is generated each time it is used (HV000271: Using @Valid on a XXX deprecated).

The DTO generated by openapi-generator seem to mostly respect this, but not for getters on attributes that are a container : in this case, the @Valid annotation is generated both on the container and on the elements.

Error message : HV000271: Using @Valid on a container (java.util.List) is deprecated. You should apply the annotation on the type argument(s). Affected element: ...Dto#getExactSources()

Class attribute is properly defined

public static final String JSON_PROPERTY_EXACT_SOURCES = "exactSources";
@jakarta.annotation.Nonnull
private List<@Valid SourceDto> exactSources;

Setters are properly defined

public ...Dto exactSources(@jakarta.annotation.Nonnull List<@Valid SourceDto> exactSources) {
  
  this.exactSources = exactSources;
  return this;
}

public ...Dto addExactSourcesItem(SourceDto exactSourcesItem) {
  if (this.exactSources == null) {
    this.exactSources = new ArrayList<>();
  }
  this.exactSources.add(exactSourcesItem);
  return this;
}

But getter has 2 @Valid annotations

@jakarta.annotation.Nonnull
@NotNull
@Valid

@JsonProperty(value = JSON_PROPERTY_EXACT_SOURCES, required = true)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public List<@Valid SourceDto> getExactSources() {
  return exactSources;
}
openapi-generator version

7.25.0

Related issues/PRs

A bit similar to #24752

Suggest a fix

The problem seems to come from beanValidation.mustache that unconditionally adds the @Valid at the container level.

Either the generated code should be fully compatible with recent Hibernate Validator, or the generation should have an option to configure how it is generated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with beanValidation.mustache and the Java DTO getter generation path, using the exactSources container example from the report. Compare annotations on the generated getter with those on the field and setter, then run the relevant Java generation or validation tests available in the repository. Done means generated container getters no longer trigger Hibernate Validator warning HV000271.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.