Skip to content

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

Description

@nvervelle

Bug Report Checklist

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.

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