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.11k stars 6.39k forks source link

[BUG] [JAVA] Schema Mapping with generics skips wrong path, fails with file error on windows #19167

Closed Xiphoseer closed 2 weeks ago

Xiphoseer commented 1 month ago

Bug Report Checklist

Description

When I specify a schema mapping (aka schemaMappings map in a config.yaml), where the mapped value contains a java type with generic parameters (e.g. Wrapper<Thing>), then the code generation aborts with

Exception in thread "main" java.lang.RuntimeException: Could not process model 'ThingWrapper'.Please make sure that your schema is correct!
[...]
Caused by: java.nio.file.InvalidPathException: Illegal char <<> at index 53: .\src\main\java\org\openapitools\client\model\Wrapper<Thing>.java

at https://github.com/OpenAPITools/openapi-generator/blob/2abcb4f7f0aa67c126304e43602ae72a83f2dca7/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L491

openapi-generator version

At least since 7.4.0, still broken on master

OpenAPI declaration file content or url

https://gist.github.com/Xiphoseer/05c1b92d36ff3beef1e7e84fc476ccf9

Generation Details
Steps to reproduce

It's possible / likely this particular error only occurs on windows, Linux will let me create a file with < in the name just fine, but my understanding is that just hides the underlying issue.

Related issues/PRs

None found

Suggest a fix

I'd suggest removing the entire loop marked as "HACK": https://github.com/OpenAPITools/openapi-generator/blob/2abcb4f7f0aa67c126304e43602ae72a83f2dca7/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L488-L493

It doesn't quite do what it's supposed to (skip generation of model templated files for models that are schema mapped), because toModelFilename for Java calls toModelName which considers schemaMappings and thus acts on the mapped model name and file, not the file it would generate without the schema mapping. Additionally, the function where this happens seems to ensure anyway (using continue after the loop) that the schema will not be imported for generation.

This overload in AbstractJavaCodegen is what causes the issue, but it is a valid implementation since Java does require the class and file name to match: https://github.com/OpenAPITools/openapi-generator/blob/2abcb4f7f0aa67c126304e43602ae72a83f2dca7/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L930-L933

The default implementation is not susceptible to this because it's stateless: https://github.com/OpenAPITools/openapi-generator/blob/2abcb4f7f0aa67c126304e43602ae72a83f2dca7/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L1637-L1639

So far I did not reason about the other generators.

Xiphoseer commented 1 month ago

https://github.com/search?q=repo%3AOpenAPITools%2Fopenapi-generator+%22String+toModelFilename%28%22&type=code shows a bunch of toModelFilename overloads that delegate to model name mappings / toModelName or schemaMappings directly, so this issue applies to more generators than just Java.