Skip to content

Commit

Permalink
Added missing Type Adaper for RestartArguments.arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Dietrich <[email protected]>
  • Loading branch information
cdietrich committed May 17, 2022
1 parent 8821222 commit eec8a7d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

package org.eclipse.lsp4j.debug;

import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import java.util.Map
import org.eclipse.lsp4j.debug.adapters.RestartArgumentsTypeAdapter
import org.eclipse.lsp4j.generator.JsonRpcData
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.eclipse.lsp4j.jsonrpc.validation.NonNull
Expand Down Expand Up @@ -938,6 +940,7 @@ class RestartArguments {
* <p>
* Since 1.47
*/
@JsonAdapter(RestartArgumentsTypeAdapter)
Either<LaunchRequestArguments, AttachRequestArguments> arguments;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/******************************************************************************
* Copyright (c) 2022 itemis AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.debug.adapters;

import java.util.function.Predicate;

import org.eclipse.lsp4j.debug.AttachRequestArguments;
import org.eclipse.lsp4j.debug.LaunchRequestArguments;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter.PropertyChecker;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

public class RestartArgumentsTypeAdapter implements TypeAdapterFactory {

private static final TypeToken<Either<LaunchRequestArguments, AttachRequestArguments>> ELEMENT_TYPE = new TypeToken<Either<LaunchRequestArguments, AttachRequestArguments>>() {
};

@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Predicate<JsonElement> leftChecker = new PropertyChecker("noDebug");
Predicate<JsonElement> rightChecker = leftChecker.negate();
TypeAdapter<Either<LaunchRequestArguments, AttachRequestArguments>> elementTypeAdapter = new EitherTypeAdapter<>(
gson, ELEMENT_TYPE, leftChecker, rightChecker);
return (TypeAdapter<T>) elementTypeAdapter;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* Copyright (c) 2022 itemis AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
******************************************************************************/
package org.eclipse.lsp4j.debug.test;

import static org.junit.Assert.assertTrue;

import org.eclipse.lsp4j.debug.RestartArguments;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class RestartArgumentsTypeAdapterFactoryTest {

@Test
public void test() {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
RestartArguments object = gson.fromJson("{\"arguments\": {\"noDebug\":true}}", RestartArguments.class);
assertTrue(object.toString(), object.getArguments().isLeft());
object = gson.fromJson("{\"arguments\": {\"noDebug\":true, \"__restart\":{}}}", RestartArguments.class);
assertTrue(object.toString(), object.getArguments().isLeft());
object = gson.fromJson("{\"arguments\": {}}", RestartArguments.class);
assertTrue(object.toString(), object.getArguments().isRight());
object = gson.fromJson("{\"arguments\": {\"__restart\":{\"dunno\": true}}}", RestartArguments.class);
assertTrue(object.toString(), object.getArguments().isRight());
}

}

0 comments on commit eec8a7d

Please sign in to comment.