Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] [JAXRS] [BUG] Enums Decorated with @Valid in Componentized Classes #18430

Closed
5 of 6 tasks
CREKD127 opened this issue Apr 19, 2024 · 2 comments · Fixed by #18451
Closed
5 of 6 tasks

[JAVA] [JAXRS] [BUG] Enums Decorated with @Valid in Componentized Classes #18430

CREKD127 opened this issue Apr 19, 2024 · 2 comments · Fixed by #18451

Comments

@CREKD127
Copy link
Contributor

CREKD127 commented Apr 19, 2024

Bug Report Checklist

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

In response to #18302, simple fields no longer undergo validation, which includes enums as well. However, it seems that enum classes that are componentized are considered container types, and without setting isContainer, the @Valid annotation does not get removed.

openapi-generator version

7.5.0

Steps to reproduce

This can be reproduced by implementing such an API definition.

components:
  schemas:
    StringEnum:
      type: string
      enum:
      - foo
      - bar
      - baz
      default: foo
    IntegerEnum:
      type: integer
      enum:
      - 1
      - 2
      - 3
      default: 1

paths:
  /tests/defaults:
    get:
      responses:
        default:
          description: response
          content:
            application/json:
              schema:
                type: object
                properties:
                  stringEnum:
                    $ref: '#/components/schemas/StringEnum'
                  integerEnum:
                    $ref: '#/components/schemas/IntegerEnum'
                  stringEnumInline:
                    type: string
                    enum:
                    - foo
                    - bar
                    - baz
                    default: foo
                  integerEnumInline:
                    type: integer
                    enum:
                    - 1
                    - 2
                    - 3
                    default: 1

The @Valid was missing for enum parameters defined inline.
However, enums described in components still had @Valid.

  @javax.annotation.Nullable
  @JsonProperty("stringEnum")
  @ApiModelProperty(value = "")
  @Valid 
  public StringEnum getStringEnum() {
    return stringEnum;
  }

  ...
  
  @javax.annotation.Nullable
  @JsonProperty("stringEnumInline")
  @ApiModelProperty(value = "")
  
  public StringEnumInlineEnum getStringEnumInline() {
    return stringEnumInline;
  }
Related issues/PRs

#18302

Suggest a fix

of this file.
https://github.com/OpenAPITools/openapi-generator/blob/v7.5.0/modules/openapi-generator/src/main/resources/JavaJaxRS/beanValidation.mustache

This part of the file

{{#required}}@NotNull {{/required}}{{^isPrimitiveType}}{{^isDate}}{{^isDateTime}}{{^isString}}{{^isFile}}@Valid {{/isFile}}{{/isString}}{{/isDateTime}}{{/isDate}}{{/isPrimitiveType}}{{>beanValidationCore}}

By modifying the file like this, @Valid has been removed even for enum types defined in components.

{{#required}}@NotNull {{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isDate}}{{^isDateTime}}{{^isString}}{{^isFile}}{{^isEnum}}@Valid {{/isEnum}}{{/isFile}}{{/isString}}{{/isDateTime}}{{/isDate}}{{/isPrimitiveType}}{{/isContainer}}{{>beanValidationCore}}
@CREKD127
Copy link
Contributor Author

@wing328

@wing328
Copy link
Member

wing328 commented Apr 22, 2024

can you please file a PR with the suggested fix?

wing328 added a commit that referenced this issue May 14, 2024
wing328 added a commit that referenced this issue May 14, 2024
* add tests for #18430

* add new filies
renatomameli pushed a commit to renatomameli/openapi-generator that referenced this issue May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants