Skip to content

Conversation

@tintoy
Copy link
Collaborator

@tintoy tintoy commented Oct 8, 2017

Specific fix for #24 (a more general fix will be offered as a separate PR).

Avoid NullReferenceException if "params" property is null.
Copy link
Member

@david-driscoll david-driscoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few changes that are needed. The thing that is causing this is probably a missing attribute in the client request object

We should add [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] to Params to avoid it from being serialized.

object requestId = null;
bool hasRequestId;
if (hasRequestId = request.TryGetValue("id", out var id))
if (hasRequestId = request.TryGetValue("id", out var id) && requestId != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't change, requestId is allowed to be provided as null. This indicates a request vs notification.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, shouldn't hasRequestId be false if id == null?

}

var hasParams = request.TryGetValue("params", out var @params);
var hasParams = request.TryGetValue("params", out var @params) && @params != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change.

Copy link
Collaborator Author

@tintoy tintoy Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't this the source of the NRE? If hasParams is true but @params is null, then the line below will attempt to check @params.Type and crash, won't it?

                  // @params can be null even though hasParams is true
 if (hasParams && @params.Type != JTokenType.Array

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm - saw your other comment :)


var hasParams = request.TryGetValue("params", out var @params);
var hasParams = request.TryGetValue("params", out var @params) && @params != null;
if (hasParams && @params.Type != JTokenType.Array && @params.Type != JTokenType.Object)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this line to better align to the spec. params if specified must be an array or object.

Swap with:
if (hasParams && @params?.Type != JTokenType.Array && @params?.Type != JTokenType.Object)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, you want to use the ?. operator. Sorry, fair enough - I missed that when viewing on my phone, I get you now. Will fix it up shortly :)

@tintoy
Copy link
Collaborator Author

tintoy commented Oct 9, 2017

The thing that is causing this is probably a missing attribute in the client request object

The first place I ran into this was when troubleshooting an LPS4j-based client, but yeah - I could easily see various clients making a similar mistake.

@david-driscoll
Copy link
Member

I wrote to follow the spec first (as per http://www.jsonrpc.org/specification) but if this comes up again with params being serialized as null (instead of absent), I think it's safe to just let it behave as a notification instead. However it's kind of a grey area, which might cause some things to behave strangely...

@david-driscoll david-driscoll merged commit f76a391 into OmniSharp:master Oct 10, 2017
@tintoy
Copy link
Collaborator Author

tintoy commented Oct 10, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants