From 0178f127d1087642ffdb86192b2670a5a0dcbdae Mon Sep 17 00:00:00 2001 From: Philipp Keck Date: Mon, 10 Nov 2025 21:55:14 +0100 Subject: [PATCH] Fix serializer bug regarding capped repeated fields that aren't at the end If, like in `HIUPDv6`, a repeated field is followed by another field, then we need to ensure that the next field writes its content to the correct `$index` (namely behind the space occupied by the repeated field) instead of overwriting the values of the repeated field. See https://github.com/nemiah/phpFinTS/issues/521 --- lib/Fhp/Syntax/Serializer.php | 1 + .../Fhp/Segment/UpdSerializationTest.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 lib/Tests/Fhp/Segment/UpdSerializationTest.php diff --git a/lib/Fhp/Syntax/Serializer.php b/lib/Fhp/Syntax/Serializer.php index 6c2ff267..155263c9 100644 --- a/lib/Fhp/Syntax/Serializer.php +++ b/lib/Fhp/Syntax/Serializer.php @@ -130,6 +130,7 @@ private static function serializeElements($obj, BaseDescriptor $descriptor): arr $value === null || $repetition >= count($value) ? null : $value[$repetition], $elementDescriptor->type, $isSegment); } + $index += $numOutputElements - 1; // The outer loop will increment by 1 as well. } } return $serializedElements; diff --git a/lib/Tests/Fhp/Segment/UpdSerializationTest.php b/lib/Tests/Fhp/Segment/UpdSerializationTest.php new file mode 100644 index 00000000..8df47a6a --- /dev/null +++ b/lib/Tests/Fhp/Segment/UpdSerializationTest.php @@ -0,0 +1,33 @@ +assertEquals(static::HIUPD, Serializer::serializeSegment($hiupd)); + } + + public function testNativePhpSerialization(): void + { + /** @var HIUPDv6 $hiupd */ + $hiupd = Parser::parseSegment(static::HIUPD, HIUPDv6::class); + $before = $hiupd->getErlaubteGeschaeftsvorfaelle(); + + /** @var HIUPDv6 $hiupd */ + $hiupd = unserialize(serialize($hiupd)); + $after = $hiupd->getErlaubteGeschaeftsvorfaelle(); + + $this->assertEquals($before, $after); + } +}