Skip to content

Treat null as undefined #86

Closed
Closed

Description

JavaScript is very special with the seperation between null and undefined. In many other languages such as PHP, if a property has not been set, it is null. Consequently, not-set properties will also JSON-encode to null. There is no way to find out if a property is null because it has been set to null or because the property was not set.

My PHP language server responds with this response to the initialize request:

Content-Type: application/vscode-jsonrpc; charset=utf8
Content-Length: 493

{
   "id":0,
   "result":{
      "capabilities":{
         "textDocumentSync":1,
         "hoverProvider":null,
         "completionProvider":null,
         "signatureHelpProvider":null,
         "definitionProvider":null,
         "referencesProvider":null,
         "documentHighlightProvider":null,
         "documentSymbolProvider":true,
         "workspaceSymbolProvider":null,
         "codeActionProvider":null,
         "codeLensProvider":null,
         "documentFormattingProvider":null,
         "documentRangeFormattingProvider":null,
         "documentOnTypeFormattingProvider":null,
         "renameProvider":null
      }
   },
   "error":null,
   "jsonrpc":"2.0"
}

For a human reading this, it is clear that the request was successful, result is defined and error is not defined. But the language client explicitely checks if error !== undefined here. This makes VS Code treat the request as a failed, and then fail with Cannot read property 'code' of null, because it tries to read the code property of the error.

It makes it impossible to implement the language server protocol with classes in any language that does not have a dedicated undefined type. Please just treat null as undefined.

value != undefined will be true for both null and undefined.

Activity

  1. dbaeumer commented on Sep 8, 2016

    @dbaeumer
    Member

    Felix Becker (@felixfbecker) we do treat undefined and null 'equal' in the protocol.I even think that I state somewhere that you should send null not undefined since undefined doesn't exist in other languages. On our server side we even have code that explicitly exchanges undefined with null. And the client code always treats undefined and null as 'equal'

    However in the case you pointed out I explicitly tried to followed the JSON RPC spec which states:

    result
       This member is REQUIRED on success.
       This member MUST NOT exist if there was an error invoking the method.
       The value of this member is determined by the method invoked on the Server.
    error
       This member is REQUIRED on error.
       This member MUST NOT exist if there was no error triggered during invocation.
       The value for this member MUST be an Object as defined in section 5.1.
    

    Would it be possible for your php json library to have two type of Response. FailedResponse and SuccessResponse.

  2. felixfbecker commented on Sep 8, 2016

    @felixfbecker
    ContributorAuthor

    Good to know you keep it in mind :)
    I knew about the protocol, but I thought not following it strictly here would be more beneficial. But using subclasses is a better solution.

  3. locked and limited conversation to collaborators on Nov 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions