[evcc] Fix #18345, now every disallowed char will be replaced with a hyphen (#18442)

* Fix #18345, now every disallowed char will be replaced with a hyphen

Signed-off-by: Marcel Goerentz <m.goerentz@t-online.de>
This commit is contained in:
Marcel Goerentz
2025-03-24 22:26:31 +01:00
committed by GitHub
parent 8d9c65c8c3
commit afd63ccbfb
2 changed files with 10 additions and 3 deletions
@@ -20,6 +20,7 @@ import com.google.gson.annotations.SerializedName;
*
* @author Florian Hotze - Initial contribution
* @author Luca Arnecke - Update to evcc version 0.123.1
* @author Marcel Goerentz - Replace invalid chars with hyphens in vehicleName
*/
public class Loadpoint {
// Data types from https://github.com/evcc-io/evcc/blob/master/api/api.go
@@ -271,7 +272,7 @@ public class Loadpoint {
* @return vehicle's title/name
*/
public String getVehicleName() {
return vehicleName != null ? vehicleName.replace(":", "-") : vehicleName;
return vehicleName != null ? vehicleName.replaceAll("[^a-zA-Z0-9_-]", "-") : null;
}
/**
@@ -25,6 +25,7 @@ import com.google.gson.annotations.SerializedName;
* @author Florian Hotze - Initial contribution
* @author Luca Arnecke - update to evcc version 0.123.1
* @author Daniel Kötting - update to evcc version 0.133.0
* @author Marcel Goerentz - Replace invalid chars with hyphens in vehicles map
*/
public class Result {
// Data types from https://github.com/evcc-io/evcc/blob/master/api/api.go
@@ -224,8 +225,13 @@ public class Result {
public Map<String, Vehicle> getVehicles() {
Map<String, Vehicle> correctedMap = new HashMap<>();
for (Entry<String, Vehicle> entry : vehicles.entrySet()) {
// The key from the vehicles map is used as uid, so it should not contain semicolons.
correctedMap.put(entry.getKey().replace(":", "-"), entry.getValue());
// The key from the vehicles map is used as uid, so it should not contain any disallowed chars
// If necessary replace the forbidden chars with hyphens
String key = entry.getKey();
if (!key.matches("[a-zA-Z0-9_-]+")) {
key = key.replaceAll("[^a-zA-Z0-9_-]", "-");
}
correctedMap.put(key, entry.getValue());
}
return correctedMap;
}