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

fromDescriptor with Custom Options #1072

Open
MarcusLongmuir opened this issue Jun 22, 2018 · 0 comments
Open

fromDescriptor with Custom Options #1072

MarcusLongmuir opened this issue Jun 22, 2018 · 0 comments

Comments

@MarcusLongmuir
Copy link

MarcusLongmuir commented Jun 22, 2018

protobuf.js version: 6.8.7 - latest master as of raising.

I have a use case that requires decoding a descriptor that uses options. Options are implemented as extensions on the MessageOptions message type used in the Descriptor.

The issue arises when decoding a descriptor that contains:

  • Multiple messages that use this custom option.
  • The custom message type that is the value type of the option.
  • The extension of the MessageOptions message type by this custom message type.

The most fundamental issue is that the extension is added to a new instance of the MessageOptions message type that exists in the new Root, which isn't actually the MessageOptions being used to decode the rest of the descriptor (the one being used to decode is loaded from the descriptor.json file and is accessed via the exports property.

I tried replacing the exports used by ext/descriptor/index.js (E.g. exports.MessageOptions) with the google.protobuf namespace that was extracted from my descriptor that had the extended MessageOptions Type. This works, but requires that the field names are camelCased as the field names in the JSON used to populate the initial exports are camelCased. The default behaviour of decoding is to use the original snake_case as that's what the strings are in the descriptor.

So to reiterate - my solution is to call Root.fromDescriptor() once to get the google.protobuf Namespace that contains the extended MessageOptions and then call Root.fromDescriptor() with the google.protobuf namespace in place of the original exports to get the full descriptor contents:

const firstPassRoot = protobufjs.Root.fromDescriptor(descriptorUint8);

const protobufNamespace = firstPassRoot.lookup("google.protobuf");

const root = protobufjs.Root.fromDescriptor(descriptorUint8, protobufNamespace);

I thought I'd describe the problem in an issue to avoid polluting the PR I've raised with a feature request.

The required code changes to fromDescriptor are in #1073.

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

No branches or pull requests

1 participant