Custom Serializer Error

Hi,

I tried to follow the example here


to use a custom serializer, but with my code below I got this error when
calling n.rpc.service_a.hello("John") from the nameko shell: Can't decode
message body: ContentDisallowed('Refusing to deserialize untrusted content
of type json (application/json)',) (type:u'application/json'
encoding:u'utf-8' raw:'u\'{"args": ["John"], "kwargs": {}}\''')

#custom_serializer.py

import json
from kombu.serialization import register
from nameko.rpc import rpc

def encode(value):
    value = json.dumps(value)
    return value.upper()

def decode(value):
    value = value.lower()
    return json.loads(value)

register("upperjson", encode, decode, "application/x-upper-json", "utf-8")

class ServiceA(object):
    name = "service_a"

    @rpc
    def hello(self, name):
        return "Hello, {}!".format(name)

I used this as my config.yaml:
serializer: 'upperjson'
AMQP_URI: 'pyamqp://guest:guest@localhost:5672'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10

And ran this:
$ nameko run --config ./config.yaml custom_serializer

I'm running nameko 2.5.1, rabbitmq-server 3.2.4-1, and python 2.7 on Ubuntu
14.04

Where did I go wrong? Any help would be awesome.

Thanks,
Pitoon

Hi Pitoon,

I'm pretty sure the problem is that your `nameko shell` doesn't have the
custom serializer registered, and so is sending messages with the default
(json) serializer, which your service now doesn't like (since it's
expecting upperjson)

unfortunately, just using the same config.yaml for `nameko shell` also
doesn't work, since it won't be able to find the serializer code and
refuses to start. i guess this is a bug/missing feature. need to think
about how to best load custom code like this into the shell

just to be clear, two regular services can happily communicate using custom
serializers, but the shell currently can't talk to such services

please raise this on the issue
tracker https://github.com/nameko/nameko/issues to help us track it.

best,
david

···

On Thursday, 9 February 2017 06:34:36 UTC, 7oo...@gmail.com wrote:

Hi,

I tried to follow the example here
https://github.com/nameko/nameko/blob/master/test/test_serialization.py#L176-L209
to use a custom serializer, but with my code below I got this error when
calling n.rpc.service_a.hello("John") from the nameko shell: Can't decode
message body: ContentDisallowed('Refusing to deserialize untrusted content
of type json (application/json)',) (type:u'application/json'
encoding:u'utf-8' raw:'u\'{"args": ["John"], "kwargs": {}}\''')

#custom_serializer.py

import json
from kombu.serialization import register
from nameko.rpc import rpc

def encode(value):
    value = json.dumps(value)
    return value.upper()

def decode(value):
    value = value.lower()
    return json.loads(value)

register("upperjson", encode, decode, "application/x-upper-json", "utf-8")

class ServiceA(object):
    name = "service_a"

    @rpc
    def hello(self, name):
        return "Hello, {}!".format(name)

I used this as my config.yaml:
serializer: 'upperjson'
AMQP_URI: 'pyamqp://guest:guest@localhost:5672'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10

And ran this:
$ nameko run --config ./config.yaml custom_serializer

I'm running nameko 2.5.1, rabbitmq-server 3.2.4-1, and python 2.7 on
Ubuntu 14.04

Where did I go wrong? Any help would be awesome.

Thanks,
Pitoon