Issue Details (XML | Word | Printable)

Key: NUCREST-4
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Critical Critical
Assignee: Andy Jefferson
Reporter: Giancarlo Dessena
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
DataNucleus API REST

Exception when trying to add SubClass Data over REST API

Created: 11/Jul/11 12:01 PM   Updated: 30/Apr/12 12:02 PM   Resolved: 11/Jul/11 02:26 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 3.0.0.release

Environment:
- Datanucleus 2.2.3 + REST API
- Tomcat 6.0
- MySQL
- Windows XP

Datastore: MySQL


 Description  « Hide
I have set up the following class-model
public class SuperClass {
        private long id;
        private String name;

        /* getters/setters*/
}

public class SubClass extends SuperClass{
        private String additionalProperty;
        /* getters/setters*/
}

with the following metadata file
       <class name="SuperClass">
            <datastore-identity strategy="increment"/>
            <inheritance strategy="new-table">
               <discriminator strategy="class-name"/>
            </inheritance>
            <field
                  name="id"
                  persistence-modifier="persistent"
                  primary-key="true"
                  value-strategy="increment">
                  <column name="MY_PK_COLUMN"/>
            </field>
            <field
                  name="name"
                  persistence-modifier="persistent"/>
                  
        </class>
        <class name="SubClass">
            <inheritance strategy="new-table">
                <join>
                    <column name="MYSUB_PK_COLUMN" target="MY_PK_COLUMN"/>
                </join>
            </inheritance>
                        
            <field name="additionalProperty"
                  persistence-modifier="persistent"/>
        </class>


I can add elements of the SuperClass with the REST API
POST http://localhost:8080/dashboard_2/dn/com.corp.dashboard.jdo.SuperClass {"name":"test"}
HTTP/1.1 201 Created


But when it comes to adding elements of the SubClass I get an exception
POST http://localhost:8080/dashboard_2/dn/com.corp.dashboard.jdo.SubClass {"additionalproperty":"test"}

SCHWERWIEGEND: Servlet.service() for servlet DataNucleus threw exception
java.lang.NullPointerException
        at org.datanucleus.rest.RestServlet$LocalFieldManager.fetchStringField(RestServlet.java:753)
        at org.datanucleus.rest.RestServlet$LocalSM.replacingStringField(RestServlet.java:1308)
        at com.corp.dashboard.jdo.SuperClass.jdoReplaceField(SuperClass.java)
        at com.corp.dashboard.jdo.SubClass.jdoReplaceField(SubClass.java)
        at com.corp.dashboard.jdo.SuperClass.jdoReplaceFields(SuperClass.java)
        at org.datanucleus.rest.RestServlet$LocalSM.replaceFields(RestServlet.java:1354)
        at org.datanucleus.rest.RestServlet.getObjectFromJSONOject(RestServlet.java:701)
        at org.datanucleus.rest.RestServlet.doPost(RestServlet.java:461)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Unknown Source)


Sort Order: Ascending order - Click to sort in descending order
Giancarlo Dessena added a comment - 11/Jul/11 01:00 PM
Solution:


In all fetch methods resolving the field-name is done expecting the relative position of the field in the current Class.
getMetaDataForManagedMemberAtRelativePosition(position).getName();

but when posting/putting a new Object, the servlet uses absolute field positioning,
       int[] fieldNumbers = cmd.getAllMemberPositions(); /*returns absolute field numbers*/
       sm1.replaceFields(fieldNumbers, new LocalFieldManager(jsonobj, null, cmd, pm));
which results in errors when trying to fetch SuperClass fields.

In my case for example when I posted a SuperClass object there were no problems because the SuperClass had no parent class so relative indexing worked well.
But as soon as I posted a SubClass, there were errors when trying to retrieve the fields that were in the SuperClass, because the method getMetaDataForManagedMemberAtRelativePosition could only retrieve the metadata of the SubClass fields.

So I replaced all occourences of getMetaDataForManagedMemberAtRelativePosition with getMetaDataForManagedMemberAtAbsolutePosition and suddenly everything works out as expected.

POST http://localhost:8080/dashboard_2/dn/com.corp.dashboard.jdo.SubClass { name : "test for persistence", "additionalProperty" : "cool"}
HTTP/1.1 201 Created
{
  "additionalProperty" : "cool",
  "class" : "com.corp.dashboard.jdo.SubClass",
  "id" : "61",
  "name" : "test for persistence"
}


Andy Jefferson added a comment - 11/Jul/11 02:26 PM
Replaced the only call to that method. Untested since you don't provide a downloadable testcase as per the docs; left as an exercise to the raiser to test it

Giancarlo Dessena added a comment - 12/Jul/11 10:35 AM
Is a Test-War file ok?

Andy Jefferson added a comment - 12/Jul/11 10:46 AM
Not for me thanks. It's your responsibility to test it, available in nightly builds.

The DN problem reporting guide is very clear what is required if contributing tests, and you could easily follow our tests
http://datanucleus.svn.sourceforge.net/viewvc/datanucleus/test/accessplatform/trunk/test.rest/

Andy Jefferson added a comment - 12/Jul/11 10:55 AM
And please fix your email address, i'm sick of getting bounced mail messages

<giancarlo.dessena@googlemail.de>: Host or domain name not found. Name service
    error for name=googlemail.de type=A: Host found but no data record of
    requested type

Giancarlo Dessena added a comment - 12/Jul/11 12:07 PM
Ok. gonna check out the nightly builds
ps. email fixed