mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

Array with null items supported? #433

Closed thedumbterminal closed 1 year ago

thedumbterminal commented 1 year ago

Hi, I'm trying to generate an array which contains null items, such as:

{
  "doc": "Array example",
  "fields": [
    {
      "doc": "example array",
      "name": "list",
      "type": {
        "items": {
          "doc": "a string",
          "name": "list",
          "type": ["null", "string"],
          "default": null
        },
        "type": "array"
      }
    }
  ],
  "name": "myschema_json",
  "namespace": "com.yourdomain.schemas",
  "type": "record"
}

But receive the error:

unknown type: ["null","string"]

Do I need to create a named type here?

mtth commented 1 year ago

Hi @thedumbterminal. The items key in an array's Avro schema expects a type (not a record field). This should work:

{
  "doc": "Array example",
  "fields": [
    {
      "doc": "example array",
      "name": "list",
      "type": {
        "items": ["null", "string"],
        "type": "array"
      }
    }
  ],
  "name": "myschema_json",
  "namespace": "com.yourdomain.schemas",
  "type": "record"
}