citrineos-core
citrineos-core copied to clipboard
Fix: Avoid to overwrite mutability and other fields in stored VariableAttribute when call set variable
Goal
This PR is to fix an issue with SetVariables overwriting data that it does not contain (i.e. mutability is reset). The cause of the issue is that, when storing VariableAttribute in db, we always set default values for some fields, e.g., mutability, when they are omitted in the given object and then store these default values in db. So the stored values are overwrite by default values. This way of setting default values works for use case handle NotifyReport req since OCPP 2.0.1 expect us to do so. But we need a fix for use case SetVariable where default values should not overwrite stored values in db.
Changes
- In
createOrUpdateDeviceModelByStationId
, replace upsert with create and update so that default values are used when creation. When updating, it depends on values in the given entity. We don't set any default values when any fields omitted. - In usages of
createOrUpdateDeviceModelByStationId
we make sure to set the correct values in the entity and give it tocreateOrUpdateDeviceModelByStationId
. - In this way, db layer don't need to know which use cases (NotifyReport or SetVariable) it is or whether use default values.
Tests
- Prepare: Manually change values of a VariableAttribute in db
- Call SetVariable from postman
- Other fields are not overwritten except for
value
- Send a notifyReport req and omit
mutability
,consistent
andpersistent
fields. - The saved VariableAttribute are overwritten with default values.