@@ -42,29 +42,40 @@ wrappers[".google.protobuf.Any"] = {
4242
4343 // unwrap value type if mapped
4444 if ( object && object [ "@type" ] ) {
45- var type = this . lookup ( object [ "@type" ] ) ;
45+ // Only use fully qualified type name after the last '/'
46+ var name = object [ "@type" ] . substring ( object [ "@type" ] . lastIndexOf ( "/" ) + 1 ) ;
47+ var type = this . lookup ( name ) ;
4648 /* istanbul ignore else */
4749 if ( type ) {
4850 // type_url does not accept leading "."
4951 var type_url = object [ "@type" ] . charAt ( 0 ) === "." ?
5052 object [ "@type" ] . substr ( 1 ) : object [ "@type" ] ;
5153 // type_url prefix is optional, but path seperator is required
54+ if ( type_url . indexOf ( "/" ) === - 1 ) {
55+ type_url = "/" + type_url ;
56+ }
5257 return this . create ( {
53- type_url : "/" + type_url ,
58+ type_url : type_url ,
5459 value : type . encode ( type . fromObject ( object ) ) . finish ( )
5560 } ) ;
5661 }
5762 }
58-
63+
5964 return this . fromObject ( object ) ;
6065 } ,
6166
6267 toObject : function ( message , options ) {
6368
69+ // Default prefix
70+ var googleApi = "type.googleapis.com/" ;
71+ var prefix = "" ;
72+
6473 // decode value if requested and unmapped
6574 if ( options && options . json && message . type_url && message . value ) {
6675 // Only use fully qualified type name after the last '/'
6776 var name = message . type_url . substring ( message . type_url . lastIndexOf ( "/" ) + 1 ) ;
77+ // Separate the prefix used
78+ prefix = message . type_url . substring ( 0 , message . type_url . lastIndexOf ( '/' ) + 1 ) ;
6879 var type = this . lookup ( name ) ;
6980 /* istanbul ignore else */
7081 if ( type )
@@ -74,7 +85,14 @@ wrappers[".google.protobuf.Any"] = {
7485 // wrap value if unmapped
7586 if ( ! ( message instanceof this . ctor ) && message instanceof Message ) {
7687 var object = message . $type . toObject ( message , options ) ;
77- object [ "@type" ] = message . $type . fullName ;
88+ var messageName = message . $type . fullName [ 0 ] === "." ?
89+ message . $type . fullName . substr ( 1 ) : message . $type . fullName ;
90+ // Default to type.googleapis.com prefix if no prefix is used
91+ if ( prefix === "" ) {
92+ prefix = googleApi ;
93+ }
94+ var name = prefix + messageName ;
95+ object [ "@type" ] = name ;
7896 return object ;
7997 }
8098
0 commit comments