Skip to content

Code generation fails if a map contains a union in"values" #11

@IMax153

Description

@IMax153

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions