From 8d316a0c32dfb94a3a03512d461ca1d0825342f2 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Wed, 18 Apr 2012 11:21:23 -0500 Subject: [PATCH] Fixed a bug in dealing with PUT/update --- build.gradle | 2 +- .../rest/webmvc/RepositoryRestController.java | 37 ++++++++++++++----- .../spec/RepositoryRestControllerSpec.groovy | 12 +++++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 769574467..be17217ef 100644 --- a/build.gradle +++ b/build.gradle @@ -61,7 +61,7 @@ subprojects { } -task wrapper(type: Wrapper) { gradleVersion = "1.0-milestone-9" } +task wrapper(type: Wrapper) { gradleVersion = "1.0-rc-1" } idea { project.ipr.withXml { provider -> diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index a0d514833..43011e870 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -392,25 +392,36 @@ public class RepositoryRestController implements InitializingBean { } else { final MediaType incomingMediaType = request.getHeaders().getContentType(); try { - final Object incoming = readIncoming(request, incomingMediaType, typeMeta.domainClass); - if (null == incoming) { - model.addAttribute(STATUS, HttpStatus.BAD_REQUEST); - } else { - typeMeta.entityMetadata.id(serId, incoming); - - Object savedEntity = repo.save(entity); - String savedId = typeMeta.entityInfo.getId(savedEntity).toString(); - - if (request.getMethod() == HttpMethod.POST) { + if (request.getMethod() == HttpMethod.POST) { + final Object incoming = readIncoming(request, incomingMediaType, typeMeta.domainClass); + if (null == incoming) { + model.addAttribute(STATUS, HttpStatus.BAD_REQUEST); + } else { + typeMeta.entityMetadata.id(serId, incoming); + Object savedEntity = repo.save(entity); + String savedId = typeMeta.entityInfo.getId(savedEntity).toString(); URI selfUri = buildUri(baseUri, repository, savedId); HttpHeaders headers = new HttpHeaders(); headers.set(LOCATION, selfUri.toString()); model.addAttribute(HEADERS, headers); model.addAttribute(STATUS, HttpStatus.CREATED); + } + } else { + final Map incoming = readIncoming(request, incomingMediaType, Map.class); + if (null == incoming) { + model.addAttribute(STATUS, HttpStatus.BAD_REQUEST); } else { + for (Map.Entry entry : typeMeta.entityMetadata.embeddedAttributes().entrySet()) { + String name = entry.getKey(); + if (incoming.containsKey(name)) { + typeMeta.entityMetadata.set(name, incoming.get(name), entity); + } + } + repo.save(entity); model.addAttribute(STATUS, HttpStatus.NO_CONTENT); } } + } catch (IOException e) { model.addAttribute(STATUS, HttpStatus.BAD_REQUEST); LOG.error(e.getMessage(), e); @@ -889,7 +900,13 @@ public class RepositoryRestController implements InitializingBean { String sId = UriUtils.path(uris.get(1)); CrudRepository repo = repositoryMetadata.repositoryFor(repoName); + if (null == repo) { + return null; + } EntityInformation entityInfo = repositoryMetadata.entityInfoFor(repo); + if (null == entityInfo) { + return null; + } Class idType = entityInfo.getIdType(); Serializable serId = stringToSerializable(sId, idType); diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy index 6abfbc564..757217cc7 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy @@ -73,6 +73,16 @@ class RepositoryRestControllerSpec extends Specification { then: model.status == HttpStatus.CREATED + when: "updating an entity" + model.clear() + req = createRequest("PUT", "person/1") + data = mapper.writeValueAsBytes([name: "Johnnie Doe"]) + req.content = data + controller.createOrUpdate(new ServletServerHttpRequest(req), uriBuilder, "person", "1", model) + + then: + model.status == HttpStatus.NO_CONTENT + when: "listing available entities" model.clear() controller.listEntities(uriBuilder, "person", model) @@ -88,7 +98,7 @@ class RepositoryRestControllerSpec extends Specification { controller.entity(new ServletServerHttpRequest(req), uriBuilder, "person", "1", model) then: - model.resource?.name == "John Doe" + model.resource?.name == "Johnnie Doe" when: "creating child entity" model.clear()