mehandih / grails-jaxrs

Automatically exported from code.google.com/p/grails-jaxrs
0 stars 0 forks source link

Child objects in has many are not mapped on put or post #60

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Given i have two classes

1.   class Production 
         static hasMany = videos: Video

2.   class Video 

         String fileLocation
         String fileName

         static belongsTo  production: Production

3.  On the get of the production object

api production 6

I get two videos which is great what i want.

Im using backbone.js to modify the model.

It does a put with the info back to grails

Which hits this method in ProductionResource

 PUT
    Response update Production dto 
        println dto.videos.size

        dto.id = id
        ok productionResourceService.update dto

The mapping has already taken place and we have a production dto

the put definately has the videos in it.

 class com.sky.obu.superclips.Production id 6 videos class Video id 2  class Video id 1

but the println dto.videos.size shows that there are no videos on the 
production.

What is the expected output? What do you see instead?

I would expect the child objects of the production also to be mapped.

I am assuming this is correct method opposed to posting back to every domain?

Original issue reported on code.google.com by joe.noa...@gmail.com on 5 Apr 2012 at 10:28

GoogleCodeExporter commented 8 years ago
Hi Joe, 

this should have been fixed with 
http://code.google.com/p/grails-jaxrs/issues/detail?id=21 (see also 
http://groups.google.com/group/grails-jaxrs-discuss/msg/442652f13f931f66). Try 
with this simple example, if it still doesn't work you, please share a small 
project or a failing test so that I can reproduce. 

Thanks, 
Martin

Original comment by krass...@googlemail.com on 6 Apr 2012 at 4:34

GoogleCodeExporter commented 8 years ago
You are correct that the example works but this is not a hasMany relationship.

Change to

class Player { 
  static belongsTo = [team:Team]
  String name 
} 

class Team { 
  String name
  static hasMany = [players :  Player] 
} 

Then post a list of players back in a team then this is where run into troubles.

Original comment by joe.noa...@gmail.com on 10 Apr 2012 at 8:26

GoogleCodeExporter commented 8 years ago
I can reproduce that, thanks for reporting. Hope I have it fixed soon although 
I'm quite busy at the moment with other things.

The jaxrs plugin just uses the default Grails mechanism for JSON 
(un)marshalling. Can you please try with Grails' proprietary REST support if 
you can create a Team-Player 1:n object graph from JSON and let me know? This 
would help me a lot.

Thanks,
Martin

Original comment by krass...@googlemail.com on 11 Apr 2012 at 5:56

GoogleCodeExporter commented 8 years ago
Any luck?

Original comment by joe.noa...@gmail.com on 19 Apr 2012 at 8:51

GoogleCodeExporter commented 8 years ago
Had no time yet, sorry. I'd highly appreciate a patch if you want to submit one.

Thanks,
Martin

Original comment by krass...@googlemail.com on 20 Apr 2012 at 10:05

GoogleCodeExporter commented 8 years ago
I'm experiencing similar behavior, albeit with a twist, as well (with Grails 
2.0.4):
----------------------

$ grails create-app baseball

$ cd baseball

$ grails install-plugin jaxrs

$ grails create-domain-class Player

 -- Player.groovy

 package baseball

 class Player {

     String name

     static constraints = {
     }
 }

$ grails create-domain-class Team

 -- Team.groovy

 package baseball

 class Team {

     String name

     static hasMany = [players: Player]

     static constraints = {
     }
 }

$ grails generate-resources baseball.Player

$ grails generate-resources baseball.Team

$ grails run-app

 -- add some players

$ curl -X POST -d '<player><name>Chris Sabot</name></player>' --header 
'Content-Type: application/xml' http://localhost:8080/baseball/api/player

  <?xml version="1.0" encoding="UTF-8"?><player id="1"><name>Chris Sabo</name></player>

$ curl -X POST -d '<player><name>Barry Larkin</name></player>' --header 
'Content-Type: application/xml' http://localhost:8080/baseball/api/player

  <?xml version="1.0" encoding="UTF-8"?><player id="2"><name>Barry Larkin</name></player>

