Skip to content

Conversation

@StasPiv
Copy link

@StasPiv StasPiv commented Feb 3, 2026

Problem

1. Empty map/record defaults serialize as [] instead of {}

PHP's json_encode([]) produces "[]", but Avro specification requires empty map/record values to be "{}".

Example:

// Input schema
{"type": "record", "name": "Test", "fields": [
  {"name": "meta", "type": {"type": "map", "values": "string"}, "default": {}}
]}

// Before fix (invalid)
"default": []

// After fix (correct)
"default": {}

2. Primitive types lose their short form

When parsing "string" (short form), the library set serialize_type_attribute=true, causing it to serialize back as {"type": "string"} (long form).

Example:

// Input
{"type": "record", "name": "Test", "fields": [
  {"name": "value", "type": "string"}
]}

// Before fix
"type": {"type": "string"}

// After fix
"type": "string"

Solution

  1. Added serialize_default_value() method that recursively converts PHP arrays to stdClass for map/record types before JSON encoding
  2. Changed serialize_type_attribute from true to false when parsing primitive types in short form

@StasPiv StasPiv changed the title Fix/avro schema serialization fix: Empty map/record defaults as {} and primitive short form preservation Feb 4, 2026
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