Skip to content

Commit 2fbb33e

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Fix handling of null responses for LSP requests over DTD
The DTD client requires that the `type` field is populated in DTD responses, but this code previously would omit it when the result was `null` (because it used `result?.runtimeType`). Change-Id: I5a6e0927e9c02f2abb690aefceca8f8b003cd43b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394484 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent dfb2554 commit 2fbb33e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

pkg/analysis_server/lib/src/lsp/handlers/custom/handler_experimental_echo.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class ExperimentalEchoHandler extends SharedMessageHandler<Object?, Object?> {
2929
MessageInfo message,
3030
CancellationToken token,
3131
) async {
32+
// The DTD client automatically converts `null` params to an empty map, but
33+
// (because of a previous bug) we want to test null results. So if the
34+
// params are an empty map, return null. This is tested by
35+
// `test_service_success_echo_nullResponse` in `SharedDtdTests`.
36+
if (params is Map && params.isEmpty) {
37+
return success(null);
38+
}
39+
3240
return success(params);
3341
}
3442
}

pkg/analysis_server/lib/src/services/dart_tooling_daemon/dtd_services.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ class DtdServices {
110110
// boolean (for example). This means we need to put the result in a
111111
// field, which we're calling 'result'.
112112
(result) => completer.complete({
113-
'type': result?.runtimeType.toString(),
113+
// result can be null, but DTD requires that we have a `type`, so don't
114+
// use `?.` here because it results in a missing `type` instead of
115+
// `Null`.
116+
'type': result.runtimeType.toString(),
114117
'result': result,
115118
}),
116119
);

pkg/analysis_server/test/shared/shared_dtd_tests.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,26 @@ mixin SharedDtdTests on LspRequestHelpersMixin {
301301
params: {'a': 'b'},
302302
);
303303

304-
var result = response.result['result'] as Map<String, Object?>;
304+
var result = response.result['result'] as Map<String, Object?>?;
305305

306306
expect(result, equals({'a': 'b'}));
307307
}
308308

309+
test_service_success_echo_nullResponse() async {
310+
await initializeServer();
311+
await sendConnectToDtdRequest(registerExperimentalHandlers: true);
312+
313+
var response = await dtd.call(
314+
lspServiceName,
315+
CustomMethods.experimentalEcho.toString(),
316+
);
317+
318+
var result = response.result['result'] as Map<String, Object?>?;
319+
320+
expect(response.type, 'Null');
321+
expect(result, isNull);
322+
}
323+
309324
@SkippedTest(reason: 'Shared LSP methods are currently disabled')
310325
test_service_success_hover() async {
311326
var code = TestCode.parse('''

0 commit comments

Comments
 (0)