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: [gapic-generator-java] align writer behavior for nested types #1709

Merged
merged 8 commits into from
Jun 5, 2023

Conversation

emmileaf
Copy link
Contributor

@emmileaf emmileaf commented May 24, 2023

This PR:

  • Removes duplicate handling of enclosing classes in JavaWriterVisitor and ImportWriterVisitor
    • Change to only import outermost class, e.g. com.example.Outer), while type references will be written as Outer.Middle.Inner
  • Fixes issue in ConcreteReference where multiple levels of enclosed classes are written in reversed order

Fixes #1708 ☕️

…asses

* To illustrate current unintended behavior
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label May 24, 2023
@emmileaf emmileaf marked this pull request as ready for review June 1, 2023 13:48
@emmileaf emmileaf requested a review from a team as a code owner June 1, 2023 13:48
@emmileaf emmileaf requested a review from blakeli0 June 1, 2023 13:55
Copy link
Member

@meltsufin meltsufin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No impact on the goldens?

@burkedavison
Copy link
Contributor

@meltsufin : No impact on the goldens?

If so, perhaps we can create a (very) simple extended showcase proto to exercise this behavior -- similar to how we did for validating REST-only client generation behavior?
https://github.com/googleapis/sdk-platform-java/tree/main/showcase/gapic-showcase-extended/proto

@emmileaf
Copy link
Contributor Author

emmileaf commented Jun 1, 2023

No impact on the goldens?

@meltsufin Not for this change. For a bit more context - this nested class import issue was initially uncovered when the generator mistakenly tried to pull in DescriptorProtos.GeneratedCodeInfo.Annotation, and this behavior is being fixed in #1726 (workaround in the specific proto was to fully specify package name).

If so, perhaps we can create a (very) simple extended showcase proto to exercise this behavior

@burkedavison I was considering this as well - actually tried doing this for validating the change in #1726, but this fix was easier to unit test so I just went with those instead. I can set up something similarly here if the approach taken in the other PR makes sense?

@blakeli0
Copy link
Collaborator

blakeli0 commented Jun 2, 2023

No impact on the goldens?

@meltsufin Not for this change. For a bit more context - this nested class import issue was initially uncovered when the generator mistakenly tried to pull in DescriptorProtos.GeneratedCodeInfo.Annotation, and this behavior is being fixed in #1726 (workaround in the specific proto was to fully specify package name).

If so, perhaps we can create a (very) simple extended showcase proto to exercise this behavior

@burkedavison I was considering this as well - actually tried doing this for validating the change in #1726, but this fix was easier to unit test so I just went with those instead. I can set up something similarly here if the approach taken in the other PR makes sense?

I agree with Mike and Burke that it would be better to have a golden test to easily see the changes. Showcase may be a little overkill since this does not interact with the server, a golden unit test would be a great fit.

ImmutableList.Builder<String> listBuilder = new ImmutableList.Builder<>();
Class<?> currentClz = clazz();
while (currentClz.getEnclosingClass() != null) {
listBuilder.add(currentClz.getEnclosingClass().getSimpleName());
currentClz = currentClz.getEnclosingClass();
}
return listBuilder.build();
return listBuilder.build().reverse();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we achieve the same thing without this change? Seems we can try to get the last element from the list in the writer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for the import writer, but the JavaWriterVisitor also relies on this ordering for the enclosing class writes.

This fix to ConcreteReference wasn't a part of the original issue investigated, but it looked like an implementation bug that perhaps didn't surface in generated code anywhere yet. I think the intended result of this enclosingClassNames call should have outer to inner ordering, given this original comment and upstream usages of this method in the generator.


writerVisitor.clear();

ConcreteReference nestedConcreteReference =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be break into two test cases if we want to test both type of references. However, I think we only need to test one of them if the only change is in the writer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the additional change in ConcreteReference is still needed given #1709 (comment). Breaking this into separate test cases makes sense though - will update.

@emmileaf
Copy link
Contributor Author

emmileaf commented Jun 2, 2023

I agree with Mike and Burke that it would be better to have a golden test to easily see the changes. Showcase may be a little overkill since this does not interact with the server, a golden unit test would be a great fit.

@blakeli0 Makes sense. Looks like there's actually a nested_message.proto that exists and is currently used in TypeParserTest, just without goldens. I'll try leveraging that to add some golden unit tests here.

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Jun 2, 2023
@emmileaf
Copy link
Contributor Author

emmileaf commented Jun 2, 2023

Added a test proto and new golden unit test in 6ab1628.

(Had some uncertainty deciding where best to put this golden test since the existing ones are mostly separate for each Composer, and all of these exercise the writer behavior implicitly. However, this fix is more specific to writer logic and could be applicable to a number of Composers and the class files they generate.

I ended up adding one of these into JavaWriterVisitorTest that uses the grpc ServiceClientClassComposer to write an example file with nested types and imports - please let me know if this approach makes sense!)

@emmileaf emmileaf requested a review from blakeli0 June 2, 2023 18:47
@sonarcloud
Copy link

sonarcloud bot commented Jun 2, 2023

[gapic-generator-java-root] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@sonarcloud
Copy link

sonarcloud bot commented Jun 2, 2023

[java_showcase_integration_tests] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Jun 2, 2023

[java_showcase_unit_tests] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@blakeli0
Copy link
Collaborator

blakeli0 commented Jun 2, 2023

Added a test proto and new golden unit test in 6ab1628.

(Had some uncertainty deciding where best to put this golden test since the existing ones are mostly separate for each Composer, and all of these exercise the writer behavior implicitly. However, this fix is more specific to writer logic and could be applicable to a number of Composers and the class files they generate.

I ended up adding one of these into JavaWriterVisitorTest that uses the grpc ServiceClientClassComposer to write an example file with nested types and imports - please let me know if this approach makes sense!)

Thanks Emily! I think your approach is fine, it's very specific to what we want to test. As you mentioned, our golden tests are currently assuming writers behave perfectly so it is a little weird to add a golden test just for writers. If you prefer, we can add this test RPC to one of the existing protos, so that it's tested through other golden tests and don't have to load it separately.

Copy link
Collaborator

@blakeli0 blakeli0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We could move the golden tests to composers or add the test case to one of the existing protos, but I don't think it should block this PR.

Copy link
Collaborator

@blakeli0 blakeli0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We could move the golden tests to composers or add the test case to one of the existing protos, but I don't think it should block this PR.

@emmileaf
Copy link
Contributor Author

emmileaf commented Jun 5, 2023

Sounds good - I'll merge this in since the fix is also independent of #1726, so that it can make it into the upcoming release!

@emmileaf emmileaf merged commit a21ffe8 into main Jun 5, 2023
18 checks passed
@emmileaf emmileaf deleted the writer-nested-types branch June 5, 2023 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gapic-generator-java: discrepancy in writers handling deeply nested types
4 participants