Repository: syncthing/syncthing (language: go)

Issue:
API: /rest/system/connections has misleading "total" entries
Popped up [on the forum](https://forum.syncthing.net/t/rest-endpoint-connections-total-incorrect-paused-status/17834).

The `total` element has these invalid entries:
```
"paused" : false,
"connected" : false,
"clientVersion" : "",
"type" : "",
"address" : ""
```

The first two could be used for a logical conjunction of all remote devices' states, but the latter three are useless.

Easiest fix would be to dump the fields by using `protocol.Statistics` instead of `ConnectionInfo` for the response type.


Discussion / hints:
It's also questionable that we return empty strings and bogus datetimes for disconnected devices to begin with. A more JSON-y approach here would be to either not return them at all or as nulls. Changing that now will probably break everyone who uses this, though.

Fix patch:
diff --git a/lib/model/model.go b/lib/model/model.go
index e722a90cc19..4bcfb1c1a9f 100644
--- a/lib/model/model.go
+++ b/lib/model/model.go
@@ -706,26 +706,12 @@ func (m *model) UsageReportingStats(report *contract.Report, version int, previe
 
 type ConnectionInfo struct {
 	protocol.Statistics
-	Connected     bool
-	Paused        bool
-	Address       string
-	ClientVersion string
-	Type          string
-	Crypto        string
-}
-
-func (info ConnectionInfo) MarshalJSON() ([]byte, error) {
-	return json.Marshal(map[string]interface{}{
-		"at":            info.At,
-		"inBytesTotal":  info.InBytesTotal,
-		"outBytesTotal": info.OutBytesTotal,
-		"connected":     info.Connected,
-		"paused":        info.Paused,
-		"address":       info.Address,
-		"clientVersion": info.ClientVersion,
-		"type":          info.Type,
-		"crypto":        info.Crypto,
-	})
+	Connected     bool   `json:"connected"`
+	Paused        bool   `json:"paused"`
+	Address       string `json:"address"`
+	ClientVersion string `json:"clientVersion"`
+	Type          string `json:"type"`
+	Crypto        string `json:"crypto"`
 }
 
 // NumConnections returns the current number of active connected devices.
@@ -769,12 +755,10 @@ func (m *model) ConnectionStats() map[string]interface{} {
 	res["connections"] = conns
 
 	in, out := protocol.TotalInOut()
-	res["total"] = ConnectionInfo{
-		Statistics: protocol.Statistics{
-			At:            time.Now().Truncate(time.Second),
-			InBytesTotal:  in,
-			OutBytesTotal: out,
-		},
+	res["total"] = map[string]interface{}{
+		"at":            time.Now().Truncate(time.Second),
+		"inBytesTotal":  in,
+		"outBytesTotal": out,
 	}
 
 	return res
diff --git a/lib/protocol/protocol.go b/lib/protocol/protocol.go
index a7f9408a067..7afca5a1e22 100644
--- a/lib/protocol/protocol.go
+++ b/lib/protocol/protocol.go
@@ -994,10 +994,10 @@ func (c *rawConnection) pingReceiver() {
 }
 
 type Statistics struct {
-	At            time.Time
-	InBytesTotal  int64
-	OutBytesTotal int64
-	StartedAt     time.Time
+	At            time.Time `json:"at"`
+	InBytesTotal  int64     `json:"inBytesTotal"`
+	OutBytesTotal int64     `json:"outBytesTotal"`
+	StartedAt     time.Time `json:"startedAt"`
 }
 
 func (c *rawConnection) Statistics() Statistics {


Test patch: