Skip to content

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

@CREKD127

Description

@CREKD127

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}}

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