OpenAPITools / OpenAPITools/openapi-generator
[BUG][Kotlin] Current code is generating invalid code (that will never run)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- What's the version of OpenAPI Generator used? 4.0.0
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
Description
When using an API with a "huge" type, the generator creates a data class that is invalid.
In our case, the data-class has 166 double properties. This implies its constructor (and copy() method) will go beyond the maximum arity of Java function (255, double and long arguments count as 2 in this case).
Please refer to the JVM spec:
https://cr.openjdk.java.net/~iris/se/11/latestSpec/java-se-11-jvms-draft-diffs.pdf => p. 352
There are no compiler error/warning and the runtime error is somewhat cryptic:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: PROC, PID: PID
java.lang.VerifyError: Verifier rejected class CALLER-CLASS: void CALLER-METHOD() failed to verify: void CALLER-METHOD(): [0x119] Rejecting invocation, expected 1 argument registers, method signature has 2 or more (declaration of 'CALLER-CLASS' appears in APK-PATH
The "expected N arguments registers" being in my case (NUMBER-OF-PROPERTIES * 2 + 1) % 256. It took quite a while before being able to track down the issue. That's typically something I'd expect the IDE to raise at compile-time.
An issue has been opened with Android Studio to improve DX around this: https://issuetracker.google.com/issues/134100430 (I don't think anonymous access is allowed, unfortunately)
There are, unfortunately, no direct mention of this restriction in the Kotlin documentation. I have open a PR with Kotlin docs regarding this limitation: https://github.com/JetBrains/kotlin-web-site/pull/1396
openapi-generator version
4.0.0
As far as I can tell, this has always been here.
OpenAPI declaration file content or url
https://gist.github.com/lemoinem/0b3d7d065d9337a898d5ee2bc49654a2
Command line used for generation
./bin/kotlin-client-string.sh while the yaml is stored at modules/openapi-generator/src/test/resources/2_0/petstore.yaml
Steps to reproduce
- Run the command line
- Instanciate the generated class or deserialize any JSON of it in code
- Build & Debug
Related issues/PRs
None that I could found.
However, keep in mind this is a JVM restriction, all the Kotlin generators are impacted.
Other generators targeting the JVM might be impacted as well, including:
- all the Java generators
- Groovy
- android
- apex
- clojure
- jmeter
Suggest a fix
I'm not sure what the fix could be. Instinctively, I'd expect the class to be broken down in parts so the language accept them...
However, this might create some issues with the serialization/deserialization process, I'm not sure how easy this would be.
I also agree this is an edge case, and such class should have very few reasons to exist in the first place. Simply triggering a generation error and preventing the code from being generated sounds acceptable as well.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running bin/kotlin-client-string.sh with the declaration in modules/openapi-generator/src/test/resources/2_0/petstore.yaml and inspect the generated data class. Reproduce the JVM arity failure by instantiating or deserializing the class, then trace the Kotlin generator path that creates its constructor and copy method. Done means generated code cannot produce an invalid JVM class, either by handling the model or rejecting it clearly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100