From 6bab812feab3b8fa59197e45993d02bd6f5248c3 Mon Sep 17 00:00:00 2001 From: bc-rte <142609826+bc-rte@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:01:54 +0100 Subject: [PATCH] Fix OverloadManagementSystem binary serialization error when having trippings without name (#3176) * fix: OverloadManagementSystem did not serialize to binary when a tripping had no name * refactor: change error message from exception part to comment Signed-off-by: CHIQUET Benoit --- .../src/main/java/com/powsybl/commons/binary/BinReader.java | 1 + .../powsybl/iidm/serde/OverloadManagementSystemSerDe.java | 2 ++ .../iidm/serde/OverloadManagementSystemSerDeTest.java | 6 ++++++ .../V1_13/overloadManagementSystemRoundTripRef.xml | 1 + 4 files changed, 10 insertions(+) diff --git a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java index ab0d90410c1..0b84a4b9ea8 100644 --- a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java +++ b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java @@ -76,6 +76,7 @@ private String readString() { } byte[] stringBytes = dis.readNBytes(stringNbBytes); if (stringBytes.length != stringNbBytes) { + // this may happen when the attribute wasn't written in the first place, causing string length to be an aberrant number throw new PowsyblException("Cannot read the full string, bytes missing: " + (stringNbBytes - stringBytes.length)); } return new String(stringBytes, StandardCharsets.UTF_8); diff --git a/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDe.java b/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDe.java index c0daff70056..0b3310ac8ce 100644 --- a/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDe.java +++ b/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDe.java @@ -83,6 +83,8 @@ private void writeTrippingCommonAttributes(OverloadManagementSystem.Tripping tri String nameOrKey = tripping.getNameOrKey(); if (nameOrKey != null && !nameOrKey.equals(tripping.getKey())) { context.getWriter().writeStringAttribute("name", nameOrKey); + } else { + context.getWriter().writeStringAttribute("name", null); } context.getWriter().writeDoubleAttribute("currentLimit", tripping.getCurrentLimit()); context.getWriter().writeBooleanAttribute("openAction", tripping.isOpenAction()); diff --git a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDeTest.java b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDeTest.java index 325cfff4321..eef92de0751 100644 --- a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDeTest.java +++ b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/OverloadManagementSystemSerDeTest.java @@ -216,6 +216,12 @@ private static Network createNetwork() { .setOpenAction(true) .setSwitchToOperateId("S1_400_LINE_2_BREAKER") .add() + .newSwitchTripping() + .setKey("trippingWithNoName") + .setCurrentLimit(800) + .setOpenAction(true) + .setSwitchToOperateId("S1_400_LINE_2_BREAKER") + .add() .add(); // Create an overload management system monitoring "LINE_1" with a tripping on "LINE_2". diff --git a/iidm/iidm-serde/src/test/resources/V1_13/overloadManagementSystemRoundTripRef.xml b/iidm/iidm-serde/src/test/resources/V1_13/overloadManagementSystemRoundTripRef.xml index e3fa457c5c2..f347392486a 100644 --- a/iidm/iidm-serde/src/test/resources/V1_13/overloadManagementSystemRoundTripRef.xml +++ b/iidm/iidm-serde/src/test/resources/V1_13/overloadManagementSystemRoundTripRef.xml @@ -36,6 +36,7 @@ +