openapi-generator
openapi-generator copied to clipboard
[BUG] [JAVA] when using schemaMapping for models in collections, not compilable code with @Valid is generated
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
Confirming also affecting us with Java 21. Downgrading to openapi-generator 7.0.1 works as a workaround for the time being.
Duplicate of #17647
@arandth can you check if my PR fixes your issue?
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, 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
@arandth I've added a test for your specific case.
To test it, I think you just have to
- clone/copy from https://github.com/jpfinne/openapi-generator/tree/fix17472
- mvn install
- use 7.3.0-SNAPSHOT from your local repository
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 :/'
The checkout is not usable (millions of deleted files) Note: I'm working on Windows....
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)
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
Hi @jpfinne Any updates here?
Hi everybody is there anybody else to help on this? @jpfinne unfortunately doesn't answer any more :-(
see #19093
Great! Anything what I need to do?
Great! Anything what I need to do?
Thanks. Maybe reviewing/testing the code and ask wing328 to merge it (in the PR) ?
@wing328 Could you please review and merge the PR?
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.
Hi @jpfinne @wing328 any ideas how to proceed here??? Regards, Thomas