singer-io / singer-tap-template

GNU Affero General Public License v3.0
68 stars 30 forks source link

cookiecutter write_schema(): JSON not serializable #13

Open prae04 opened 4 years ago

prae04 commented 4 years ago

When following documentation for setting up tap-template, user in Slack first reported issue with executing singer.write_schema(). Have to call to dict to_dict() on schema. See Slack message: https://singer-io.slack.com/archives/C2TGFCZEV/p1584891258008800

lpillmann commented 4 years ago

Since it seems Singer Slack channel is free (thus has message limits and will make history not accessible), I'm copy-pasting the message with the workaround solution:


Author: Steven Ram

Hi. Just starting out with Singer, and currently building a tap on the cookiecutter Python template (https://github.com/singer-io/singer-tap-template).

I keep running into a TypeError: Schema(...) is not JSON serializable in

singer.write_schema(
    stream_name=stream.tap_stream_id,
    schema=stream.schema,
    key_properties=stream.key_properties
)

unless I convert Schema to dict first

singer.write_schema(
    stream_name=stream.tap_stream_id,
    schema=stream.schema.to_dict(),
    key_properties=stream.key_properties
)

Is there a better, recommended way than in the cookiecutter template to build the Schema, so as to avoid this?


Thanks also to Rebecca Sanjabi for linking to Steven's original message.

I tested and the solution works.

ednarb29 commented 4 years ago

Awesome guys! I was running into the same problem, and your solution works for me, too. Thanks!

dgitis commented 3 years ago

I had the same problem that I managed to trace back further upstream.

It seems like the catalog command line argument isn't getting passed so that sync mode is falling back to calling discover instead of reading the catalog.

if args.catalog: catalog = args.catalog else: catalog = discover() sync(args.config, args.state, catalog)

It seems discover() is returning the Schema class which is not JSON Serializable.

Here's the traceback I get when running the following command: ~/.virtualenvs/tap-woocommerce/bin/tap-woocommerce --config tap-woocommerce/config.json --catalog tap-woocommerce/catalog.json | ~/.virtualenvs/target-csv/bin/target-csv

File "/home/user/.virtualenvs/tap-woocommerce/bin/tap-woocommerce", line 11, in <module> load_entry_point('tap-woocommerce', 'console_scripts', 'tap-woocommerce')() File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/singer/utils.py", line 229, in wrapped return fnc(*args, **kwargs) File "/c/Users/user/dev/singer/tap-woocommerce/tap_woocommerce/__init__.py", line 106, in main sync(args.config, args.state, catalog) File "/c/Users/user/dev/singer/tap-woocommerce/tap_woocommerce/__init__.py", line 67, in sync key_properties=stream.key_properties, File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/singer/messages.py", line 269, in write_schema bookmark_properties=bookmark_properties)) File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/singer/messages.py", line 226, in write_message sys.stdout.write(format_message(message) + '\n') File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/singer/messages.py", line 222, in format_message return json.dumps(message.asdict(), use_decimal=True) File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/simplejson/__init__.py", line 380, in dumps return _default_encoder.encode(obj) File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/simplejson/encoder.py", line 291, in encode chunks = self.iterencode(o, _one_shot=True) File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/simplejson/encoder.py", line 373, in iterencode return _iterencode(o, 0) File "/home/user/.virtualenvs/tap-woocommerce/lib/python3.6/site-packages/simplejson/encoder.py", line 268, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: Schema(type='object', properties={'order_id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'order_key': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'status': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'date_created': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format='date-time', additionalProperties=None, patternProperties=None), 'date_modified': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format='date-time', additionalProperties=None, patternProperties=None), 'discount_total': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'shipping_total': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'total': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'line_items': Schema(type=['null', 'array'], properties=None, items=Schema(type=['null', 'object'], properties={'id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'name': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'product_id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'variation_id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'quantity': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'subtotal': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'subtotal_tax': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'total': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'sku': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'price': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None)}, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'shipping_lines': Schema(type=['null', 'array'], properties=None, items=Schema(type=['null', 'object'], properties={'id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'method_title': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'method_id': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'total': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None)}, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'coupon_lines': Schema(type=['null', 'array'], properties=None, items=Schema(type=['null', 'object'], properties={'id': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'code': Schema(type=['null', 'string'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), 'discount': Schema(type=['null', 'number'], properties=None, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None)}, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None), selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None)}, items=None, selected=None, inclusion=None, description=None, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None, maxLength=None, minLength=None, anyOf=None, format=None, additionalProperties=None, patternProperties=None) is not JSON serializable

The fixes proposed earlier work, but hopefully this helps solve the root problem.

tanguyantoine commented 1 year ago

https://github.com/singer-io/singer-tap-template/pull/17 fixes the issue