-
Notifications
You must be signed in to change notification settings - Fork 16
Description
The ActionChanged event has a property named data annotated with the BareAction class type. Despite this, it is used with the full Action instances for all change types except remove.
This annotation is confusing and leads clients to create a custom serialization logic for this single event because the data models generated from OpenApi specifications are wrong:
private void HandleActionChanged(string data) {
var actionChanged = JsonConvert.DeserializeObject<ActionChanged>(data, jsonSettings)!;
// The OpenApi generator thinks all the events use BareAction, which is not true.
// So we have to use this small hack.
switch(actionChanged.ChangeType) {
case ActionChanged.ChangeTypeEnum.Add:
var fullActionJson = JObject.Parse(data)["data"]!.ToString();
var fullAction = JsonConvert.DeserializeObject<Action>(fullActionJson, jsonSettings);
ActionAdded?.Invoke(this, new ActionEventArgs(fullAction!, actionChanged.ParentId));
break;
case ActionChanged.ChangeTypeEnum.Remove:
ActionRemoved?.Invoke(this, new BareActionEventArgs(actionChanged.Data));
break;
case ActionChanged.ChangeTypeEnum.Update:
var fullActionJson2 = JObject.Parse(data)["data"]!.ToString();
var fullAction2 = JsonConvert.DeserializeObject<Action>(fullActionJson2, jsonSettings);
ActionUpdated?.Invoke(this, new ActionEventArgs(fullAction2!, actionChanged.ParentId));
break;
case ActionChanged.ChangeTypeEnum.UpdateBase:
var fullActionJson3 = JObject.Parse(data)["data"]!.ToString();
var fullAction3 = JsonConvert.DeserializeObject<Action>(fullActionJson3, jsonSettings);
ActionBaseUpdated?.Invoke(this, new ActionEventArgs(fullAction3!, actionChanged.ParentId));
break;
default:
throw new NotImplementedException("Unknown change type for 'ActionChanged' event.");
}
}
While this is not something major, it caused me to wonder for a good while where I was supposed to obtain the parameters/flows of the actions. The successful serialisation without throwing exceptions (BareAction is a subset of Action, and extra properties do not throw by default for most JSON libraries) does not help either.
Personally, I don't see a problem with modifying the annotation to use the Action type and sending it even with the remove change type for consistency.