-
Notifications
You must be signed in to change notification settings - Fork 686
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
SOLR-17326: Fix wrong references in generated API classes #2510
SOLR-17326: Fix wrong references in generated API classes #2510
Conversation
Hey @malliaridis - thanks for the fix, and thanks even more for the detailed writeup. I wish everyone (myself included) was so thorough in listing out other potential approaches. This is awesome! Ultimately I think the approach you took here is the best one available for now. As you pointed out, it does have the downside that it requires all response classes to live in the same package. I agree this is non-ideal, but something we can live with. In fact, there are actually one or other two places where the template relies on the "single package" assumption. I tried hard to avoid it in the initial implementation, but couldn't crack it entirely at the time. In hindsight I think we could probably do something based on vendor-extension annotations (like the one used in SelectApi), but that'd require a larger effort than just fixing this bug.
I think this would probably be OK as a longer-term solution. Though I wonder about a few APIs that return "blobs" of data (e.g. returning a single file out of a configset). These responses are "just" raw data, so they don't quite fit into the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM!
@malliaridis - do you mind if I take this out of "Draft" mode and start working towards merge? I'll likely make a few minor changes (add a CHANGES.txt entry to give you credit for the fix, tweak the test Javadoc), but otherwise this looks ready for a committer to get involved and get it closed out.
Thanks for the positive feedback, it means a lot.
If this could be addressed then adding additional type-safety would definitely be something for looking forward to.
Go ahead and thank you for taking over. :) |
Also makes some minor Javadoc tweaks.
Thanks for the fix @malliaridis ! (FYI - I've filed SOLR-17329 to address the type-narrowing you suggested adding to JacksonParsingResponse. So we can take care of that at some point too!) |
A handful of the v2 SolrRequest implementations generated by our OAS spec relied on response model classes whose names conflicted with other (unrelated) classes in solrj. This caused errors at request time as JacksonParsingResponse would try to deserialize the JSON, XML, etc. response body into these unintended classes. This commit fixes this by modifying the 'api.mustache' template so that generated SolrRequest classes now reference their response model using the fully-qualified classname (i.e. including the package). This resolves the ambiguity. --------- Co-authored-by: Jason Gerlowski <[email protected]>
https://issues.apache.org/jira/browse/SOLR-17326
Description
Adds a test and fixes SOLR-17326 by using the model package in affected class references.
Solution
In the
java-template/api.mustache
themodelPackage
is used to add the packge toreturnType
references, so that in case of a naming conflict, the correct class is still used.Tests
The new test uses an existing generated response class that references the wrong class in the return type. A dummy response is created and parsed, then the class of the parsed data is checked. During the test, if a conflict is present, parsing errors may occur if the data contains unknown fields that are not ignored.
Checklist
Please review the following and check all that apply:
main
branch../gradlew check
.Additional Information
It is possible to add a generics bound in
JacksonParsingResponse<T>
that limitsT
to only classes that extendSolrJerseyResponse
, but this will limit the possible classes forT
. This would ensure potential result in compile errors if tWith the use of fully qualified names in the current solution, some imports in the generated classes are redundant but cannot be excluded due to the way they are added. The solution implemented here is not perfect, as it cannot be guaranteed that the return type class is always in the model package. The same applies for the test, as it cannot be guaranteed that in future there won't be any classes that do not extend
SolrJerseyResponse
. However, in the current state of the project, all API return types extendSolrJerseyResponse
.Alternative slutions that were considered:
1. Rename the API endpoint return types
This solution would solve the naming conflicts, but cannot guarantee future conflicts without additional "guards" / checks. Additionaly, this would introduce changes in the OpenAPI and API module that users may depend on.
2. Limit the allowed types in
JacksonParsingResponse<T>
with a generics upper boundThis option would introduce a guard that would break compilation, identifying conflicts early on. However, this would require additional actions (e.g. option 1) to fix existing conflicts. Additionally, return types of newly added endpoints would always have to inherit from
SolrJerseyResponse
(which is currently the case and therefore checked in the added test).