OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.91k stars 6.58k forks source link

[BUG] 6.2.1 regression - JavaSpring `@NotNull` code annotation with no import #14059

Open ardc-shorn opened 2 years ago

ardc-shorn commented 2 years ago

Bug Report Checklist

Description

JavaSpring generator creates java files with @NotNull but there's no import for that symbol, resulting in javac error cannot find symbol.

openapi-generator version

6.2.1

This is a regression, was working on 6.2.0.

OpenAPI declaration file content or url
    AuthzRequestStatus:
      type: string
      enum: ['APPROVED', 'REQUESTED', 'REJECTED']

Full file: https://github.com/au-research/raido-v2/blob/895de24f6dc9ec3a360ba04acf0f3ee1355b6d59/api-svc/idl-raid-v2/src/admin-experimental.yaml#L334

Generation Details

Project uses the Gradle plugin to generate, full configuration is here: https://github.com/au-research/raido-v2/blob/895de24f6dc9ec3a360ba04acf0f3ee1355b6d59/api-svc/idl-raid-v2/build.gradle#L136

I think the relevant configuration item might be: openApiNullable: "false" and useBeanValidation: "false"?

Generates code like: image

Steps to reproduce
Suggest a fix

If openApiNullable and useBeanValidation set to false - 6.2.1 should not generate the @NotNull annotation. 6.2.0 does not generate the annotation.

Workaround

Customise pojo.mustache template to effective revert the effect of commit 0060dfd1. https://github.com/au-research/raido-v2/blob/main/api-svc/idl-raid-v2/src/main/resources/template/pojo.mustache#L3

ardc-shorn commented 2 years ago

Commit that introduced the issue: https://github.com/OpenAPITools/openapi-generator/commit/0060dfd1#diff-d34f77bd237cdd0449fb17e44bf5485ecac1a2fa5b5a329943ac128bb12fb222

  {{^useBeanValidation}}
  {{#required}}@NotNull{{/required}}
  {{/useBeanValidation}}

I don't yet have a good grasp of Mustache templating or the openapi template variable system. But shouldn't the fact that my openapi config is set to useBeanValidation: "false" mean that the @NotNull annotation should not be generated?

Am I doing something wrong with the way I'm setting the variable from Gradle?
https://github.com/au-research/raido-v2/blob/895de24f6dc9ec3a360ba04acf0f3ee1355b6d59/api-svc/idl-raid-v2/build.gradle#L141