openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [JAVA] when using schemaMapping for models in collections, not compilable code with @Valid is generated

Open arandth opened this issue 1 year ago • 11 comments
trafficstars

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

My Spec defines a compenent like this:

MyResponse:
  required:
    - myResponses
  type: object
  properties:
    myResponses:
      uniqueItems: true
      type: array
      default: []
      items:
        $ref: '#/components/schemas/MyResponseItem'

This results in a java Declaration like this:

@Valid private Set<@Valid MyResponseItem> myResponses;

So far so good.

I need to use schemaMapping for MyResponseItem, e.g. <schemaMappings> MyResponseItem=com.mycompany.MyResponseItem </schemeMappings> (as MyResponseItem class need to be manually provided)

Now the generator generates non-compilable code:

@Valid private Set<@Valid com.mycompan.MyResponseItem> myResponses;

as java (at least java17) only expects simle typenames after @Valid

openapi-generator version

7.2.0. But also happens with 7.1.0

OpenAPI declaration file content or url

see above

Generation Details
Steps to reproduce

see above

Related issues/PRs
Suggest a fix

arandth avatar Jan 25 '24 14:01 arandth

Confirming also affecting us with Java 21. Downgrading to openapi-generator 7.0.1 works as a workaround for the time being.

ctvairplus avatar Jan 26 '24 08:01 ctvairplus

Duplicate of #17647

wacker avatar Jan 26 '24 17:01 wacker

@arandth can you check if my PR fixes your issue?

jpfinne avatar Feb 01 '24 18:02 jpfinne

Hi @jpfinne sure, I will do. Thanks for having provided it. One question: is there a maven-artifact for it? Or do I need to build it privately in order to test? Looking at the PR: seems that it aims to skip the @Valid in such container-situations, right? Does ModelUtils.isArraySchema(target) also return true of you have another collection (like Set)? And it seems to remove the validation also when I do not use schemaMapping. Is that really wanted?

arandth avatar Feb 02 '24 07:02 arandth

@arandth, I've just committed the last changes (I forgot yesterday!)

The code should produce Set<com.mycompany.@Valid MyResponseItem> or Set<@Valid MyResponseItem> for an argument Set<com.mycompany.MyResponseItem> or Set<MyResponseItem> for a response.

I'll add a test for uniqueItems: true

jpfinne avatar Feb 02 '24 08:02 jpfinne

@arandth I've added a test for your specific case.

To test it, I think you just have to

  1. clone/copy from https://github.com/jpfinne/openapi-generator/tree/fix17472
  2. mvn install
  3. use 7.3.0-SNAPSHOT from your local repository

jpfinne avatar Feb 02 '24 10:02 jpfinne

error: unable to create file samples/server/petstore/cpp-restbed/test/java_client_tests/java_client/src/test/java/test_error_handling_server_stubs/approval_files/StoreApiTest.getOrderByIdThrowsStdExceptionDerivedException.approved.txt: Filename too long Updating files: 100% (41193/41193), done. fatal: unable to checkout working tree warning: Clone succeeded, but checkout failed. You can inspect what was checked out with 'git status' and retry with 'git restore --source=HEAD :/'

arandth avatar Feb 02 '24 12:02 arandth

The checkout is not usable (millions of deleted files) Note: I'm working on Windows....

arandth avatar Feb 02 '24 12:02 arandth

the structure is very deep. You have to clone in a folder with a short path (c:\openapi for example) You might need to configure git for long paths as well. git config core.longpaths true

Otherwise you just unzip the zip version (in a short path as well)

jpfinne avatar Feb 02 '24 13:02 jpfinne

Hi @jpfinne sorry for the late response. As I encountered the same problem again with another project, I remembered this bug report. I tried your latest hint (shorter pathname in checkout directory), and that worked fine. mvn install worked and produced a usable plugin. I could verify that the produced code looks like Set<com.mycompany.@Valid MyResponseItem>, and it compiles fine.

In the meantime, I'm trying to use version 7.6.0 of the openapi-generator-plugin, there the problem still exist. So I would appreciate if your fix would get promoted and made available in an official release soon. Thanks a lot, Thomas

arandth avatar May 28 '24 08:05 arandth

Hi @jpfinne Any updates here?

arandth avatar Jun 28 '24 06:06 arandth

Hi everybody is there anybody else to help on this? @jpfinne unfortunately doesn't answer any more :-(

arandth avatar Jul 02 '24 15:07 arandth

see #19093

jpfinne avatar Jul 05 '24 17:07 jpfinne

Great! Anything what I need to do?

arandth avatar Jul 08 '24 10:07 arandth

Great! Anything what I need to do?

Thanks. Maybe reviewing/testing the code and ask wing328 to merge it (in the PR) ?

jpfinne avatar Jul 08 '24 11:07 jpfinne

@wing328 Could you please review and merge the PR?

arandth avatar Aug 02 '24 07:08 arandth

Hi @jpfinne Please note that I commented on your PR. Especially I verified that the feature branch for that PR also works in my environment.

arandth avatar Aug 20 '24 10:08 arandth

Hi @jpfinne @wing328 any ideas how to proceed here??? Regards, Thomas

arandth avatar Sep 02 '24 05:09 arandth