Skip to content

Return None for optional primite field if its version is less than actingVersion. #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ tasks.register('generateRustTestCodecs', JavaExec) {
'sbe-tool/src/test/resources/issue987.xml',
'sbe-tool/src/test/resources/issue1028.xml',
'sbe-tool/src/test/resources/issue1057.xml',
'sbe-tool/src/test/resources/issue1066.xml',
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
'sbe-tool/src/test/resources/nested-composite-name.xml',
Expand Down
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ issue_984 = { path = "../generated/rust/issue984" }
issue_987 = { path = "../generated/rust/issue987" }
issue_1028 = { path = "../generated/rust/issue1028" }
issue_1057 = { path = "../generated/rust/issue1057" }
issue_1066 = { path = "../generated/rust/issue1066" }
baseline_bigendian = { path = "../generated/rust/baseline-bigendian" }
nested_composite_name = { path = "../generated/rust/nested-composite-name" }
fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" }
Expand Down
40 changes: 40 additions & 0 deletions rust/tests/issue_1066_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use issue_1066::{
issue_1066_codec,
issue_1066_codec::{Issue1066Decoder, Issue1066Encoder},
*,
};

#[test]
fn decode_optional_primitive_field() {
let mut buffer = vec![0u8; 256];

// Create a message with `field` set.
let mut encoder = Issue1066Encoder::default().wrap(
WriteBuf::new(&mut buffer),
message_header_codec::ENCODED_LENGTH,
);
encoder.field(10);

// Drop encoder to drop the mut reference for the buffer.
drop(encoder);

// Create a decoder for the message, but stating that the version is 1, instead of 2
// which is the acting version for `field`. Thus, `field()` must return `None`.
let decoder = Issue1066Decoder::default().wrap(
ReadBuf::new(&mut buffer),
message_header_codec::ENCODED_LENGTH,
issue_1066_codec::SBE_BLOCK_LENGTH,
1,
);
assert!(decoder.field().is_none());

// Create a decoder for the message, but stating that the version is 1, instead of 2
// which is the acting version for `field`. Thus, `field()` must return `None`.
let decoder = Issue1066Decoder::default().wrap(
ReadBuf::new(&mut buffer),
message_header_codec::ENCODED_LENGTH,
issue_1066_codec::SBE_BLOCK_LENGTH,
2,
);
assert_eq!(decoder.field(), Some(10));
}
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,13 @@ private static void generatePrimitiveOptionalDecoder(
formatFunctionName(name),
rustPrimitiveType);

if (fieldToken.version() > 0)
{
indent(sb, level + 1, "if self.acting_version() < %d {\n", fieldToken.version());
indent(sb, level + 2, "return None;\n");
indent(sb, level + 1, "}\n\n");
}

indent(sb, level + 1, "let value = self.get_buf().get_%s_at(self.%s);\n",
rustPrimitiveType,
getBufOffset(fieldToken));
Expand Down
18 changes: 18 additions & 0 deletions sbe-tool/src/test/resources/issue1066.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe" package="issue1066" id="1066" version="2"
semanticVersion="1.0.0" byteOrder="littleEndian">
<types>
<composite name="messageHeader">
<type name="blockLength" primitiveType="uint16"
description="Length of the root of the FIX message contained before repeating groups or variable/conditions fields." />
<type name="templateId" primitiveType="uint16"
description="Template ID used to encode the message." />
<type name="schemaId" primitiveType="uint16"
description="ID of the system publishing the message." />
<type name="version" primitiveType="uint16" description="Schema version." />
</composite>
</types>
<message name="issue1066" id="1">
<field name="field" id="1" type="uint16" semanticType="Int" sinceVersion="2"
presence="optional" />
</message>
</messageSchema>
Loading