Skip to content

Commit

Permalink
lyaml: fix alias serialization
Browse files Browse the repository at this point in the history
The tarantool#8350 was introduced by the commit b42302f ("lua-yaml: enable
aliasing for objects returned by __serialize") so the patch is
effectively reversed.

The idea is to call all object __serialize methods recursively before
finding references. In the process we need to keep an eye on reference
structure of course.

Closes tarantool#8350
Closes tarantool#8321
Closes tarantool#8310

NO_DOC=bugfix
  • Loading branch information
nshy committed Feb 27, 2024
1 parent 94d2813 commit aa91fa1
Show file tree
Hide file tree
Showing 49 changed files with 1,439 additions and 1,299 deletions.
3 changes: 3 additions & 0 deletions changelogs/unreleased/fix-yaml-alias-serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## bugfix/lua

* Fixed yaml alias serialization (gh-8350, gh-8321, gh-8310).
2 changes: 0 additions & 2 deletions src/lua/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ lua_field_try_serialize(struct lua_State *L, struct luaL_serializer *cfg,
if (luaL_tofield(L, cfg, -1, field) != 0)
return -1;
lua_replace(L, idx);
field->serialized = true;
return 0;
}
if (!lua_isstring(L, -1)) {
Expand Down Expand Up @@ -441,7 +440,6 @@ luaL_tofield(struct lua_State *L, struct luaL_serializer *cfg, int index,
field->type = MP_NIL;
field->ext_type = MP_UNKNOWN_EXTENSION;
field->compact = false;
field->serialized = false;

if (index < 0)
index = lua_gettop(L) + index + 1;
Expand Down
2 changes: 0 additions & 2 deletions src/lua/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ struct luaL_field {
/* subtypes of MP_EXT */
enum mp_extension_type ext_type;
bool compact; /* a flag used by YAML serializer */
/** Set if __serialize metamethod was called for this field. */
bool serialized;
};

/**
Expand Down
42 changes: 42 additions & 0 deletions test/app-luatest/serializer_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local yaml = require('yaml')
local fiber = require('fiber')
local t = require('luatest')

local g = t.group()

local strip = function(str)
return str:gsub('^%s*', ''):gsub('\n%s*', '\n')
end

local function serialize(o)
return yaml.decode(yaml.encode(o))
end

g.test_gh_8350_no_unnecessary_anchors = function()
local x = {{}}
setmetatable(x, {__serialize = function(t) return {x[1]} end})
local expected = [[
---
- []
...
]]
t.assert_equals(yaml.encode(x), strip(expected))
end

g.test_gh_8310_alias_across_serialize_method = function()
local x = {}
local y = setmetatable({}, {__serialize = function() return x end})
local z = {x, y}
local expected = [[
---
- &0 []
- *0
...
]]
t.assert_equals(yaml.encode(z), strip(expected))
end

g.test_gh_8321_alias_between_same_udata_objects = function()
local x = serialize({fiber.self(), fiber.self()})
t.assert(x[1] == x[2])
end
48 changes: 24 additions & 24 deletions test/app-tap/tap.result
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,16 @@ not ok - failed subtests
not ok - {1} and {2}
---
path: //1
strict: false
expected: 2
got: 1
expected: 2
strict: false
...
not ok - {1,2,{3,4}} and {1,2,{3,5}}
---
path: //3/2
strict: false
expected: 5
got: 4
expected: 5
strict: false
...
ok - {} and {a = box.NULL} strict = false
ok - {a = box.NULL} and {} strict = false
Expand All @@ -165,57 +165,57 @@ not ok - failed subtests
ok - {a = box.NULL} and {a = box.NULL} strict false
not ok - {} and {a = box.NULL} strict = true
---
strict: true
expected: key a
got: nil
expected: key a
strict: true
...
not ok - {a = box.NULL} and {} strict = true
---
path: //a
strict: true
expected: nil
got: cdata
expected: nil
strict: true
...
not ok - {a = box.NULL} and {b = box.NULL} strict = true
---
path: //a
strict: true
expected: nil
got: cdata
expected: nil
strict: true
...
not ok - {a = box.NULL} and {b = box.NULL, c = box.NULL} strict = true
---
path: //a
strict: true
expected: nil
got: cdata
expected: nil
strict: true
...
not ok - nil and box.NULL strict = true
---
got: nil
expected: cdata
strict: true
expected: cdata
got: nil
...
not ok - box.NULL and nil strict = true
---
got: cdata
expected: nil
strict: true
expected: nil
got: cdata
...
ok - {a = box.NULL} and {a = box.NULL} strict true
# check strict flag inheritance
1..2
not ok - {} and {a = box.NULL} strict = true
---
strict: true
expected: key a
got: nil
expected: key a
strict: true
...
not ok - nil and box.NULL strict = true
---
got: nil
expected: cdata
strict: true
expected: cdata
got: nil
...
# check strict flag inheritance: end
not ok - failed subtests
Expand All @@ -237,20 +237,20 @@ not ok - failed subtests
ok - like
not ok - compare {1, 2, 3} and '200'
---
strict: false
expected: '200'
got:
- 1
- 2
- 3
expected: '200'
strict: false
...
not ok - compare '200' and {1, 2, 3}
---
strict: false
got: '200'
expected:
- 1
- 2
- 3
got: '200'
strict: false
...
# failed subtest: 21
4 changes: 2 additions & 2 deletions test/app/argparse.result
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ argparse({'tarantoolctl', 'start', 'instance', '-vovov'})
- 1: tarantoolctl
2: start
3: instance
o:
v:
- true
- true
v:
- true
o:
- true
- true
...
Expand Down
6 changes: 3 additions & 3 deletions test/box/alter.result
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,8 @@ s.index.pk
hint: true
id: 0
type: TREE
space_id: 512
name: pk
space_id: 512
...
s:select{}
---
Expand Down Expand Up @@ -750,8 +750,8 @@ s.index.pk
hint: true
id: 0
type: TREE
space_id: 512
name: pk
space_id: 512
...
s.index.secondary
---
Expand All @@ -765,8 +765,8 @@ s.index.secondary
hint: true
id: 1
type: TREE
space_id: 512
name: secondary
space_id: 512
...
s.index.secondary:select{}
---
Expand Down
6 changes: 3 additions & 3 deletions test/box/errinj.result
Original file line number Diff line number Diff line change
Expand Up @@ -1732,21 +1732,21 @@ rtreespace:create_index('pk', {if_not_exists = true})
hint: true
id: 0
type: TREE
space_id: 512
name: pk
space_id: 512
...
rtreespace:create_index('target', {type='rtree', dimension = 3, parts={2, 'array'},unique = false, if_not_exists = true,})
---
- parts:
- type: array
exclude_null: false
is_nullable: false
fieldno: 2
dimension: 3
is_nullable: false
id: 1
type: RTREE
space_id: 512
name: target
dimension: 3
...
count = 10
---
Expand Down
6 changes: 3 additions & 3 deletions test/box/function1.result
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ box.func["function1.args"]
id: 66
takes_raw_args: false
setuid: false
is_multikey: false
is_deterministic: false
is_multikey: false
name: function1.args
language: C
...
Expand Down Expand Up @@ -587,8 +587,8 @@ func
id: 66
takes_raw_args: false
setuid: false
is_multikey: false
is_deterministic: false
is_multikey: false
comment: Divide two values
name: divide
language: LUA
Expand Down Expand Up @@ -660,8 +660,8 @@ func
id: 66
takes_raw_args: false
setuid: false
is_multikey: false
is_deterministic: false
is_multikey: false
name: function1.divide
language: C
...
Expand Down
6 changes: 3 additions & 3 deletions test/box/gh-5451-netbox-path-index-schema.result
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ c = netbox.connect(box.cfg.listen)
c.space.gh5451.index[0].parts
---
- - fieldno: 1
path: path1
type: unsigned
exclude_null: false
is_nullable: false
path: path1
...
c.space.gh5451.index[1].parts
---
- - fieldno: 2
path: path2
type: unsigned
exclude_null: false
is_nullable: false
path: path2
...
c.space.gh5451.index[2].parts
---
- - fieldno: 3
path: '[*]'
type: unsigned
exclude_null: false
is_nullable: false
path: '[*]'
...
c:close()
---
Expand Down
2 changes: 1 addition & 1 deletion test/box/gh-5998-one-tx-for-ddl.result
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ s1:create_index('pk')
| hint: true
| id: 0
| type: TREE
| space_id: 512
| name: pk
| space_id: 512
| ...

for i = 0, 10 do s1:replace{i} end
Expand Down
4 changes: 2 additions & 2 deletions test/box/net.box_fiber-async_gh-3107.result
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ tostring(future)
...
future
---
- xyz: abc
abc: 123
- abc: 123
xyz: abc
...
future:wait_result()
---
Expand Down

0 comments on commit aa91fa1

Please sign in to comment.