$ curl -X POST -d '<player><name>Eric Davis</name></player>' --header 
'Content-Type: application/xml' http://localhost:8080/baseball/api/player

  <?xml version="1.0" encoding="UTF-8"?><player id="3"><name>Eric Davis</name></player>

 -- create a team with two players

$ curl -X POST -d '<team><name>Cincinnati Reds</name><players><player id="1" 
/><player id="2" /></players></team>' --header 'Content-Type: application/xml' 
http://localhost:8080/baseball/api/team

  <?xml version="1.0" encoding="UTF-8"?><team id="1"><name>Cincinnati Reds</name><players /></team>

 -- notice that <players /> is empty
 -- if we pass the ids as elements rather than attributes:

$ curl -X POST -d '<team><name>Cincinnati 
Reds</name><players><player><id>1</id></player><player><id>2</id></player></play
ers></team>' --header 'Content-Type: application/xml' 
http://localhost:8080/baseball/api/team 

  <?xml version="1.0" encoding="UTF-8"?><team id="2"><name>Cincinnati Reds</name><players><player id="1" /><player id="1" /></players></team>

 -- player id 1 is duplicated.
 -- if we POST a team with three players:

$ curl -X POST -d '<team><name>Cincinnati 
Reds</name><players><player><id>1</id></player><player><id>2</id></player><playe
r><id>3</id></player></players></team>' --header 'Content-Type: 
application/xml' http://localhost:8080/baseball/api/team 

  Error 500: Internal Server Error
  URI /baseball/api/team
  Class org.hibernate.HibernateException 
    Message identifier of an instance of baseball.Player was altered from 2 to 1 
    Around line 22 of grails-app/resources/baseball/TeamCollectionResource.groovy
  19:
  20:     @POST
  21:     Response create(Team dto) {
  22:         created teamResourceService.create(dto)
  23:     }
  24:
  25:     @GET

    Around line 192 of JaxrsContext.java
  189:
  190:    public void process(HttpServletRequest request, HttpServletResponse response)
  191:            throws ServletException, IOException {
  192:        jaxrsServlet.service(request, response);
  193:    }
  194:
  195:}

        Around line 48 of grails-app/controllers/org/grails/jaxrs/JaxrsController.groovy
  45:     def unwrapped = unwrap(response)
  46:
  47:     // Let the JAX-RS implementation process the request
  48:     jaxrsContext.jaxrsService.process(new RequestWrapper(request), unwrapped)
  49:
  50:     // Marks the response as committed
  51:     unwrapped.outputStream.flush()

        Around line 46 of JaxrsFilter.java
  43:     HttpServletResponse response, FilterChain chain)
  44:             throws ServletException, IOException {
  45:         JaxrsUtils.setRequestUriAttribute(request, request.getRequestURI());
  46:         chain.doFilter(request, response);
  47:     }
  48:
  49:}

  Trace
    Line | Method
      22 | create           in TeamCollectionResource.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     60 | invoke           in 
com.sun.jersey.spi.container.JavaMethodInvokerFactory$1
|    205 | _dispatch . . .  in 
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchP
rovider$ResponseOutInvoker
|     75 | dispatch         in 
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher
|    288 | accept . . . . . in 
com.sun.jersey.server.impl.uri.rules.HttpMethodRule
|    108 | accept           in 
com.sun.jersey.server.impl.uri.rules.ResourceClassRule
|    147 | accept . . . . . in 
com.sun.jersey.server.impl.uri.rules.RightHandPathRule
|     84 | accept           in 
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule
|   1469 | _handleRequest . in 
com.sun.jersey.server.impl.application.WebApplicationImpl
|   1400 | _handleRequest   in     ''
|   1349 | handleRequest .  in     ''
|   1339 | handleRequest    in     ''
|    416 | service . . . .  in com.sun.jersey.spi.container.servlet.WebComponent
|    537 | service          in 
com.sun.jersey.spi.container.servlet.ServletContainer
|    699 | service . . . .  in     ''
|    192 | process          in JaxrsContext.java
|     48 | doCall . . . . . in JaxrsController.groovy
|     46 | doFilterInternal in JaxrsFilter.java
|   1110 | runWorker . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run              in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . .  in java.lang.Thread

Original comment by x011...@gmail.com on 26 Jun 2012 at 1:27