From 8d15c8b86cd3b1761d42b7e6ef830feeb4ac2ae6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= <dcermak@suse.com>
Date: Mon, 23 Jan 2023 14:08:22 +0100
Subject: [PATCH] Only pass the id parameter to types that use it

This fixes https://github.com/openlawlibrary/pygls/issues/312
---
 CHANGELOG.md           |  2 ++
 CONTRIBUTORS.md        |  1 +
 pygls/protocol.py      | 10 ++++------
 tests/test_protocol.py | 27 +++++++++++++++++++++++++++
 4 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d49b73c8..4e9c76cd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning][semver].
  - Fix progress example in json extension. ([#230])
  - Fix `AttributeErrors` in `get_configuration_async`, `get_configuration_callback`, `get_configuration_threaded` commands in json extension. ([#307])
  - Fix type annotations for `get_configuration_async` and `get_configuration` methods on `LanguageServer` and `LanguageServerProtocol` objects ([#307])
+ - Fix passing `id` to `TextDocumentDidOpenNotification` ([#312])
 
 [#230]: https://github.com/openlawlibrary/pygls/issues/230
 [#307]: https://github.com/openlawlibrary/pygls/issues/307
+[#312]: https://github.com/openlawlibrary/pygls/issues/312
 
 
 ## [1.0.0] - 2/12/2022
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 03e9ec7c..e188c798 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -5,6 +5,7 @@
 - [Brett Cannon](https://github.com/brettcannon/)
 - [Daniel Elero](https://github.com/danixeee)
 - [Daniel Miller](https://github.com/millerdev)
+- [Dan Čermák](https://github.com/dcermak)
 - [DeathAxe](https://github.com/deathaxe)
 - [Denis Loginov](https://github.com/dinvlad)
 - [Dillan Mills](https://github.com/DillanCMills)
diff --git a/pygls/protocol.py b/pygls/protocol.py
index 4f0d012d..1dba0737 100644
--- a/pygls/protocol.py
+++ b/pygls/protocol.py
@@ -581,12 +581,10 @@ def send_request(self, method, params=None, callback=None, msg_id=None):
         request_type = self.get_message_type(method) or JsonRPCRequestMessage
         logger.debug('Sending request with id "%s": %s %s', msg_id, method, params)
 
-        request = request_type(
-            id=msg_id,
-            method=method,
-            params=params,
-            jsonrpc=JsonRPCProtocol.VERSION,
-        )
+        kwargs = {"method": method, "params": params, "jsonrpc": JsonRPCProtocol.VERSION}
+        if hasattr(request_type, "id"):
+            kwargs["id"] = msg_id
+        request = request_type(**kwargs)
 
         future = Future()
         # If callback function is given, call it when result is received
diff --git a/tests/test_protocol.py b/tests/test_protocol.py
index b47dde36..f7e56fdb 100644
--- a/tests/test_protocol.py
+++ b/tests/test_protocol.py
@@ -28,10 +28,12 @@
 from lsprotocol.types import (
     PROGRESS,
     TEXT_DOCUMENT_COMPLETION,
+    TEXT_DOCUMENT_DID_OPEN,
     ClientCapabilities,
     CompletionItem,
     CompletionItemKind,
     CompletionParams,
+    DidOpenTextDocumentParams,
     InitializeParams,
     InitializeResult,
     ProgressParams,
@@ -39,6 +41,7 @@
     ShutdownResponse,
     TextDocumentCompletionResponse,
     TextDocumentIdentifier,
+    TextDocumentItem,
     WorkDoneProgressBegin,
 )
 from pygls.protocol import (
@@ -492,6 +495,30 @@ def test_serialize_response_message(msg_type, result, expected):
                 },
             },
         ),
+        (
+            TEXT_DOCUMENT_DID_OPEN,
+            DidOpenTextDocumentParams(
+                text_document=TextDocumentItem(
+                    uri="file:///file.txt",
+                    language_id="txt",
+                    version=0,
+                    text="just some text",
+                )
+            ),
+            {
+                "jsonrpc": "2.0",
+                "id": "1",
+                "method": TEXT_DOCUMENT_DID_OPEN,
+                "params": {
+                    "textDocument": {
+                        "uri": "file:///file.txt",
+                        "languageId": "txt",
+                        "version": 0,
+                        "text": "just some text",
+                    },
+                },
+            },
+        ),
         (   # Unknown type with object params.
             EXAMPLE_REQUEST,
             ExampleParams(