swagger-api / swagger-api/swagger-codegen

[JAVA] No validator could be found for constraint '@Size` on enum type

Open
#12,472 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

If a Swagger spec declares an enum but also specifies a maxLength, running swagger-codegen with useBeanValidation enabled, will tag the enum with @Size constraint. Invoking validation on the generated model will result in this Java error: HV000030: No validator could be found for constraint 'jakarta.validation.constraints.Size' validating type

The reason for the failure is that @Size can only be used to enforce validation on String or Collections, so if it's used on an enum type, the jakarta library will panic.

Swagger-codegen version

The issue is observed in version 3.0.62

Swagger declaration file content or URL

My Swagger spec contains this enum declaration:

        Status:
          type: "string"
          maxLength: 10
          description: "status of something"
          enum:
            - "PENDING"
            - "COMPLETED"
Command line used for generation

This command was run on mac using the latest swagger-codegen installed using brew:

swagger-codegen generate -i sample.yaml -l java --additional-properties useBeanValidation=true -o ./build
$ swagger-codegen version
3.0.62
Steps to reproduce
  1. Find a swagger spec that contains enum with maxLenghh or add one to any swagger spec.
  2. Run the command above to generate JAVA code with bean validation.
  3. Invoke the validation on the model containing the enum using Validation.buildDefaultValidatorFactory().getValidator().validate(toValidate);
Related issues/PRs

N/A

Suggest a fix/enhancement

SImple fix: remove maxLength from the enum declaration, or remove @Size manually in the generated model. However it would be good for swagger-codgen to handle this sensibly.

Perhaps we can update the mustache template to add @Size for non-enum type like this:

{{#minLength}}{{#maxLength}}{{^isEnum}} @Size(min={{minLength}},max={{maxLength}}){{/isEnum}}{{/maxLength}}{{/minLength}}

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

Inspect modules/swagger-codegen/src/main/resources/Java/beanValidationCore.mustache, especially the @Size rendering mentioned in the issue. Generate Java code from an enum with maxLength using useBeanValidation=true, then validate the generated model; done means enum types no longer receive an invalid @Size constraint while other applicable types retain it.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.