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 14, 2022
1 parent 8821222 commit 40b8263
Show file tree
Hide file tree
Showing 3 changed files with 94 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.RestartArgumentsTypeAdapterFactory
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(RestartArgumentsTypeAdapterFactory)
Either<LaunchRequestArguments, AttachRequestArguments> arguments;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/******************************************************************************
* 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 RestartArgumentsTypeAdapterFactory 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) {
if (!ELEMENT_TYPE.equals(type))
return null;
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,46 @@
/******************************************************************************
* 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.AttachRequestArguments;
import org.eclipse.lsp4j.debug.LaunchRequestArguments;
import org.eclipse.lsp4j.debug.adapters.RestartArgumentsTypeAdapterFactory;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.Test;

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

public class RestartArgumentsTypeAdapterFactoryTest {

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

@Test
public void test() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapterFactory(new RestartArgumentsTypeAdapterFactory());
Gson gson = builder.create();
Either<LaunchRequestArguments,AttachRequestArguments> object = gson.fromJson("{\"noDebug\":true}", ELEMENT_TYPE.getType());
assertTrue(object.toString(), object.isLeft());
object = gson.fromJson("{\"noDebug\":true, \"__restart\":{}}", ELEMENT_TYPE.getType());
assertTrue(object.toString(), object.isLeft());
object = gson.fromJson("{}", ELEMENT_TYPE.getType());
assertTrue(object.toString(), object.isRight());
object = gson.fromJson("{\"__restart\":{\"dunno\": true}}", ELEMENT_TYPE.getType());
assertTrue(object.toString(), object.isRight());
}

}

0 comments on commit 40b8263

Please sign in to comment.