Skip to content

Commit f2f9564

Browse files
committed
DATAREST-825 - Fixed exposure of DELETE HTTP method if findOne(…) is not exposed.
Previously our support for the HTTP DELETE method on item resources was requiring a repository's findOne(…) method to be available and exposed. However, the latter might not be desirable as the support of GET and HEAD requests for item resources depends on that. We now changed that to only checking that a findOne(…) method is declared on the repository as the implementation of RepositoryEntityController.deleteItemResource(…) requires it to be present to be able to trigger the events that intercept deletes for a particular type.
1 parent 3306b5c commit f2f9564

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/CrudMethodsSupportedHttpMethods.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818
import static org.springframework.data.rest.core.mapping.ResourceType.*;
1919
import static org.springframework.http.HttpMethod.*;
2020

21+
import lombok.NonNull;
22+
import lombok.RequiredArgsConstructor;
23+
2124
import java.lang.reflect.Method;
2225
import java.util.Collections;
2326
import java.util.HashSet;
@@ -57,14 +60,15 @@ public CrudMethodsSupportedHttpMethods(CrudMethods crudMethods) {
5760
* @see org.springframework.data.rest.core.mapping.SupportedHttpMethods#getSupportedHttpMethods(org.springframework.data.rest.core.mapping.ResourceType)
5861
*/
5962
@Override
60-
public Set<HttpMethod> getMethodsFor(ResourceType resourcType) {
63+
public Set<HttpMethod> getMethodsFor(ResourceType resourceType) {
6164

62-
Assert.notNull(resourcType, "Resource type must not be null!");
65+
Assert.notNull(resourceType, "Resource type must not be null!");
6366

6467
Set<HttpMethod> methods = new HashSet<HttpMethod>();
6568
methods.add(OPTIONS);
6669

67-
switch (resourcType) {
70+
switch (resourceType) {
71+
6872
case COLLECTION:
6973

7074
if (exposedMethods.exposesFindAll()) {
@@ -80,7 +84,7 @@ public Set<HttpMethod> getMethodsFor(ResourceType resourcType) {
8084

8185
case ITEM:
8286

83-
if (exposedMethods.exposesDelete() && exposedMethods.exposesFindOne()) {
87+
if (exposedMethods.exposesDelete()) {
8488
methods.add(DELETE);
8589
}
8690

@@ -97,7 +101,7 @@ public Set<HttpMethod> getMethodsFor(ResourceType resourcType) {
97101
break;
98102

99103
default:
100-
throw new IllegalArgumentException(String.format("Unsupported resource type %s!", resourcType));
104+
throw new IllegalArgumentException(String.format("Unsupported resource type %s!", resourceType));
101105
}
102106

103107
return Collections.unmodifiableSet(methods);
@@ -134,16 +138,10 @@ public Set<HttpMethod> getMethodsFor(PersistentProperty<?> property) {
134138
/**
135139
* @author Oliver Gierke
136140
*/
141+
@RequiredArgsConstructor
137142
private static class DefaultExposureAwareCrudMethods implements ExposureAwareCrudMethods {
138143

139-
private final CrudMethods crudMethods;
140-
141-
/**
142-
* @param exposedMethods
143-
*/
144-
public DefaultExposureAwareCrudMethods(CrudMethods crudMethods) {
145-
this.crudMethods = crudMethods;
146-
}
144+
private final @NonNull CrudMethods crudMethods;
147145

148146
/*
149147
* (non-Javadoc)
@@ -160,7 +158,7 @@ public boolean exposesSave() {
160158
*/
161159
@Override
162160
public boolean exposesDelete() {
163-
return exposes(crudMethods.getDeleteMethod());
161+
return exposes(crudMethods.getDeleteMethod()) && crudMethods.hasFindOneMethod();
164162
}
165163

166164
/*

spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/CrudMethodsSupportedHttpMethodsUnitTests.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ public void exposesMethodsForProperties() {
121121
allOf(hasItem(GET), not(hasItems(DELETE, PATCH, PUT, POST))));
122122
}
123123

124+
/**
125+
* @see DATAREST-825
126+
*/
127+
@Test
128+
public void supportsDeleteIfFindOneIsHidden() {
129+
assertMethodsSupported(getSupportedHttpMethodsFor(HidesFindOne.class), ITEM, true, DELETE, PATCH, PUT, OPTIONS);
130+
}
131+
132+
/**
133+
* @see DATAREST-825
134+
*/
135+
@Test
136+
public void doesNotSupportDeleteIfNoFindOneAvailable() {
137+
assertMethodsSupported(getSupportedHttpMethodsFor(NoFindOne.class), ITEM, false, DELETE);
138+
}
139+
124140
private static SupportedHttpMethods getSupportedHttpMethodsFor(Class<?> repositoryInterface) {
125141

126142
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
@@ -148,7 +164,18 @@ interface SampleRepository extends CrudRepository<Object, Long> {}
148164
interface HidesDelete extends CrudRepository<Object, Long> {
149165

150166
@RestResource(exported = false)
151-
void delete(Object id);
167+
void delete(Object entity);
168+
}
169+
170+
interface HidesFindOne extends CrudRepository<Object, Long> {
171+
172+
@Override
173+
@RestResource(exported = false)
174+
Object findOne(Long id);
175+
}
176+
177+
interface NoFindOne extends Repository<Object, Long> {
178+
void delete(Object entity);
152179
}
153180

154181
interface EntityRepository extends CrudRepository<Entity, Long> {}

0 commit comments

Comments
 (0)