avro-to-python icon indicating copy to clipboard operation
avro-to-python copied to clipboard

Code generation fails if a map contains a union in`"values"`

Open IMax153 opened this issue 3 years ago • 0 comments

From the Avro documentation on the Union schema:

Unions, as mentioned above, are represented using JSON arrays. For example, ["null", "string"] declares a schema which may be either a null or string.

Using the following example schema:

{"namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]},
     {"name": "event", "type": { "type": "map", "values": ["null", "boolean", "double", "long", "string"], "default": {} } }
 ]
}

I get the following error:

Traceback (most recent call last):
  File "/usr/local/bin/avro-to-python", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/cli.py", line 33, in main
    reader.read()
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/reader/read.py", line 81, in read
    self._build_namespace_tree()
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/reader/read.py", line 163, in _build_namespace_tree
    _record_file(file, item, queue)
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/files/record.py", line 53, in _record_file
    field = _map_field(
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/types/map.py", line 58, in _map_field
    map_type = _get_field_type(
  File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/types/type_factory.py", line 22, in _get_field_type
    if isinstance(field['type'], dict):
TypeError: list indices must be integers or slices, not str

IMax153 avatar Feb 02 '22 04:02 IMax153