From e49a44ab1d8a386909ad6f52f503374041a2b583 Mon Sep 17 00:00:00 2001 From: "Anthony F. Molinaro" Date: Sat, 18 Jun 2011 06:04:01 +0000 Subject: [PATCH] THRIFT-1151 - catch some serialization errors git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1137131 13f79535-47bb-0310-9956-ffa450edef68 --- lib/erl/src/thrift_protocol.erl | 6 ++++++ test/erl/Makefile | 4 +++- test/erl/src/Thrift1151.thrift | 3 +++ test/erl/src/test_thrift_1151.erl | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/erl/src/Thrift1151.thrift create mode 100644 test/erl/src/test_thrift_1151.erl diff --git a/lib/erl/src/thrift_protocol.erl b/lib/erl/src/thrift_protocol.erl index 4c334128..95b7d1ad 100644 --- a/lib/erl/src/thrift_protocol.erl +++ b/lib/erl/src/thrift_protocol.erl @@ -329,6 +329,12 @@ write(Proto, {{struct, {Module, StructureName}}, Data}) element(1, Data) =:= StructureName -> write(Proto, {Module:struct_info(StructureName), Data}); +write(_, {{struct, {Module, StructureName}}, Data}) + when is_atom(Module), + is_atom(StructureName) -> + error(struct_unmatched, {{provided, element(1, Data)}, + {expected, StructureName}}); + write(Proto0, {{list, Type}, Data}) when is_list(Data) -> {Proto1, ok} = write(Proto0, diff --git a/test/erl/Makefile b/test/erl/Makefile index cd4c8a78..bfb1d005 100644 --- a/test/erl/Makefile +++ b/test/erl/Makefile @@ -29,7 +29,7 @@ SRCDIR=src ALL_INCLUDEDIR=$(GEN_INCLUDEDIR) $(INCLUDEDIR) ../../lib/erl/include INCLUDEFLAGS=$(patsubst %,-I%, ${ALL_INCLUDEDIR}) -MODULES = stress_server test_server test_client test_disklog test_membuffer +MODULES = stress_server test_server test_client test_disklog test_membuffer test_thrift_1151 INCLUDES = TARGETS = $(patsubst %,${TARGETDIR}/%.beam,${MODULES}) @@ -39,12 +39,14 @@ all: ${GEN_TARGETDIR}/ ${TARGETS} TEST_RPCFILE = ../ThriftTest.thrift STRESS_RPCFILE = ../StressTest.thrift +THRIFT_1151_FILE = src/Thrift1151.thrift THRIFT = ../../compiler/cpp/thrift ${GENDIR}/: ${RPCFILE} rm -rf ${GENDIR} ${THRIFT} --gen erl ${TEST_RPCFILE} ${THRIFT} --gen erl ${STRESS_RPCFILE} + ${THRIFT} --gen erl ${THRIFT_1151_FILE} mkdir -p ${GEN_INCLUDEDIR} mkdir -p ${GEN_SRCDIR} mkdir -p ${GEN_TARGETDIR} diff --git a/test/erl/src/Thrift1151.thrift b/test/erl/src/Thrift1151.thrift new file mode 100644 index 00000000..6f934a7b --- /dev/null +++ b/test/erl/src/Thrift1151.thrift @@ -0,0 +1,3 @@ +struct StructA { 1: i16 x; } +struct StructB { 1: i32 x; } +struct StructC { 1: StructA x; } diff --git a/test/erl/src/test_thrift_1151.erl b/test/erl/src/test_thrift_1151.erl new file mode 100644 index 00000000..c50ddee7 --- /dev/null +++ b/test/erl/src/test_thrift_1151.erl @@ -0,0 +1,19 @@ +-module(test_thrift_1151). + +-include("thrift1151_types.hrl"). + +-export([t/0, t1/0]). + +t() -> + S1 = #structC{x=#structB{x=1}}, + {ok, Transport} = thrift_memory_buffer:new(), + {ok, Protocol} = thrift_binary_protocol:new(Transport), + thrift_protocol:write(Protocol, + {{struct, element(2, thrift1151_types:struct_info('structC'))}, S1}). + +t1() -> + S2 = #structC{x=#structA{x="1"}}, + {ok, Transport} = thrift_memory_buffer:new(), + {ok, Protocol} = thrift_binary_protocol:new(Transport), + thrift_protocol:write(Protocol, + {{struct, element(2, thrift1151_types:struct_info('structC'))}, S2}). -- 2.17.1