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

Fix ClassCastException if expected type is Object #11510

Merged
merged 1 commit into from
Feb 5, 2022

Conversation

lbilger
Copy link
Contributor

@lbilger lbilger commented Feb 3, 2022

The currently generated code will throw a ClassCastException if type is Object. I'm not quite sure why isAssignableFrom is used here but I tried to keep the change minimal - in my understanding, the result would be the same if the line was

if(Types.getRawType(type).equals(ApiResponse.class)) {

because the ApiResponse only extends Object and implements no interfaces. Maybe it was meant to be ApiResponse.class.isAssignableFrom(Types.getRawType(type))? This would also permit subclasses of ApiResponse, but then it would be more complicated to determine the actual type parameter (because type itself may not be parameterized) and to create the object to return (because it would have to be an instance of the subclass).

Fixes #11509

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/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.

The build did not work locally, probably because I don't have Java 8 installed anyomore. As it's a really small change, I hope that will be ok...

The currently generated code will throw a `ClassCastException` if `type` is `Object`. I'm not quite sure why `isAssignableFrom` is used here but I tried to keep the change minimal - in my understanding, the result would be the same if the line was 
```
if(Types.getRawType(type).equals(ApiResponse.class)) {
``` 
because the `ApiResponse` only extends `Object` and implements no interfaces. Maybe it was meant to be `ApiResponse.class.isAssignableFrom(Types.getRawType(type))`? This would also permit subclasses of `ApiResponse`, but then it would be more complicated to determine the actual type parameter (because `type` itself may not be parameterized) and to create the object to return (because it would have to be an instance of the subclass).
@wing328
Copy link
Member

wing328 commented Feb 5, 2022

Tested locally with updated samples and the result is ok

[INFO] Building jar: /Users/williamcheng/Code/openapi-generator2/samples/client/petstore/java/feign/target/petstore-feign-1.0.0-javadoc.jar
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar-no-fork (attach-sources) @ petstore-feign ---
[INFO] Building jar: /Users/williamcheng/Code/openapi-generator2/samples/client/petstore/java/feign/target/petstore-feign-1.0.0-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  32.095 s
[INFO] Finished at: 2022-02-05T14:42:21+08:00
[INFO] ------------------------------------------------------------------------

@wing328 wing328 merged commit 1a14d9e into OpenAPITools:master Feb 5, 2022
@wing328
Copy link
Member

wing328 commented Feb 5, 2022

@lbilger thanks for the PR, which has been merged. Next time please follow step 3 in the PR checklist so that the CI can verify the results with updated samples.

@wing328 wing328 added this to the 6.0.0 milestone Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Java Feign Client produces ClassCastException for methods returning Object
2 participants