avro-schema
avro-schema copied to clipboard
Consider lack of value as null
This is follow up of #64. At least the one case found where box.NULL need to be specified explicitly: union with 'null' branch.
Consider test case (can be added to test/ddt_suite/record_union.lua):
local foo = [[{
"name": "foo",
"type": "record",
"fields": [
{"name": "A", "type": "string"},
{"name": "B", "type": ["null", "string"]},
{"name": "C", "type": "int"}
]
}]]
-- gh-67: treat lack of value (B) as null
t {
schema = foo,
func = 'flatten',
input = '{"A":"Hello, world!","C":42}',
output = '["Hello, world!", 0, null, 42]'
}
Other cases are possible, the following are all I know that need to be checked:
- [ ] non nil/box.NULL default
- [ ] nullable type
- [ ] union with 'null' branch
Found the following case that affects both validate and flatten:
avro_schema = require('avro_schema')
schema = {type = 'record', name = 'foo', fields = {{name = 'foo', type = 'null'}}}
ok, h = avro_schema.create(schema)
ok, m = avro_schema.compile(h)
avro_schema.validate(h, {})
---
- false
- Field foo missing
...
avro_schema.validate(h, setmetatable({}, {__serialize = 'map'}))
---
- false
- Field foo missing
...
m.flatten({})
---
- false
- Expecting MAP, encountered ARRAY
...
m.flatten(setmetatable({}, {__serialize = 'map'}))
---
- false
- 'Key missing: "foo"'
...
m.flatten({foo = box.NULL})
---
- true
- [null]
...
Look: the latter case differs from others.
However just null (not as a field) works as expected:
tarantool> schema = 'null'
---
...
ok, h = avro_schema.create(schema)
ok, m = avro_schema.compile(h)
avro_schema.validate(h)
---
- true
- null
...
avro_schema.validate(h, box.NULL)
---
- true
- null
...
m.flatten()
---
- true
- [null]
...
m.flatten(box.NULL)
---
- true
- [null]
...