thiagolocatelli / parse4j

Java Library to deal with Parse (parse.com) REST API
http://thiagolocatelli.github.io/parse4j
143 stars 117 forks source link

ParseFile must be saved before being set on a ParseObject #17

Closed lucrus73 closed 9 years ago

lucrus73 commented 10 years ago

I use the following code with parse4j 1.3 in a wicket web application:

        ParseQuery pq = ParseQuery.getQuery(Event.class);
        pq.skip((int)first);
        pq.limit((int)count);
        return pq.find().iterator();

The find() call raises an IllegalArgumentException: ParseFile must be saved before being set on a ParseObject. However I'm not saving any object, I'm only fetching. Here is my Event class:

@ParseClassName("Event")
public class Event extends ParseObject
{

  public String getEventLinkRegistration()
  {
    return getString("eventLinkRegistration");
  }

  public void setEventLinkRegistration(String eventLinkRegistration)
  {
    put("eventLinkRegistration", eventLinkRegistration);
  }

  public String getEventContactName()
  {
    return getString("eventContactName");
  }

  public void setEventContactName(String eventContactName)
  {
    put("eventContactName", eventContactName);
  }

  public String getEventContactMail()
  {
    return getString("eventContactMail");
  }

  public void setEventContactMail(String eventContactMail)
  {
    put("eventContactMail", eventContactMail);
  }

  public String getEventContactPhone()
  {
    return getString("eventContactPhone");
  }

  public void setEventContactPhone(String eventContactPhone)
  {
    put("eventContactPhone", eventContactPhone);
  }

  public String getEventAddress()
  {
    return getString("eventAddress");
  }

  public void setEventAddress(String eventAddress)
  {
    put ("eventAddress", eventAddress);
  }

  public String getEventCity()
  {
    return getString("eventCity");
  }

  public void setEventCity(String eventCity)
  {
    put("eventCity", eventCity);
  }

  public ParseRelation getEventCountry()
  {
    return getRelation("eventCountry");
  }

  public void setEventCountry(ParseRelation eventCountry)
  {
    put("eventCountry", eventCountry);
  }

  public String getEventTitle()
  {
    return getString("eventTitle");
  }

  public void setEventTitle(String eventTitle)
  {
    put("eventTitle", eventTitle);
  }

  public String getEventText()
  {
    return getString("eventText");
  }

  public void setEventText(String eventText)
  {
    put("eventText", eventText);
  }

  public Date getEventDate()
  {
    return getDate("eventDate");
  }

  public void setEventDate(Date eventDate)
  {
    put("eventDate", eventDate);
  }

  public ParseFile getEventImg()
  {
    return getParseFile("eventImg");
  }

  public void setEventImg(ParseFile eventImg)
  {
    put("eventImg", eventImg);
  }

  public ParseGeoPoint getEventPosition()
  {
    return getParseGeoPoint("eventPosition");
  }

  public void setEventPosition(ParseGeoPoint eventPosition)
  {
    put("eventPosition", eventPosition);
  }

}

Here is the relevant part of the stack trace:

Caused by: java.lang.IllegalArgumentException: ParseFile must be saved before being set on a ParseObject. at org.parse4j.ParseObject.put(ParseObject.java:359) at org.parse4j.ParseObject.setData(ParseObject.java:725) at org.parse4j.ParseQuery.find(ParseQuery.java:499)

lucrus73 commented 10 years ago

I cloned the master repo and fixed this issue locally a few minutes ago, however I never used github before and got lost while trying to understand how to send you my commit. Please advice.

drmillan commented 10 years ago

if you send me the code i could test it (have the same problem) and make a pull request to @thiagolocatelli can merge it to master.

thiagolocatelli commented 10 years ago

yes, send a pull request and I will merge the changes right away... :) Thanks guys

On Fri, Sep 5, 2014 at 8:51 AM, Daniel Rodríguez Millán < notifications@github.com> wrote:

if you send me the code i could test it (have the same problem) and make a pull request to @thiagolocatelli https://github.com/thiagolocatelli can merge it to master.

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-54620668 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')

lucrus73 commented 10 years ago

Here is the output of git diff, hope that's what you need.

