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

static js module encode issue when oneof objects. #644

Closed
ehallander9591 opened this Issue Jan 13, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@ehallander9591

ehallander9591 commented Jan 13, 2017

protobuf.js version: <6.5.0>

In the generated Static Module code, complex oneof components are initialized to null, but the discriminator getter function checks against undefined

syntax = "proto3";

package test;

message SomeContent {
	string someString = 1;
}

message SomeOtherContent {
	uint32 someInt = 1;
    string someString = 2;
}

message TestMessage {
	string someId = 1;
    oneof oneOrTheOther {
	    SomeContent someContent = 2;
	    SomeOtherContent someOtherContent = 3;
	}
}

The create for TestMessage shows

        test.TestMessage = (function() {
    
            /**
             * Constructs a new TestMessage.
             * @exports test.TestMessage
             * @constructor
             * @param {Object} [properties] Properties to set
             */
            function TestMessage(properties) {
                if (properties) {
                    var keys = Object.keys(properties);
                    for (var i = 0; i < keys.length; ++i)
                        this[keys[i]] = properties[keys[i]];
                }
            }
    
            /** @alias test.TestMessage.prototype */
            var $prototype = TestMessage.prototype;
    
            /**
             * TestMessage someId.
             * @type {string}
             */
            $prototype.someId = "";
    
            /**
             * TestMessage someContent.
             * @type {test.SomeContent}
             */
            $prototype.someContent = null;
    
            /**
             * TestMessage someOtherContent.
             * @type {test.SomeOtherContent}
             */
            $prototype.someOtherContent = null;
    

and when encode does a switch on message.oneOrTheOther
it triggers this

            Object.defineProperty($prototype, "oneOrTheOther", {
                get: function() {
                    if (this["someContent"] !== undefined)
                        return "someContent";
                    if (this["someOtherContent"] !== undefined)
                        return "someOtherContent";
                    return undefined;
                },

<please paste the stack trace of the error if applicable>
@ehallander9591

This comment has been minimized.

ehallander9591 commented Jan 13, 2017

I should have finished the statement. If I set someOtherContent and then try to encode the TestMessage, the above get will return "someContent" because $prototype.someContent == null which !== undefined, and then a nice stack trace will get issued when SomeContent.encode is called with a null object.

Cheers

@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented Jan 14, 2017

Hmm, I'd even go a bit further and say that also everything with a default value on the prototype other than undefined leads to incorrect return values. Will probably even be necessary to port what's used for reflection to static code.

@dcodeIO dcodeIO added the bug label Jan 14, 2017

dcodeIO added a commit that referenced this issue Jan 14, 2017

@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented Jan 14, 2017

This now re-uses some common runtime utility derived from reflection within static code (example output). Let me know if there are any issues!

@ehallander9591

This comment has been minimized.

ehallander9591 commented Jan 14, 2017

That is a working solution.

Thanks

Since my oneof block was all complex messages, it wasn't until I was driving home that I thought about how that code was going to work for primitives

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment