correctly handle null Result in a Response

This commit is contained in:
Christopher Willis-Ford 2022-05-03 08:24:57 -07:00
parent 9ccdabb64d
commit d1bff9b2c7
3 changed files with 7 additions and 13 deletions

View file

@ -36,25 +36,19 @@ internal class JsonRpc2MessageConverter : JsonConverter<JsonRpc2Message>
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, JsonRpc2Message value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, options);
private class MessageTypeDiscriminator
private class MessageTypeDiscriminator : JsonRpc2Message
{
[JsonPropertyName("method")]
public string Method { get; set; }
[JsonPropertyName("result")]
public object Result { get; set; }
[JsonPropertyName("error")]
public JsonRpc2Error Error { get; set; }
public Type GuessMessageType()
{
if (this.Result != null || this.Error != null)
if (this.Error != null || this.ExtraProperties.ContainsKey("result"))
{
return typeof(JsonRpc2Response);
}
if (!string.IsNullOrEmpty(this.Method))
if (this.ExtraProperties.ContainsKey("method"))
{
return typeof(JsonRpc2Request);
}

View file

@ -36,11 +36,11 @@ internal class JsonRpc2Message
public object Id { get; set; }
/// <summary>
/// Gets a dictionary to contain additional properties not part of the static C# type.
/// Gets or sets a dictionary to contain additional properties not part of the static C# type.
/// </summary>
[JsonExtensionData]
[JsonPropertyOrder(100)]
public Dictionary<string, object> ExtraProperties { get; } = new ();
public Dictionary<string, object> ExtraProperties { get; set; }
/// <summary>
/// Gets a value indicating whether or not this is a valid JSON-RPC message.

View file

@ -402,11 +402,11 @@ internal class Session : IDisposable
{
if (error != null)
{
this.completionSource.SetException(new JsonRpc2Exception(error));
this.completionSource.TrySetException(new JsonRpc2Exception(error));
}
else
{
this.completionSource.SetResult(result);
this.completionSource.TrySetResult(result);
}
}
}