Skip to content

Commit 4676481

Browse files
committed
try and handle ropey social urls
1 parent a69dd65 commit 4676481

File tree

7 files changed

+80
-4
lines changed

7 files changed

+80
-4
lines changed

intercom-java/src/main/java/io/intercom/api/SocialProfile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class SocialProfile extends TypedData {
2626

2727
@JsonProperty("url")
2828
@JsonDeserialize(using = URIDeserializer.class)
29+
@JsonInclude(JsonInclude.Include.NON_NULL)
2930
private URI url;
3031

3132
public SocialProfile() {

intercom-java/src/main/java/io/intercom/api/URIDeserializer.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.node.ValueNode;
88

99
import java.io.IOException;
10+
import java.net.MalformedURLException;
1011
import java.net.URI;
1112
import java.net.URISyntaxException;
1213
import java.net.URL;
@@ -22,11 +23,22 @@ public URIDeserializer() {
2223
public URI deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
2324
final ObjectMapper mapper = (ObjectMapper) jp.getCodec();
2425
final ValueNode vNode = mapper.readTree(jp);
26+
27+
URI uri = null;
2528
try {
26-
final URL url = new URL(vNode.asText());
27-
return new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), url.getRef());
28-
} catch (URISyntaxException e) {
29-
throw new RuntimeException(e);
29+
uri = new URI(vNode.asText());
30+
} catch (URISyntaxException ignored) {
31+
}
32+
33+
if (uri == null) {
34+
try {
35+
final URL url = new URL(vNode.asText());
36+
uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), url.getRef());
37+
} catch (MalformedURLException ignored) {
38+
} catch (URISyntaxException ignored) {
39+
}
3040
}
41+
42+
return uri;
3143
}
3244
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static io.intercom.api.TestSupport.load;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class SocialProfileTest {
11+
12+
13+
private static ObjectMapper mapper;
14+
15+
@BeforeClass
16+
public static void beforeClass() {
17+
mapper = MapperSupport.objectMapper();
18+
}
19+
20+
@Test
21+
public void testURLMarshalling() throws Exception {
22+
String json = load("social_profile_url_1.json");
23+
SocialProfile socialProfile = mapper.readValue(json, SocialProfile.class);
24+
assertTrue(socialProfile.getUrl() != null);
25+
26+
json = load("social_profile_url_2.json");
27+
socialProfile = mapper.readValue(json, SocialProfile.class);
28+
assertTrue(socialProfile.getUrl() != null);
29+
30+
json = load("social_profile_url_3.json");
31+
socialProfile = mapper.readValue(json, SocialProfile.class);
32+
assertTrue(socialProfile.getUrl() == null);
33+
34+
json = load("social_profile_url_4.json");
35+
socialProfile = mapper.readValue(json, SocialProfile.class);
36+
assertTrue(socialProfile.getUrl() != null);
37+
}
38+
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Social",
3+
"id": "1235d3213",
4+
"username": "th1sland",
5+
"url": "th1sland.example.com"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Social",
3+
"id": "1235d3213",
4+
"username": "th1sland",
5+
"url": "./relative.html"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Social",
3+
"id": "1235d3213",
4+
"username": "th1sland",
5+
"url": "./relative troll"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Social",
3+
"id": "1235d3213",
4+
"username": "th1sland",
5+
"url": "http://example.net/absolute troll"
6+
}

0 commit comments

Comments
 (0)