Skip to content

Fix nullable schema generation for non-required dictionary fields#100

Open
RobertIndie wants to merge 1 commit into
Sharp-Pulsar:devfrom
RobertIndie:nullable-map
Open

Fix nullable schema generation for non-required dictionary fields#100
RobertIndie wants to merge 1 commit into
Sharp-Pulsar:devfrom
RobertIndie:nullable-map

Conversation

@RobertIndie
Copy link
Copy Markdown
Contributor

Motivation

Currently, fields of type Dictionary are generated as non-nullable Avro map schemas, for example:

{
  "name": "MyMap",
  "type": {
    "type": "map",
    "values": "string"
  },
  "default": {}
}

This causes serialization to fail when an optional dictionary field is unset, because the generated schema does not allow null values:

Unhandled exception. Avro.AvroException: Map is null - use a union for nullable types in field MyMap
 ---> Avro.AvroTypeException: Map is null - use a union for nullable types

Modifications

  • Make non-[Required] Dictionary<TKey, TValue> fields generate nullable Avro union schemas.
  • Keep [Required] dictionary fields non-nullable.

Non-required dictionary fields now generate:

"type": [
  { "type": "map", "values": "string" },
  "null"
],
"default": {}

Required dictionary fields still generate:

"type": { "type": "map", "values": "string" },
"default": {}

## Summary
- Make non-`[Required]` `Dictionary<TKey, TValue>` fields generate nullable Avro union schemas.
- Keep `[Required]` dictionary fields non-nullable.

## Details
Non-required dictionary fields now generate:

```json
"type": [
  { "type": "map", "values": "string" },
  "null"
],
"default": {}
```

Required dictionary fields still generate:

```json
"type": { "type": "map", "values": "string" },
"default": {}
```
@RobertIndie
Copy link
Copy Markdown
Contributor Author

@eaba Could you help review this PR? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant