OpenAPITools / OpenAPITools/openapi-generator

[REQ] Possibilty of annoted type arguments with bean Validation for Java Collections

Open
#23,705 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

I need to annoted a type argument to apply bean validation for each Collection elements ( List, Set)

// constraint violation when some element is null
  @Valid
  private List<@jakarta.validation.constraints.NotNull Stubb> sample= new ArrayList<>();

but a property x-field-extra-annotation only mark a property. ex.:

// constraint violation when my list is null
  @Valid
  @jakarta.validation.constraints.NotNull
  private List<Stubb> sample = new ArrayList<>();

for solve a problem I think about create a new one ( ex.: x-inner-validation) for use like this:

        sample:
          type: array
          items:
            x-inner-validation: '@jakarta.validation.constraints.NotNull'
            allOf:
              - $ref: '#/components/schemas/Stubb'

I try to edit a pojo.mustache ( JavaSpring/Pojo.mustache) follow my attempt:

property:

{{#isContainer}}
  {{#useBeanValidation}}@Valid{{/useBeanValidation}}
  {{#openApiNullable}}
  private {{>nullableAnnotation}}{{#isNullable}}{{>nullableDataTypeBeanValidation}} {{name}} = JsonNullable.<{{{items.vendorExtensions.x-inner-validation}}}{{{datatypeWithEnum}}}>undefined();{{/isNullable}}{{^required}}{{^isNullable}}{{>nullableDataTypeBeanValidation}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};{{/isNullable}}{{/required}}{{#required}}{{^isNullable}}{{>nullableDataTypeBeanValidation}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};{{/isNullable}}{{/required}}
  {{/openApiNullable}}
  {{^openApiNullable}}
  private {{>nullableAnnotation}}{{#isSet}}Set{{/isSet}}{{^isSet}}List{{/isSet}}<{{{items.vendorExtensions.x-inner-validation}}} {{{items.datatypeWithEnum}}}> {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}} = new {{#isSet}}LinkedHashSet{{/isSet}}{{^isSet}}ArrayList{{/isSet}}<>(){{/defaultValue}};
  {{/openApiNullable}}
  {{/isContainer}}
  {{^isContainer}}

getter:

  public {{>nullableAnnotation}}{{#isContainer}}{{#isSet}}Set{{/isSet}}{{^isSet}}List{{/isSet}}<{{{items.vendorExtensions.x-inner-validation}}} {{{items.datatypeWithEnum}}}>{{/isContainer}}{{^isContainer}}{{>nullableDataTypeBeanValidation}}{{/isContainer}} {{getter}}() {
  return {{name}};

setter

{{#jackson}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{>jackson_annotations}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{/jackson}}
  public void {{setter}}({{>nullableAnnotation}}{{#isContainer}}{{#isSet}}Set{{/isSet}}{{^isSet}}List{{/isSet}}<{{{items.vendorExtensions.x-inner-validation}}} {{{items.datatypeWithEnum}}}>{{/isContainer}}{{^isContainer}}{{>nullableDataType}}{{/isContainer}} {{name}}) {
  this.{{name}} = {{name}};

this config is breaking when I try to use a Set instead of List, but I paste here to try to help a develop a new config. Thank you

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 the JavaSpring Pojo.mustache template and the issue's proposed x-inner-validation extension, then trace how collection item metadata is exposed for List and Set properties. Done means generated Java models place the requested Bean Validation annotation on the collection type argument without breaking Set generation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.