diff --git a/src/main/java/org/parse4j/ParseObject.java b/src/main/java/org/parse4j/ParseObject.java
index b6ed735..537598c 100644
--- a/src/main/java/org/parse4j/ParseObject.java
+++ b/src/main/java/org/parse4j/ParseObject.java
@@ -337,6 +337,17 @@ public class ParseObject {
    }

    public void put(String key, Object value) {
+    put(key, value, false);
+  }
+  
+  /**
+   * 
+   * @param key
+   * @param value
+   * @param disableChecks some checks have to be skipped during fetch. Currently the only
+   * effect of passing true here is to disable the check on uploaded files. See issue #17 on github.
+   */
+  protected void put(String key, Object value, boolean disableChecks) {

        if (key == null) {
            LOGGER.error("key may not be null.");
@@ -354,7 +365,7 @@ public class ParseObject {
                    "ParseFile must be saved before being set on a ParseObject.");
        }

-       if (value instanceof ParseFile && !((ParseFile) value).isUploaded()) {
+       if (value instanceof ParseFile && !((ParseFile) value).isUploaded() && !disableChecks) {
            LOGGER.error("ParseFile must be saved before being set on a ParseObject.");
            throw new IllegalArgumentException(
                    "ParseFile must be saved before being set on a ParseObject.");
@@ -712,7 +723,11 @@ public class ParseObject {

    }

-   protected void setData(JSONObject jsonObject) {
+  protected void setData(JSONObject jsonObject) {
+    setData(jsonObject, false);  
+  }
+  
+  protected void setData(JSONObject jsonObject, boolean disableChecks) {

        Iterator it = jsonObject.keys();
        while (it.hasNext()) {
@@ -722,7 +737,7 @@ public class ParseObject {
                setReservedKey(key, value);
            }
            else {
-               put(key, ParseDecoder.decode(value));
+               put(key, ParseDecoder.decode(value), disableChecks);
            }
        }

diff --git a/src/main/java/org/parse4j/ParseQuery.java b/src/main/java/org/parse4j/ParseQuery.java
index 2359e77..6339f27 100644
--- a/src/main/java/org/parse4j/ParseQuery.java
+++ b/src/main/java/org/parse4j/ParseQuery.java
@@ -524,13 +524,21 @@ public class ParseQuery {
                    if(clazz != null) {
                        T po = (T) clazz.newInstance();
                        JSONObject obj = (JSONObject) objs.get(i);
-                       po.setData(obj);
+            
+            /*
+            We disable some checks while setting data in objects during fetch because
+            those checks are useful only when setting data from client
+            code. The "true" argument disables such checks.
+            */
+                       po.setData(obj, true);
                        results.add((T) po);
                    }
                    else {
                        ParseObject po = new ParseObject(getClassName());
                        JSONObject obj = (JSONObject) objs.get(i);
-                       po.setData(obj);
+            
+            // see above for the "true" argument
+                       po.setData(obj, true);
                        results.add((T) po);
                    }
                }
lucrus73 commented 9 years ago

Any news?

davisg123 commented 9 years ago

I had this issue, but it was fixed with https://github.com/thiagolocatelli/parse4j/commit/f2c1306a3fab79291bf89bba77ce92b1682b31e7

tibuurcio commented 9 years ago

I'm having this issue, but I'm using maven and don't know how to change the file so it works. Can I use the library somehow without maven?

EDIT: I see that the problem was fixed but I'm still getting the error...

EDIT2: Solved by downloading library from http://thiagolocatelli.github.io/parse4j/ and loading the project directly on IntelliJ.

Fjnova commented 9 years ago

hi guys, Is this issue fixed in ver 1.3 ?

I'm still get this error when I got data

lucrus73 commented 9 years ago

I'm not sure, but I suspect 1.3 will always have the issue, because I reported the bug against 1.3 in the first place... maybe my patch will be included in a future release, maybe not, but 1.3 was released before I reported the bug.

Gsxgit commented 9 years ago

Can you upload the latest version to maven repository? Thanks

thiagolocatelli commented 9 years ago

I will manage to get the new version out there as soon as I can! Thank you

On Mon, Mar 2, 2015 at 9:19 AM, Gareth Lacey notifications@github.com wrote:

Can you upload the latest version to maven repository? Thanks

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-76718692 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')

Gsxgit commented 9 years ago

I see the readme has been updated saying that v1.4 is available but it doesn't appear to be working in maven.

thiagolocatelli commented 9 years ago

What kinda of error are you getting?

On Mon, Mar 2, 2015 at 3:16 PM, Gareth Lacey notifications@github.com wrote:

I see the readme has been updated saying that v1.4 is available but it doesn't appear to be working in maven.

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-76806829 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')

Gsxgit commented 9 years ago

Missing artifact com.github.thiagolocatelli:parse4j:jar:1.4. 1.3 works fine though.

thiagolocatelli commented 9 years ago

Thats interesting, I just tested it here and it is working, let me retry after deleting my .m2 folder

On Mon, Mar 2, 2015 at 3:28 PM, Gareth Lacey notifications@github.com wrote:

Missing artifact com.github.thiagolocatelli:parse4j:jar:1.4. 1.3 works fine though.

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-76810604 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')

Gsxgit commented 9 years ago

It's not on here either: http://mvnrepository.com/artifact/com.github.thiagolocatelli/parse4j

thiagolocatelli commented 9 years ago

its available here:

http://search.maven.org/#artifactdetails%7Ccom.github.thiagolocatelli%7Cparse4j%7C1.4%7Cjar

I have just removed my .m2 folder and forced the project to download the jar again and it worked fine for me

On Mon, Mar 2, 2015 at 3:30 PM, Gareth Lacey notifications@github.com wrote:

It's not on here either: http://mvnrepository.com/artifact/com.github.thiagolocatelli/parse4j

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-76811570 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')

Gsxgit commented 9 years ago

I have managed to fix it. Thanks for the help!

thiagolocatelli commented 9 years ago

No problem

On Mon, Mar 2, 2015 at 3:37 PM, Gareth Lacey notifications@github.com wrote:

I have managed to fix it. Thanks for the help!

— Reply to this email directly or view it on GitHub https://github.com/thiagolocatelli/parse4j/issues/17#issuecomment-76813993 .

"thiago:locatelli$gmail:com".replace(':','.').replace('$','@')