From f7ebc8831b032196e6695fa22647717a7bf2b55c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 22 Aug 2021 14:52:40 +0300 Subject: ActivityPub: fix empty object serialization --- .../api/activity/helpers/ContextTypeFilter.java | 29 ++++++++++++++++++++++ .../com/juick/www/api/activity/model/Context.java | 2 ++ .../java/com/juick/server/tests/ServerTests.java | 7 +++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/juick/www/api/activity/helpers/ContextTypeFilter.java (limited to 'src') diff --git a/src/main/java/com/juick/www/api/activity/helpers/ContextTypeFilter.java b/src/main/java/com/juick/www/api/activity/helpers/ContextTypeFilter.java new file mode 100644 index 00000000..e8a1dfab --- /dev/null +++ b/src/main/java/com/juick/www/api/activity/helpers/ContextTypeFilter.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2008-2021, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.www.api.activity.helpers; + +public class ContextTypeFilter { + + @Override + public boolean equals(Object obj) { + if (obj == null || !(obj instanceof String)) { + return true; + } + return ((String)obj).equals("Context"); + } +} diff --git a/src/main/java/com/juick/www/api/activity/model/Context.java b/src/main/java/com/juick/www/api/activity/model/Context.java index f5d95d24..66259b7b 100644 --- a/src/main/java/com/juick/www/api/activity/model/Context.java +++ b/src/main/java/com/juick/www/api/activity/model/Context.java @@ -19,6 +19,7 @@ package com.juick.www.api.activity.model; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.juick.www.api.activity.helpers.ContextTypeFilter; import com.juick.www.api.activity.helpers.LinkValueDeserializer; import com.juick.www.api.activity.model.activities.*; import com.juick.www.api.activity.model.objects.*; @@ -78,6 +79,7 @@ public class Context { this.id = id; } + @JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = ContextTypeFilter.class) public String getType() { return getClass().getSimpleName(); } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index db1449f2..bdca6cd2 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -41,7 +41,6 @@ import com.juick.www.api.activity.model.Context; import com.juick.www.api.activity.model.activities.*; import com.juick.www.api.activity.model.objects.Actor; import com.juick.www.api.activity.model.objects.Application; -import com.juick.www.api.activity.model.objects.Image; import com.juick.www.api.activity.model.objects.Note; import com.juick.www.api.activity.model.objects.Person; import com.juick.www.api.webfinger.model.Account; @@ -2384,4 +2383,10 @@ public class ServerTests { mockMvc.perform(get("/api/messages").header("Accept", "application/xml")).andExpect(status().isBadRequest()) .andExpect(content().string("Invalid media type")); } + @Test + public void emptyContextShouldNotSerializeType() throws Exception { + Context context = new Context("http://juick.com/u/ermine"); + String contextString = jsonMapper.writeValueAsString(context); + assertThat(contextString, is("{\"id\":\"http://juick.com/u/ermine\"}")); + } } -- cgit v1.2.3