Skip to content

Support target: "type_use" for @annotate to avoid deprecated @Valid on containers (fixes #954) - #958

Open
harshit6392 wants to merge 2 commits into
Netflix:masterfrom
harshit6392:fix/annotate-type-use-target
Open

harshit6392 wants to merge 2 commits into
Netflix:masterfrom
harshit6392:fix/annotate-type-use-target

Conversation

@harshit6392

Copy link
Copy Markdown

Fixes #954

Hibernate Validator 9.x / Jakarta EE 11 deprecates placing cascading-validation
annotations such as @Valid directly on a container type:

@Valid List<Employee> employees; // HV000271 deprecation warning

and recommends annotating the type argument instead:

List<@Valid Employee> employees;

Today the @annotate directive's target option only supports placing the
generated annotation on the field/getter/setter/constructor-parameter
declaration (target: "field" | "get" | "set" | "setparam" | "param"),
so there was no way to reproduce the recommended List<@Valid T> shape.

What this PR does

Adds a new target: "type_use" option for @annotate (Java codegen only).
When used, the annotation is woven into the type itself (JSR‑308 /
type-use style) rather than added as a declaration annotation:

  • For a parameterized/container type (e.g. List<Employee>), the annotation
    is applied to the type argument: List<@Valid Employee>.
  • For any other type, the annotation is applied to the type itself, e.g.
    @NotNull String.

The resulting type is applied consistently everywhere the field's type is
declared: the field itself, the getter return type, the setter parameter,
and the all-args constructor parameter.

Example

input WorkplaceInput {
    employees: [EmployeeInput!]
        @annotate(name: "jakarta.validation.Valid", target: "type_use")
}

generates:

private List<@Valid EmployeeInput> employees;
...
public List<@Valid EmployeeInput> getEmployees() { ... }
public void setEmployees(List<@Valid EmployeeInput> employees) { ... }

instead of the deprecated @Valid List<EmployeeInput> employees.

Changes

  • SiteTarget.kt — new TYPE_USE enum value.
  • JavaPoetUtils.kt — new applyTypeUseAnnotations(type, annotations) helper
    that annotates a ParameterizedTypeName's type argument(s), or the type
    itself for non-parameterized types.
  • DataTypeGenerator.ktaddField, addGetterAndSetter, and
    addParameterizedConstructor now recognize SiteTarget.TYPE_USE and apply
    the annotation to the type via the new helper instead of as a declaration
    annotation.
  • CodeGenTest.kt — two new tests covering the list/container case and the
    plain-type case.

Scope / follow-ups

  • This only covers the Java generator, matching the issue's example.
    Kotlin/Kotlin2 codegen (KotlinPoetUtils.kt, Kotlin2*.kt) intentionally
    isn't touched here — happy to add parity there in a follow-up if
    maintainers want it before merging (CONTRIBUTING.md asks for feature
    parity between the two).
  • The public docs for the @annotate directive live in the separate
    netflix.github.io/dgs docs site, not in this repo, so they aren't
    updated as part of this PR.

Testing

Added CodeGenTest cases:

  • annotateOnTypesWithTargetsOnTypeUse annotates the list type argument instead of the container
  • annotateOnTypesWithTargetsOnTypeUse annotates a non-container field type directly

I wasn't able to run ./gradlew test in the environment I prepared this
patch in (no network access to Maven Central / Gradle's distribution
server), so please run the full test suite before merging — happy to fix up
anything that doesn't line up.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HV000271: Using @Valid on a container (java.util.List) is deprecated. You should apply the annotation on the type argument(s).

1 participant