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

handle 'type.googleapis.com/' string #850

Closed
FoghostCn opened this issue Jun 27, 2017 · 5 comments
Closed

handle 'type.googleapis.com/' string #850

FoghostCn opened this issue Jun 27, 2017 · 5 comments
Labels

Comments

@FoghostCn
Copy link

FoghostCn commented Jun 27, 2017

protobuf.js version: 6.8.0

in google's any.proto file I find this document:

The pack methods provided by protobuf library will by default use
'type.googleapis.com/full.type.name' as the type URL and the unpack
methods only use the fully qualified type name after the last '/'
in the type URL, for example "foo.bar.com/x/y.z" will yield type
name "y.z".

and I have seen your code about any type

wrappers[".google.protobuf.Any"] = {
    fromObject: function(object) {
        // unwrap value type if mapped
        if (object && object["@type"]) {
            var type = this.lookup(object["@type"]);
            ........
        }
        return this.fromObject(object);
    },
    toObject: function(message, options) {
        // decode value if requested and unmapped
        if (options && options.json && message.type_url && message.value) {
            var type = this.lookup(message.type_url);
        .......
        return this.toObject(message, options);
    }
};

fromObject and toObject method didn't handle type.googleapis.com/ string

actually Msg.decode(buffer).toJSON() didn't auto decode Any type with type_url =type.googleapis.com/myMsg but can auto decode .myMsg

did I missing something?

@dobryanskyy
Copy link

Hi @FoghostCn , did you manage to find out how to make this working?

@FoghostCn
Copy link
Author

FoghostCn commented Feb 12, 2018

@dobryanskyy I have to override the two method fromObject and toObject with follow

protobufjs.wrappers['.google.protobuf.Any'] = {
    fromObject(object) {
        if (object && object["@type"]) {
            const t = object["@type"];
            const type = this.lookup(t);
            if (type) {
                return this.create({
                    type_url: `type.googleapis.com/${t}`,
                    value: type.encode(type.fromObject(object)).finish()
                });
            }
        }
        return this.fromObject(object);
    },
    toObject(message, options) {
        if (options && options.json && message.type_url && message.value) {
            const type = this.lookup(message.type_url.replace('type.googleapis.com/', ''));
            if (type) {
                message = type.decode(message.value);
            }
        }
        if (!(message instanceof this.ctor) && message instanceof protobufjs.Message) {
            const object = message.$type.toObject(message, options);
            const name = message.$type.fullName;
            object["@type"] = name.charAt(0) === '.' ? name.substr(1) : name;
            return object;
        }
        return this.toObject(message, options);
    }
};

@dobryanskyy
Copy link

great, thanks

@nickpestov
Copy link
Contributor

nickpestov commented Jun 7, 2018

@dcodeIO, are there any plans to add the type.googleapis.com/ prefix to @type?
Currently, if we follow Google's Any.proto docs and include the prefix, fromObject fails to lookup the type and toObject sets a @type that is not compatible with what other clients expect.
Would submitting the above as a PR help progress this?

@johncsnyder
Copy link

+1 I'm also running into this issue. Ive tested out the PR #1068 locally and its working great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants