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

"Google Protobuf Any" javscript example #68

Open
psaelango opened this issue Jul 10, 2018 · 14 comments
Open

"Google Protobuf Any" javscript example #68

psaelango opened this issue Jul 10, 2018 · 14 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request javascript triaged Issue has been triaged

Comments

@psaelango
Copy link

psaelango commented Jul 10, 2018

Can anyone please provide an example for https://developers.google.com/protocol-buffers/docs/proto3#any

I couldn't able to use the example provided in protocolbuffers/protobuf#2612

@xfxyjwf xfxyjwf added question Further information is requested javascript labels Jul 12, 2018
@xfxyjwf xfxyjwf added enhancement New feature or request documentation Improvements or additions to documentation and removed question Further information is requested labels Jul 30, 2018
@xfxyjwf
Copy link

xfxyjwf commented Jul 30, 2018

Bo, we need to add documentation for Any API in javascript.

@mwei0210
Copy link

mwei0210 commented Aug 8, 2018

Any documentation or guide for Any?

@TeBoring
Copy link

    var any = new proto.google.protobuf.Any();
    var msg = new proto.jspb.test.TestAllTypes();

    fillAllFields(msg);

    any.pack(msg.serializeBinary(), 'jspb.test.TestAllTypes');

    assertEquals('type.googleapis.com/jspb.test.TestAllTypes',
                 any.getTypeUrl());

    var msg2 = any.unpack(
        proto.jspb.test.TestAllTypes.deserializeBinary,
        'jspb.test.TestAllTypes');

    checkAllFields(msg, msg2);

@TeBoring
Copy link

https://github.com/google/protobuf/blob/master/js/binary/proto_test.js#L646-L662

@chrisplusplus
Copy link

Man the documentation for javascript is scarce. But for any one using the google-protobuf node module in angular, you can include the use of Any type in the below fashion. I recommend anyone using the javascript API to look through the tests because you won't find documentation as of yet.

var google_protobuf_any_pb = require('google-protobuf/google/protobuf/any_pb.js');
let any_pb = new google_protobuf_any_pb.Any;

@clehene
Copy link

clehene commented Feb 19, 2019

@TeBoring I found the unit test, however I can't understand how that can be useful as documentation for how to actually use the code. Obviously if you have the actual message type of a byte string, you can deserialize it. You don't even need the type url string.

When consuming an Any field you only have access to the serialized byte string and the type URL.
Hence you need to somehow get the actual type from the URL, which doesn't seem to be documented anywhere, unless I'm missing something.

Concretely, here's how the Java implementation looks like

    java.lang.Class<T> clazz)
      throws com.google.protobuf.InvalidProtocolBufferException {
    if (!is(clazz)) {
      throw new com.google.protobuf.InvalidProtocolBufferException(
          "Type of the Any message does not match the given class.");
    }
    if (cachedUnpackValue != null) {
      return (T) cachedUnpackValue;
    }
    T defaultInstance =
        com.google.protobuf.Internal.getDefaultInstance(clazz);
    T result = (T) defaultInstance.getParserForType()
        .parseFrom(getValue());
    cachedUnpackValue = result;
    return result;
  }
  public static final int TYPE_URL_FIELD_NUMBER = 1;
  private volatile java.lang.Object typeUrl_;

Is there an equivalent of com.google.protobuf.Internal.getDefaultInstance(clazz) for javascript?

@chrisplusplus
Copy link

I don't see any way in Javascript to do that. I work around this by including the type, as an enum, as part of my message struct and then using a switch case to properly unpack the any. Not dynamic I know, but it's the only way I know to do it.

@clehene
Copy link

clehene commented Feb 19, 2019

@chrisplusplus that's what I ended up doing too

@imraoarvind
Copy link

Is there any way to dynamic create/use NestJs (gRPC/proto3) nestjs

@acozzette acozzette transferred this issue from protocolbuffers/protobuf May 16, 2022
@dibenede
Copy link
Contributor

dibenede commented Sep 23, 2022

You need to know the type of the Any in order to work with it. There is intentionally no type registry and it's reasonable to use a map of a type -> constructor using getTypeName().

We can improve the documentation to better illustrate this.

@dibenede dibenede added the triaged Issue has been triaged label Sep 23, 2022
@chrisplusplus
Copy link

chrisplusplus commented Sep 23, 2022 via email

@lukesandberg
Copy link
Contributor

Java Lite protos similarly don't provide registries to support any

The goal in both cases is the same, don't leak type names by default. As such it does shift a burden onto users.

@chrisplusplus
Copy link

chrisplusplus commented Sep 24, 2022 via email

@lukesandberg
Copy link
Contributor

This is intentionally unimplemented. You are correct that we could modify our generated code so that every message would know its own name and thus simply the pack API and others like it.

The tradeoff would be that it would 1. increase code size 2. leak type names for all types.

By forcing developers to pass these strings themselves, only the type names of any proto payloads are leaked (and they are already leaked by design).

For teams using this product to build websites it is, at least sometimes, important to ensure that features don't leak accidentally.

Other proto implementations make different tradeoffs because they have different target use-cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request javascript triaged Issue has been triaged
Projects
None yet
Development

No branches or pull requests

9 participants