Repository: networkx/networkx

Issue:
Deserialisation artifacts in adjacency_graph
### Current Behavior

Serialising and deserialising a Graph using the matched pair json_graph.adjacency_data and json_graph.adjacency_graph produces a graph which is not equal to the incoming graph using the graphs_equal method.
This is because adjacency.py:152 and adjacency.py:156 set the edge attributes to a dictionary containing the successor node of the edge, rather than to the dictionary from which it has been popped:

    for i, d in enumerate(data["adjacency"]):
        source = mapping[i]
        for tdata in d:
            target_data = tdata.copy()
            target = target_data.pop(id_)
            if not multigraph:
                graph.add_edge(source, target)
                graph[source][target].update(tdata)  # Should be target_data, which has v removed
            else:
                ky = target_data.pop(key, None)
                graph.add_edge(source, target, key=ky)
                graph[source][target][ky].update(tdata)  # Should be target_data, which has v removed

### Expected Behavior

A Graph when serialised and deserialised with paired methods should be equal to itself, if its nodes are defined in a way to enable the equality.

### Steps to Reproduce


    def test_deserialized_graph_equal(self):
        G = nx.MultiGraph()
        G.add_edge(1, 2, key="first")
        G.add_edge(1, 2, key="second", color="blue")
        H = adjacency_graph(adjacency_data(G))
        assert graphs_equal(G, H) #  == False

### Environment

Python version: 3.10
NetworkX version: 2.8.6

### Additional context

I have a patchset ready to go with a fix, opening this bug report to attach to.


Discussion:

Resolving patch:
diff --git a/networkx/readwrite/json_graph/adjacency.py b/networkx/readwrite/json_graph/adjacency.py
--- a/networkx/readwrite/json_graph/adjacency.py
+++ b/networkx/readwrite/json_graph/adjacency.py
@@ -149,9 +149,9 @@ def adjacency_graph(data, directed=False, multigraph=True, attrs=_attrs):
             target = target_data.pop(id_)
             if not multigraph:
                 graph.add_edge(source, target)
-                graph[source][target].update(tdata)
+                graph[source][target].update(target_data)
             else:
                 ky = target_data.pop(key, None)
                 graph.add_edge(source, target, key=ky)
-                graph[source][target][ky].update(tdata)
+                graph[source][target][ky].update(target_data)
     return graph


Test patch:
diff --git a/networkx/readwrite/json_graph/tests/test_adjacency.py b/networkx/readwrite/json_graph/tests/test_adjacency.py
--- a/networkx/readwrite/json_graph/tests/test_adjacency.py
+++ b/networkx/readwrite/json_graph/tests/test_adjacency.py
@@ -1,16 +1,18 @@
+import copy
 import json
 
 import pytest
 
 import networkx as nx
 from networkx.readwrite.json_graph import adjacency_data, adjacency_graph
+from networkx.utils import graphs_equal
 
 
 class TestAdjacency:
     def test_graph(self):
         G = nx.path_graph(4)
         H = adjacency_graph(adjacency_data(G))
-        assert nx.is_isomorphic(G, H)
+        assert graphs_equal(G, H)
 
     def test_graph_attributes(self):
         G = nx.path_graph(4)
@@ -20,12 +22,14 @@ def test_graph_attributes(self):
         G.graph[1] = "one"
 
         H = adjacency_graph(adjacency_data(G))
+        assert graphs_equal(G, H)
         assert H.graph["foo"] == "bar"
         assert H.nodes[1]["color"] == "red"
         assert H[1][2]["width"] == 7
 
         d = json.dumps(adjacency_data(G))
         H = adjacency_graph(json.loads(d))
+        assert graphs_equal(G, H)
         assert H.graph["foo"] == "bar"
         assert H.graph[1] == "one"
         assert H.nodes[1]["color"] == "red"
@@ -36,7 +40,7 @@ def test_digraph(self):
         nx.add_path(G, [1, 2, 3])
         H = adjacency_graph(adjacency_data(G))
         assert H.is_directed()
-        assert nx.is_isomorphic(G, H)
+        assert graphs_equal(G, H)
 
     def test_multidigraph(self):
         G = nx.MultiDiGraph()
@@ -44,15 +48,29 @@ def test_multidigraph(self):
         H = adjacency_graph(adjacency_data(G))
         assert H.is_directed()
         assert H.is_multigraph()
+        assert graphs_equal(G, H)
 
     def test_multigraph(self):
         G = nx.MultiGraph()
         G.add_edge(1, 2, key="first")
         G.add_edge(1, 2, key="second", color="blue")
         H = adjacency_graph(adjacency_data(G))
-        assert nx.is_isomorphic(G, H)
+        assert graphs_equal(G, H)
         assert H[1][2]["second"]["color"] == "blue"
 
+    def test_input_data_is_not_modified_when_building_graph(self):
+        G = nx.path_graph(4)
+        input_data = adjacency_data(G)
+        orig_data = copy.deepcopy(input_data)
+        # Ensure input is unmodified by deserialisation
+        assert graphs_equal(G, adjacency_graph(input_data))
+        assert input_data == orig_data
+
+    def test_adjacency_form_json_serialisable(self):
+        G = nx.path_graph(4)
+        H = adjacency_graph(json.loads(json.dumps(adjacency_data(G))))
+        assert graphs_equal(G, H)
+
     def test_exception(self):
         with pytest.raises(nx.NetworkXError):
             G = nx.MultiDiGraph()