From Mon Sep 17 00:00:00 2001
From: Matt McCutchen <matt@mattmccutchen.net>
Date: Fri, 30 Oct 2026 13:46:16 -0400

fix #24956 - Way "modified" flag should be based on node IDs, not coordinates

Change Way.hasEqualSemanticAttributes to compare node IDs for non-new
nodes. Keep the existing behavior of comparing semantic attributes of
new nodes.
---
 src/org/openstreetmap/josm/data/osm/Way.java       |  23 ++++-
 .../josm/data/osm/DataSetMergerTest.java           | 110 +++++++++++++++++++++
 2 files changed, 132 insertions(+), 1 deletion(-)

diff --git a/src/org/openstreetmap/josm/data/osm/Way.java b/src/org/openstreetmap/josm/data/osm/Way.java
index 85501abef5..6a492a782f 100644
--- a/src/org/openstreetmap/josm/data/osm/Way.java
+++ b/src/org/openstreetmap/josm/data/osm/Way.java
@@ -318,6 +318,27 @@ public final class Way extends OsmPrimitive implements IWay<Node> {
         return "{Way id=" + getUniqueId() + " version=" + getVersion()+ ' ' + getFlagsAsString() + ' ' + nodesDesc + '}';
     }
 
+    private static boolean memberNodesAreEquivalent(Node n1, Node n2) {
+        /*
+         * This logic attempts to serve at least the following use cases:
+         *
+         * 1. Determining whether a way is modified with respect to the OSM server and
+         *    uploading it will have an effect. The data model stores only the node IDs,
+         *    so this should be based only on whether the IDs are equal (see #24956).
+         *    Since the server never contains "new" nodes, we will always use the ID
+         *    comparison.
+         * 2. Merging layers. If both nodes are "new", then they will typically have
+         *    different IDs, but we want to consider them equivalent for the purpose of
+         *    comparing the ways if all other semantic attributes of the nodes are
+         *    equal. See DataSetMergerTest.testWaySimpleTwoWaysWithNoIdNodesWithoutId.
+         *    (TODO: What is the *end to end* use case that requires this behavior?)
+         */
+        if (n1.isNew() && n2.isNew())
+            return n1.hasEqualSemanticAttributes(n2);
+        else
+            return (n1.getId() == n2.getId());
+    }
+
     @Override
     public boolean hasEqualSemanticAttributes(OsmPrimitive other, boolean testInterestingTagsOnly) {
         if (!(other instanceof Way))
@@ -327,7 +348,7 @@ public final class Way extends OsmPrimitive implements IWay<Node> {
         if (!super.hasEqualSemanticAttributes(other, testInterestingTagsOnly))
             return false;
         return IntStream.range(0, getNodesCount())
-                .allMatch(i -> getNode(i).hasEqualSemanticAttributes(w.getNode(i)));
+                .allMatch(i -> memberNodesAreEquivalent(getNode(i), w.getNode(i)));
     }
 
     /**
diff --git a/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java b/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java
index 1c50e89a4b..902f64c5d6 100644
--- a/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java
+++ b/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java
@@ -720,6 +720,116 @@ class DataSetMergerTest {
         assertFalse(merged.isModified());
     }
 
+    /*
+     * The next two tests relate to the ability of re-downloading a way to reset the
+     * "modified" flag if the way has no difference compared to the server, which
+     * can happen if a change was made and then manually reverted. (See #3384 and
+     * #24956.) This behavior should be based on whether the list of node IDs is
+     * identical and is implemented in Way.memberNodesAreEquivalent.
+     */
+
+    /**
+     * Non-regression test 1 for
+     * <a href="https://josm.openstreetmap.de/ticket/24956">Bug #24956</a>. A moved
+     * node should not prevent the way's "modified" flag from being cleared.
+     */
+    @Test
+    void testWayWithMovedNodeResetModified() {
+
+        // -- my data set
+
+        Node n1 = new Node(LatLon.ZERO);
+        n1.setOsmId(1, 1);
+        my.addPrimitive(n1);
+
+        // Different coordinates than n4.
+        Node n2 = new Node(new LatLon(2, 2));
+        n2.setOsmId(2, 1);
+        n2.setModified(true);
+        my.addPrimitive(n2);
+
+        Way myWay = new Way();
+        myWay.setOsmId(3, 1);
+        myWay.setModified(true);
+        myWay.addNode(n1);
+        myWay.addNode(n2);
+        my.addPrimitive(myWay);
+
+        // -- their data set
+
+        Node n3 = new Node(LatLon.ZERO);
+        n3.setOsmId(1, 1);
+        their.addPrimitive(n3);
+
+        Node n4 = new Node(new LatLon(1, 1));
+        n4.setOsmId(2, 1);
+        their.addPrimitive(n4);
+
+        Way theirWay = new Way();
+        theirWay.setOsmId(3, 1);
+        theirWay.addNode(n3);
+        theirWay.addNode(n4);
+        their.addPrimitive(theirWay);
+
+        DataSetMerger visitor = new DataSetMerger(my, their);
+        visitor.merge();
+
+        // -- tests
+        Way merged = (Way) my.getWays().toArray()[0];
+        assertFalse(merged.isModified());
+    }
+
+    /**
+     * Non-regression test 2 for
+     * <a href="https://josm.openstreetmap.de/ticket/24956">Bug #24956</a>.
+     * Replacement of a node with a new duplicate node with identical coordinates is
+     * a modification at the OSM data model level and should prevent the way's
+     * "modified" flag from being cleared.
+     */
+    @Test
+    void testWayWithNewDuplicateNodeDontResetModified() {
+
+        // -- my data set
+
+        Node n1 = new Node(LatLon.ZERO);
+        n1.setOsmId(1, 1);
+        my.addPrimitive(n1);
+
+        // Same coordinates as n4, but new, without an ID.
+        Node n2 = new Node(new LatLon(1, 1));
+        my.addPrimitive(n2);
+
+        Way myWay = new Way();
+        myWay.setOsmId(3, 1);
+        myWay.setModified(true);
+        myWay.addNode(n1);
+        myWay.addNode(n2);
+        my.addPrimitive(myWay);
+
+        // -- their data set
+
+        Node n3 = new Node(LatLon.ZERO);
+        n3.setOsmId(1, 1);
+        their.addPrimitive(n3);
+
+        Node n4 = new Node(new LatLon(1, 1));
+        n4.setOsmId(2, 1);
+        their.addPrimitive(n4);
+
+        Way theirWay = new Way();
+        theirWay.setOsmId(3, 1);
+        theirWay.addNode(n3);
+        theirWay.addNode(n4);
+        their.addPrimitive(theirWay);
+
+        DataSetMerger visitor = new DataSetMerger(my, their);
+        visitor.merge();
+
+        // -- tests
+        Way merged = (Way) my.getWays().toArray()[0];
+        assertTrue(merged.isModified());
+    }
+
     /**
      * My dataset includes a deleted node.
      * Their dataset includes a way with three nodes, the first one being my node.
