Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't warn when empty object or array is given to method without params #357

Merged
merged 1 commit into from Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,7 @@
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.nio.channels.ClosedChannelException;
Expand All @@ -26,6 +27,7 @@ public class JsonRpcException extends RuntimeException {
public static boolean indicatesStreamClosed(Throwable thr) {
return thr instanceof InterruptedIOException
|| thr instanceof ClosedChannelException
|| thr instanceof IOException && "Pipe closed".equals(thr.getMessage())
|| thr instanceof SocketException && ("Connection reset".equals(thr.getMessage()) || "Socket closed".equals(thr.getMessage()))
|| thr instanceof JsonRpcException && indicatesStreamClosed(thr.getCause());
}
Expand Down
Expand Up @@ -116,7 +116,7 @@ public void listen(MessageConsumer callback) {
} catch (IOException exception) {
if (JsonRpcException.indicatesStreamClosed(exception)) {
// Only log the error if we had intended to keep running
if( keepRunning )
if (keepRunning)
fireStreamClosed(exception);
} else
throw new JsonRpcException(exception);
Expand Down
Expand Up @@ -265,7 +265,13 @@ protected Object parseParams(JsonReader in, String method) throws IOException, J
}
return parameters;
}
return new JsonParser().parse(in);
JsonElement rawParams = new JsonParser().parse(in);
if (method != null && parameterTypes.length == 0 && (
rawParams.isJsonArray() && rawParams.getAsJsonArray().size() == 0
|| rawParams.isJsonObject() && rawParams.getAsJsonObject().size() == 0)) {
return null;
}
return rawParams;
}

/**
Expand Down Expand Up @@ -308,6 +314,11 @@ protected Object parseParams(Object params, String method) {
}
return parameters;
}
if (method != null && parameterTypes.length == 0 && (
rawParams.isJsonArray() && rawParams.getAsJsonArray().size() == 0
|| rawParams.isJsonObject() && rawParams.getAsJsonObject().size() == 0)) {
return null;
}
return rawParams;
}

Expand Down
Expand Up @@ -15,21 +15,37 @@ import java.io.ByteArrayOutputStream
import java.io.OutputStreamWriter
import java.io.PipedInputStream
import java.io.PipedOutputStream
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.TimeUnit
import org.eclipse.lsp4j.DidChangeConfigurationParams
import org.eclipse.lsp4j.DidChangeTextDocumentParams
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
import org.eclipse.lsp4j.DidCloseTextDocumentParams
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.DidSaveTextDocumentParams
import org.eclipse.lsp4j.InitializeParams
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.Position
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.lsp4j.ShowMessageRequestParams
import org.eclipse.lsp4j.TextDocumentIdentifier
import org.eclipse.lsp4j.TextDocumentPositionParams
import org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint
import org.eclipse.lsp4j.launch.LSPLauncher
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.lsp4j.services.LanguageServer
import org.eclipse.lsp4j.services.TextDocumentService
import org.eclipse.lsp4j.services.WorkspaceService
import org.eclipse.xtend.lib.annotations.Accessors
import org.junit.Test

import static org.junit.Assert.*

class LSPEndpointTest {

static val TIMEOUT = 2000

@Test
def void testIssue152() throws Exception {
val client = new DummyClient
Expand Down Expand Up @@ -63,6 +79,56 @@ class LSPEndpointTest {
}
}

@Test
def void testIssue346() throws Exception {
val logMessages = new LogMessageAccumulator
try {
logMessages.registerTo(GenericEndpoint)

val server = new DummyServer
val in = new PipedInputStream
val messageStream = new PipedOutputStream
in.connect(messageStream)
val messageWriter = new OutputStreamWriter(messageStream)
val out = new ByteArrayOutputStream
val launcher = LSPLauncher.createServerLauncher(server, in, out, true, null)
launcher.startListening()

messageWriter.write('Content-Length: 48\r\n\r\n')
messageWriter.write('{"jsonrpc": "2.0","method": "exit","params": {}}')
messageWriter.flush()

server.exited.get(TIMEOUT, TimeUnit.MILLISECONDS)
assertTrue(logMessages.records.join('\n', [message]), logMessages.records.isEmpty)
} finally {
logMessages.unregister();
}
}

@Accessors
private static class DummyServer implements LanguageServer {

val textDocumentService = new TextDocumentService {
override didChange(DidChangeTextDocumentParams params) {}
override didClose(DidCloseTextDocumentParams params) {}
override didOpen(DidOpenTextDocumentParams params) {}
override didSave(DidSaveTextDocumentParams params) {}
}

val workspaceService = new WorkspaceService {
override didChangeConfiguration(DidChangeConfigurationParams params) {}
override didChangeWatchedFiles(DidChangeWatchedFilesParams params) {}
}

override initialize(InitializeParams params) {}
override shutdown() {}

val exited = new CompletableFuture<Void>
override exit() {
exited.complete(null)
}
}

private static class DummyClient implements LanguageClient {
override logMessage(MessageParams message) {}
override publishDiagnostics(PublishDiagnosticsParams diagnostics) {}
Expand Down