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

Add more thorough checks for when to adapt a value when resolving custom feature #306

Merged
merged 5 commits into from
May 22, 2024

Conversation

jhump
Copy link
Member

@jhump jhump commented May 10, 2024

As is, this code is unable to handle the following case:

  1. The descriptor includes dynamic extensions for all custom options and custom features.
  2. The user supplies a generated extension type, to query for the custom feature.

The code here ostensibly handles this. But the cases where it decides it needs to adapt the value (by marshalling and then unmarshalling into different type) isn't quite thorough enough. The issue is when the custom feature is a message. In that case, it works fine to query for the extension itself. But if we then need to extract a field from the message, the field descriptor is likely referring to a generated message type, whose descriptor is an instance in protoregistry.GlobalTypes. But the value is likely to be a dynamic message, whose descriptor may have been constructed dynamically (such as being parsed from source). In that case, the protoreflect stuff will happily panic when it sees the mismatch 😱.

Up to now, I had worked around this in a rather gross way: in the "buf breaking" implementation where I need to resolve custom features, I had a bunch of code that looks very similar to this stuff, to re-parse the features (here).

But if I simply beef up this function to do a slightly more thorough check for this case, then I can remove a bunch of that redundant (and less efficient) code from the "buf breaking" implementation.

@jhump
Copy link
Member Author

jhump commented May 10, 2024

Welp, let's hold off on review for now... This is not quite working as desired (broken tests in bufbuild/buf#2967).

I'll mark as ready for review after I get time to do more digging/troubleshooting/fixing.

@jhump jhump removed the request for review from emcfarlane May 10, 2024 20:42
@jhump jhump marked this pull request as draft May 10, 2024 20:42
@jhump jhump marked this pull request as ready for review May 20, 2024 17:03
@jhump
Copy link
Member Author

jhump commented May 20, 2024

I was able to step through the code to see where it was breaking. I added a test to cover that logic (which failed) and then added a fix. See the later two commits.

I also tested with bufbuild/buf to make sure that all of the tests pass there, too.

@jhump jhump requested a review from emcfarlane May 20, 2024 17:04
@jhump
Copy link
Member Author

jhump commented May 21, 2024

@emcfarlane, ping.

@jhump jhump merged commit 85801e4 into main May 22, 2024
8 checks passed
@jhump jhump deleted the jh/more-thorough-resolve-custom branch May 22, 2024 13:09
kralicky pushed a commit to kralicky/protocompile that referenced this pull request Jun 19, 2024
…tom feature (bufbuild#306)

Previously, the protoutil.ResolveCustomFeature code was unable to
reliably handle the following case:
1. The descriptor includes dynamic extensions for all custom options and
custom features.
2. The user supplies a _generated_ extension type, to query for the
custom feature.

The code here _ostensibly_ handles this. But the cases where it decides
it needs to adapt the value (by marshalling and then unmarshalling into
different type) isn't quite thorough enough. The issue is when the
custom feature is a _message_. In that case, it works fine to query for
the extension itself. But if we then need to extract a field from the
message, the field descriptor is likely referring to a generated message
type, whose descriptor is an instance in `protoregistry.GlobalTypes`.
But the value is likely to be a dynamic message, whose descriptor may
have been constructed dynamically (such as being parsed from source). In
that case, the `protoreflect` stuff will happily panic when it sees the
mismatch 😱.

This commit fixes this issue so that custom feature values can be
successfully queried under these conditions, without panic.

(cherry picked from commit 85801e4)
@jhump jhump mentioned this pull request Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants