From 4b37c34b247aa41c0a3b50e07ba86d068cad2b41 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 2 Jul 2017 21:35:26 -0700 Subject: [PATCH] Fix for Issue #95 - Twitter userId is too large for JS serialization Twitter provides id and id_str in User Object. Id is of datatype long and that could get too long for some programming languages to handle. For such languages, API is modified to return id in String format using the method getIdStr(). --- .../social/twitter/api/TwitterProfile.java | 20 +++++++++++++++++++ .../twitter/api/impl/UserTemplateTest.java | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/spring-social-twitter/src/main/java/org/springframework/social/twitter/api/TwitterProfile.java b/spring-social-twitter/src/main/java/org/springframework/social/twitter/api/TwitterProfile.java index f60a4dc6..7979c39e 100644 --- a/spring-social-twitter/src/main/java/org/springframework/social/twitter/api/TwitterProfile.java +++ b/spring-social-twitter/src/main/java/org/springframework/social/twitter/api/TwitterProfile.java @@ -22,11 +22,13 @@ * Model class representing a Twitter user's profile information. * * @author Craig Walls + * @author karthik */ public class TwitterProfile extends TwitterObject implements Serializable { private static final long serialVersionUID = 1L; private final long id; + private final String idStr; private final String screenName; private final String name; private final String url; @@ -66,7 +68,12 @@ public static long getSerialversionuid() { } public TwitterProfile(long id, String screenName, String name, String url, String profileImageUrl, String description, String location, Date createdDate) { + this(id, String.valueOf(id), screenName, name, url, profileImageUrl, description, location, createdDate); + } + + public TwitterProfile(long id, String idStr, String screenName, String name, String url, String profileImageUrl, String description, String location, Date createdDate) { this.id = id; + this.idStr = idStr; this.screenName = screenName; this.name = name; this.url = url; @@ -85,6 +92,15 @@ public long getId() { return id; } + /** + * The user's Twitter ID in String format + * + * @return The user's Twitter ID in String format + */ + public String getIdStr() { + return idStr; + } + /** * The user's Twitter screen name * @@ -422,6 +438,9 @@ public boolean equals(Object o) { if (verified != that.verified) { return false; } + if (idStr != null ? !idStr.equals(that.idStr) : that.idStr != null) { + return false; + } if (backgroundColor != null ? !backgroundColor.equals(that.backgroundColor) : that.backgroundColor != null) { return false; } @@ -475,6 +494,7 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (idStr != null ? idStr.hashCode() : 0); result = 31 * result + (screenName != null ? screenName.hashCode() : 0); result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (url != null ? url.hashCode() : 0); diff --git a/spring-social-twitter/src/test/java/org/springframework/social/twitter/api/impl/UserTemplateTest.java b/spring-social-twitter/src/test/java/org/springframework/social/twitter/api/impl/UserTemplateTest.java index 0571d15a..5c33496c 100644 --- a/spring-social-twitter/src/test/java/org/springframework/social/twitter/api/impl/UserTemplateTest.java +++ b/spring-social-twitter/src/test/java/org/springframework/social/twitter/api/impl/UserTemplateTest.java @@ -60,6 +60,7 @@ public void getUserProfile() throws Exception { TwitterProfile profile = twitter.userOperations().getUserProfile(); assertEquals(161064614, profile.getId()); + assertEquals("161064614", profile.getIdStr()); assertEquals("artnames", profile.getScreenName()); assertEquals("Art Names", profile.getName()); assertEquals("I'm just a normal kinda guy", profile.getDescription()); @@ -100,6 +101,7 @@ public void getUserProfile_userId() throws Exception { TwitterProfile profile = twitter.userOperations().getUserProfile(12345); assertEquals(161064614, profile.getId()); + assertEquals("161064614", profile.getIdStr()); assertEquals("artnames", profile.getScreenName()); assertEquals("Art Names", profile.getName()); assertEquals("I'm just a normal kinda guy", profile.getDescription()); @@ -117,6 +119,7 @@ public void getUserProfile_userId_appAuthorization() throws Exception { TwitterProfile profile = appAuthTwitter.userOperations().getUserProfile(12345); assertEquals(161064614, profile.getId()); + assertEquals("161064614", profile.getIdStr()); assertEquals("artnames", profile.getScreenName()); assertEquals("Art Names", profile.getName()); assertEquals("I'm just a normal kinda guy", profile.getDescription()); @@ -133,6 +136,7 @@ public void getUserProfile_screenName() throws Exception { TwitterProfile profile = twitter.userOperations().getUserProfile("artnames"); assertEquals(161064614, profile.getId()); + assertEquals("161064614", profile.getIdStr()); assertEquals("artnames", profile.getScreenName()); assertEquals("Art Names", profile.getName()); assertEquals("I'm just a normal kinda guy", profile.getDescription()); @@ -150,6 +154,7 @@ public void getUserProfile_screenName_appAuthorization() throws Exception { TwitterProfile profile = appAuthTwitter.userOperations().getUserProfile("artnames"); assertEquals(161064614, profile.getId()); + assertEquals("161064614", profile.getIdStr()); assertEquals("artnames", profile.getScreenName()); assertEquals("Art Names", profile.getName()); assertEquals("I'm just a normal kinda guy", profile.getDescription());