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] fix beanvalidation compilation failed when items type in array is int64 #18379

Merged
merged 2 commits into from
Apr 16, 2024

Conversation

fanqiewanzi
Copy link
Contributor

… is int64

fix #18082

Changes:
I changed the getBeanValidation function for AbstractJavaCodegen to align with the logic in beanValidationCore.mustache. Added judgment on Long types.

Tests:
openapi.yaml

openapi: 3.0.3
info:
  title: test
  description: Test API
  version: 1.0.1

paths:
  /test:
    get:
      responses:
        200:
          description: Valid response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeLongIdArrayObject"
    put:
      responses:
        200:
          description: Valid response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeLongIdArrayObjectMinimumOnly"
    post:
      responses:
        200:
          description: Valid response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeLongIdArrayObjectMaximumOnly"

components:
  schemas:
    SomeLongIdArrayObject:
      type: object
      properties:
        ids:
          type: array
          items:
            type: integer
            format: int64
            minimum: 1000000000000
            maximum: 1999999999999
    SomeLongIdArrayObjectMinimumOnly:
      type: object
      properties:
        ids:
          type: array
          items:
            type: integer
            format: int64
            minimum: 1000000000000
    SomeLongIdArrayObjectMaximumOnly:
      type: object
      properties:
        ids:
          type: array
          items:
            type: integer
            format: int64
            maximum: 1999999999999

run following command to generate:

openapi-generator-cli generate -i openapi.yaml -o test --additional-properties=useBeanValidation=true,useJakartaEe=true -g java

And the code compiled successfully.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member

wing328 commented Apr 13, 2024

cc
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

@martin-mfg
Copy link
Contributor

Thanks for the PR!
The changes look good to me.
Could you please add a unit test or a sample config, to avoid regressions in the future?

@fanqiewanzi
Copy link
Contributor Author

sure, I will finish unit tests today, and I found another bug related to beanvalidation package import, adding a config before fixing this bug doesn't seem particularly appropriate here.

@fanqiewanzi fanqiewanzi force-pushed the fix_issue_18082 branch 2 times, most recently from b492202 to ad46997 Compare April 15, 2024 15:09
@fanqiewanzi
Copy link
Contributor Author

fanqiewanzi commented Apr 15, 2024

Tests are added into AbstractJavaCodegenTest.AnnotationsContainerTest, the following code logic is so complicated to test, so I skip it. 😭 New tests I added here can act as a regression.

if (items.get$ref() != null) {
            Map<String, Schema>  shemas = this.openAPI.getComponents().getSchemas();
            String ref = ModelUtils.getSimpleRef(items.get$ref());
            if (ref != null) {
                Schema<?> schema = shemas.get(ref);
                if (schema == null || ModelUtils.isObjectSchema(schema)) {
                    return "@Valid ";
                }
                items = schema;
            }
        }

Test result is here:

image

@wing328
Copy link
Member

wing328 commented Apr 16, 2024

tested locally and the output compiles without issues

[INFO]
[INFO] --- jar:3.3.0:test-jar (default) @ openapi-java-client ---
[INFO] Building jar: C:\Users\wing3\AppData\Local\Temp\test2342\target\openapi-java-client-1.0.1-tests.jar
[INFO]
[INFO] --- javadoc:3.6.3:jar (attach-javadocs) @ openapi-java-client ---
[INFO] No previous run data found, generating javadoc.
[INFO] Building jar: C:\Users\wing3\AppData\Local\Temp\test2342\target\openapi-java-client-1.0.1-javadoc.jar
[INFO]
[INFO] --- source:3.3.0:jar-no-fork (attach-sources) @ openapi-java-client ---
[INFO] Building jar: C:\Users\wing3\AppData\Local\Temp\test2342\target\openapi-java-client-1.0.1-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.486 s
[INFO] Finished at: 2024-04-16T12:29:55+08:00
[INFO] ------------------------------------------------------------------------

@wing328 wing328 merged commit c7b33c4 into OpenAPITools:master Apr 16, 2024
14 checks passed
@wing328 wing328 changed the title [Java] fix beanvalidation compilation failed when items type in array… [Java] fix beanvalidation compilation failed when items type in array is int64 Apr 16, 2024
@wing328 wing328 added this to the 7.5.0 milestone Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants