Skip to content

Commit 25a606d

Browse files
authored
Merge pull request #574 from tom-smalls/fix-appendable-decoder-version
[Java] Invalid code generated for newly added string field
2 parents 642225a + f44972c commit 25a606d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ private void generateDataDecodeMethods(
827827
indent + " }\n" +
828828
indent + " }\n",
829829
Generators.toUpperFirstChar(propertyName),
830-
generateStringNotPresentCondition(token.version(), indent),
830+
generateStringNotPresentConditionForAppendable(token.version(), indent),
831831
sizeOfLengthField,
832832
generateGet(lengthType, "limit", byteOrderStr),
833833
byteOrderStr));
@@ -1734,6 +1734,22 @@ private static CharSequence generateArrayFieldNotPresentCondition(final int sinc
17341734
sinceVersion);
17351735
}
17361736

1737+
private static CharSequence generateStringNotPresentConditionForAppendable(
1738+
final int sinceVersion, final String indent)
1739+
{
1740+
if (0 == sinceVersion)
1741+
{
1742+
return "";
1743+
}
1744+
1745+
return String.format(
1746+
indent + " if (parentMessage.actingVersion < %d)\n" +
1747+
indent + " {\n" +
1748+
indent + " return;\n" +
1749+
indent + " }\n\n",
1750+
sinceVersion);
1751+
}
1752+
17371753
private static CharSequence generateStringNotPresentCondition(final int sinceVersion, final String indent)
17381754
{
17391755
if (0 == sinceVersion)

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/SchemaExtensionTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void setup() throws Exception
6161
generator().generate();
6262
}
6363

64+
@SuppressWarnings("MethodLength")
6465
@Test
6566
public void testMessage1() throws Exception
6667
{
@@ -81,6 +82,8 @@ public void testMessage1() throws Exception
8182
final Object setEncoder = encoder.getClass().getMethod("tag5").invoke(encoder);
8283
set(setEncoder, "firstChoice", boolean.class, false);
8384
set(setEncoder, "secondChoice", boolean.class, true);
85+
86+
set(encoder, "tag6", String.class, "This is some variable length data");
8487
}
8588

8689
{ // Decode version 0
@@ -90,12 +93,16 @@ public void testMessage1() throws Exception
9093
assertNull(get(decoderVersion0, "tag3"));
9194
assertThat(get(decoderVersion0, "tag4").toString(), is("NULL_VAL"));
9295
assertNull(get(decoderVersion0, "tag5"));
96+
final StringBuilder tag6Value = new StringBuilder();
97+
get(decoderVersion0, "tag6", tag6Value);
98+
assertThat(tag6Value.length(), is(0));
9399

94100
assertEquals(0, decoderVersion0.getClass().getMethod("tag1SinceVersion").invoke(null));
95101
assertEquals(1, decoderVersion0.getClass().getMethod("tag2SinceVersion").invoke(null));
96102
assertEquals(2, decoderVersion0.getClass().getMethod("tag3SinceVersion").invoke(null));
97103
assertEquals(3, decoderVersion0.getClass().getMethod("tag4SinceVersion").invoke(null));
98104
assertEquals(4, decoderVersion0.getClass().getMethod("tag5SinceVersion").invoke(null));
105+
assertEquals(5, decoderVersion0.getClass().getMethod("tag6SinceVersion").invoke(null));
99106
}
100107

101108
{ // Decode version 1
@@ -105,6 +112,9 @@ public void testMessage1() throws Exception
105112
assertNull(get(decoderVersion1, "tag3"));
106113
assertThat(get(decoderVersion1, "tag4").toString(), is("NULL_VAL"));
107114
assertNull(get(decoderVersion1, "tag5"));
115+
final StringBuilder tag6Value = new StringBuilder();
116+
get(decoderVersion1, "tag6", tag6Value);
117+
assertThat(tag6Value.length(), is(0));
108118
}
109119

110120
{ // Decode version 2
@@ -116,6 +126,9 @@ public void testMessage1() throws Exception
116126
assertEquals(300, get(compositeDecoder2, "value"));
117127
assertThat(get(decoderVersion2, "tag4").toString(), is("NULL_VAL"));
118128
assertNull(get(decoderVersion2, "tag5"));
129+
final StringBuilder tag6Value = new StringBuilder();
130+
get(decoderVersion2, "tag6", tag6Value);
131+
assertThat(tag6Value.length(), is(0));
119132
}
120133

121134
{ // Decode version 3
@@ -128,6 +141,9 @@ public void testMessage1() throws Exception
128141
final Object enumConstant = getAEnumConstant(decoderVersion3, "AEnum", 1);
129142
assertEquals(enumConstant, get(decoderVersion3, "tag4"));
130143
assertNull(get(decoderVersion3, "tag5"));
144+
final StringBuilder tag6Value = new StringBuilder();
145+
get(decoderVersion3, "tag6", tag6Value);
146+
assertThat(tag6Value.length(), is(0));
131147
}
132148

133149
{ // Decode version 4
@@ -143,6 +159,27 @@ public void testMessage1() throws Exception
143159
assertNotNull(setDecoder);
144160
assertEquals(false, get(setDecoder, "firstChoice"));
145161
assertEquals(true, get(setDecoder, "secondChoice"));
162+
final StringBuilder tag6Value = new StringBuilder();
163+
get(decoderVersion4, "tag6", tag6Value);
164+
assertThat(tag6Value.length(), is(0));
165+
}
166+
167+
{ // Decode version 5
168+
final Object decoderVersion5 = getMessage1Decoder(buffer, 14, 5);
169+
assertEquals(100, get(decoderVersion5, "tag1"));
170+
assertEquals(200, get(decoderVersion5, "tag2"));
171+
final Object compositeDecoder4 = get(decoderVersion5, "tag3");
172+
assertNotNull(compositeDecoder4);
173+
assertEquals(300, get(compositeDecoder4, "value"));
174+
final Object enumConstant = getAEnumConstant(decoderVersion5, "AEnum", 1);
175+
assertEquals(enumConstant, get(decoderVersion5, "tag4"));
176+
final Object setDecoder = get(decoderVersion5, "tag5");
177+
assertNotNull(setDecoder);
178+
assertEquals(false, get(setDecoder, "firstChoice"));
179+
assertEquals(true, get(setDecoder, "secondChoice"));
180+
final StringBuilder tag6Value = new StringBuilder();
181+
get(decoderVersion5, "tag6", tag6Value);
182+
assertThat(tag6Value.toString(), is("This is some variable length data"));
146183
}
147184
}
148185

sbe-tool/src/test/resources/extension-schema.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
33
package="code.generation.test"
44
id="1"
5-
version="4"
5+
version="5"
66
description="Unit Test">
77
<types>
88
<composite name="messageHeader" description="Message identifiers and length of message root">
@@ -19,6 +19,10 @@
1919
<composite name="AComposite" sinceVersion="1">
2020
<type name="value" primitiveType="int32"/>
2121
</composite>
22+
<composite name="varDataEncoding" semanticType="Length" sinceVersion="5">
23+
<type name="length" primitiveType="uint8" semanticType="Length"/>
24+
<type name="varData" primitiveType="char" semanticType="data"/>
25+
</composite>
2226
<enum name="AEnum" encodingType="uint8" sinceVersion="3">
2327
<validValue name="FirstValue">0</validValue>
2428
<validValue name="SecondValue">1</validValue>
@@ -34,13 +38,15 @@
3438
Version 2: tag3 added to TestMessage1, tag2 added to TestMessage2
3539
Version 3: AEnum, ASet introduced; tag4 added to TestMessage1, tag5 added to TestMessage2
3640
Version 4: tag5 added to TestMessage1, tag4 added to TestMessage2
41+
Version 5: tag6 added to TestMessage1
3742
-->
3843
<sbe:message name="TestMessage1" id="1">
3944
<field name="tag1" id="1" type="int32"/>
4045
<field name="tag2" id="2" type="AType" sinceVersion="1"/>
4146
<field name="tag3" id="3" type="AComposite" sinceVersion="2"/>
4247
<field name="tag4" id="4" type="AEnum" sinceVersion="3"/>
4348
<field name="tag5" id="5" type="ASet" sinceVersion="4"/>
49+
<data name="tag6" type="varDataEncoding" id="6" sinceVersion="5"/>
4450
</sbe:message>
4551
<sbe:message name="TestMessage2" id="2">
4652
<field name="tag1" id="1" type="int32"/>

0 commit comments

Comments
 (0)