Permalink
Browse files

Fixed a bug in dealing with PUT/update

  • Loading branch information...
1 parent f8fc6a5 commit 8d316a0c32dfb94a3a03512d461ca1d0825342f2 @jbrisbin jbrisbin committed Apr 18, 2012
View
@@ -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 ->
@@ -392,25 +392,36 @@ public void createOrUpdate(ServerHttpRequest request,
} 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<String, Attribute> 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 @@ private Object resolveTopLevelResource(URI baseUri, String uri) {
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<? extends Serializable> idType = entityInfo.getIdType();
Serializable serId = stringToSerializable(sId, idType);
@@ -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()

0 comments on commit 8d316a0

Please sign in to comment.