Skip to content

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

Open
RobertIndie wants to merge 2 commits into
Sharp-Pulsar:devfrom
RobertIndie:nullable-map
Open

Fix nullable schema generation for non-required dictionary fields#100
RobertIndie wants to merge 2 commits 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!

@eaba
Copy link
Copy Markdown
Member

eaba commented May 16, 2026

I will resume work soon - months or years 🥰🥰

https://github.com/Sharp-Pulsar/AvroSchemaGenerator#note

eaba

This comment was marked as off-topic.

@eaba eaba self-assigned this May 16, 2026
@eaba eaba added the enhancement New feature or request label May 16, 2026
@eaba
Copy link
Copy Markdown
Member

eaba commented May 16, 2026

@RobertIndie
Screenshot_20260516-143756

@RobertIndie
Copy link
Copy Markdown
Contributor Author

@RobertIndie Screenshot_20260516-143756

HI @eaba Thanks for your review. This has been addressed by #101. Could you help take a look?

@eaba
Copy link
Copy Markdown
Member

eaba commented May 22, 2026

@RobertIndie Screenshot_20260516-143756

HI @eaba Thanks for your review. This has been addressed by #101. Could you help take a look?

Yes

@RobertIndie
Copy link
Copy Markdown
Contributor Author

HI @eaba Could you help merge this PR? Thanks!

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants