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

Bug [Bean Validation]: Compilation error with array of strings with validations #925

Open
joschi opened this issue Dec 23, 2024 · 2 comments

Comments

@joschi
Copy link

joschi commented Dec 23, 2024

When modeling a schema object with an array property with items of type string and the minLength validation set, the generated code is missing an import to jakarta.validation.constraints.Size or the annotation must use its FQCN when using Bean Validation.

Example:

list:
  type: array
  items:
    type: string
    minLength: 1

Generated code: target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java

        /**
          * Some object to be validated
         **/
        @jakarta.ws.rs.QueryParam("_list")
        private List<@Size(min = 1)String> _list = null;

Compilation error:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[227,23] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject.ValidatedObjectQueryParam
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[332,22] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject.ValidatedObjectQueryParam
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[339,35] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject.ValidatedObjectQueryParam
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[343,54] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject.ValidatedObjectQueryParam
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[39,19] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[135,18] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[142,31] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject
[ERROR] ./client/integration-tests/bean-validation/target/generated-sources/open-api-yaml/org/openapi/quarkus/bean_validation_true_yaml/model/ValidatedObject.java:[146,40] cannot find symbol
  symbol:   class Size
  location: class org.openapi.quarkus.bean_validation_true_yaml.model.ValidatedObject
@denvitaharen
Copy link
Contributor

denvitaharen commented Dec 26, 2024

For fun I looked at this and as I understand it, the @Size annotation is added by the OpenAPI generator, so either you add an import in the beginning of the file or you somehow modify the code that OpenAPI generator generates.

Or am I missing something in the code? I have tried to look if there is some code that alters the code that's generated, but I can't find it when looking in the code and tried solving the issue.

Edit 1:
I have now checked a bit more and realized that the "problem" lies here:

{#if v.isContainer}
    private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{/if};
{#else}

v.datatypeWithEnum that I understand comes from the OpenAPI generator is in this case List<@Size(min = 1)String>, I can't find an good way to add so we get an FQCN in the annotation. The simplest solution is to add an import, but I know @hbelmiro has said thats not what's the goal. What also looks like a problem is that the code in beanValidationCore.qute doesn't handle Lists.

Or am I totally wrong? :)

@ricardozanini
Copy link
Member

@denvitaharen you can open a PR to change the template by adding the FQCN to the annotation. 👍

Adding the import is not an option because we can't possibly know for sure if it will include @Size in the generated code.

Alternatively, you can add this import to the list of imports (see our subclasses of JavaGenerator from the openapi-gen lib) if validation is enabled and the POJOs would have these annotations.

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

No branches or pull requests

3 participants