Merge branch 'automations-fix-1' into 'dev'

Fixed GSON serializations of triggers

See merge request sa4-2020/the-sanmarinoes/backend!104
This commit is contained in:
Claudio Maggioni 2020-05-01 20:09:43 +02:00
commit a7e26aee98

View file

@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationFastUpdateRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import com.google.gson.*;
import java.lang.reflect.Type;
import org.springframework.context.annotation.Bean;
@ -27,6 +28,7 @@ public class GsonConfig {
private static GsonBuilder configureBuilder() {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
@SuppressWarnings({"rawTypes"})
RuntimeTypeAdapterFactory<State> runtimeTypeAdapterFactory =
RuntimeTypeAdapterFactory.of(State.class, "kind")
.registerSubtype(SwitchableState.class, "switchableState")
@ -43,6 +45,10 @@ public class GsonConfig {
"rangeTrigger");
builder.registerTypeAdapterFactory(runtimeTypeAdapterFactory);
builder.registerTypeAdapterFactory(runtimeTypeAdapterFactoryII);
builder.registerTypeAdapter(
Trigger.class,
(JsonSerializer<Trigger<?>>)
(src, typeOfSrc, context) -> context.serialize((Object) src));
return builder;
}