Skip to content

Commit ea482e0

Browse files
committed
DATAREST-994 - Fixed two argument constructor of RepositoryRestHandlerMapping.
Repositories in RepositoryCorsConfigurationAccessor may be null now. findCorsConfiguration returns null when no repositories are provided. Original pull request: #257.
1 parent b06f866 commit ea482e0

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -55,7 +55,7 @@
5555
* {@link org.springframework.data.repository.Repository} is exported under that URL path segment. Also ensures the
5656
* {@link OpenEntityManagerInViewInterceptor} is registered in the application context. The OEMIVI is required for the
5757
* REST exporter to function properly.
58-
*
58+
*
5959
* @author Jon Brisbin
6060
* @author Oliver Gierke
6161
* @author Mark Paluch
@@ -75,7 +75,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
7575
/**
7676
* Creates a new {@link RepositoryRestHandlerMapping} for the given {@link ResourceMappings} and
7777
* {@link RepositoryRestConfiguration}.
78-
*
78+
*
7979
* @param mappings must not be {@literal null}.
8080
* @param config must not be {@literal null}.
8181
*/
@@ -102,8 +102,8 @@ public RepositoryRestHandlerMapping(ResourceMappings mappings, RepositoryRestCon
102102
this.mappings = mappings;
103103
this.configuration = config;
104104
this.repositories = repositories;
105-
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories,
106-
NoOpStringValueResolver.INSTANCE);
105+
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings,
106+
NoOpStringValueResolver.INSTANCE, repositories);
107107
}
108108

109109
/**
@@ -122,8 +122,8 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
122122

123123
super.setEmbeddedValueResolver(resolver);
124124

125-
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories,
126-
resolver == null ? NoOpStringValueResolver.INSTANCE : resolver);
125+
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings,
126+
resolver == null ? NoOpStringValueResolver.INSTANCE : resolver, repositories);
127127
}
128128

129129
/*
@@ -223,7 +223,7 @@ protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequ
223223

224224
/**
225225
* Returns the first segment of the given repository lookup path.
226-
*
226+
*
227227
* @param repositoryLookupPath must not be {@literal null}.
228228
* @return
229229
*/
@@ -266,14 +266,14 @@ public String resolveStringValue(String value) {
266266
static class RepositoryCorsConfigurationAccessor {
267267

268268
private final @NonNull ResourceMappings mappings;
269-
private final @NonNull Repositories repositories;
270269
private final @NonNull StringValueResolver embeddedValueResolver;
270+
private final Repositories repositories;
271271

272272
CorsConfiguration findCorsConfiguration(String lookupPath) {
273273

274274
ResourceMetadata resource = getResourceMetadata(getRepositoryBasePath(lookupPath));
275275

276-
return resource != null ? createConfiguration(
276+
return resource != null && repositories != null ? createConfiguration(
277277
repositories.getRepositoryInformationFor(resource.getDomainType()).getRepositoryInterface()) : null;
278278
}
279279

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryCorsConfigurationAccessorUnitTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
*/
1616
package org.springframework.data.rest.webmvc;
1717

18+
import static java.util.Collections.*;
1819
import static org.hamcrest.MatcherAssert.*;
1920
import static org.hamcrest.Matchers.*;
21+
import static org.mockito.Mockito.*;
2022

2123
import org.junit.Before;
2224
import org.junit.Test;
2325
import org.junit.runner.RunWith;
2426
import org.mockito.Mock;
2527
import org.mockito.runners.MockitoJUnitRunner;
2628
import org.springframework.data.repository.support.Repositories;
29+
import org.springframework.data.rest.core.Path;
2730
import org.springframework.data.rest.core.mapping.ResourceMappings;
31+
import org.springframework.data.rest.core.mapping.ResourceMetadata;
2832
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.NoOpStringValueResolver;
2933
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor;
3034
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -49,7 +53,7 @@ public class RepositoryCorsConfigurationAccessorUnitTests {
4953

5054
@Before
5155
public void before() throws Exception {
52-
accessor = new RepositoryCorsConfigurationAccessor(mappings, repositories, NoOpStringValueResolver.INSTANCE);
56+
accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, repositories);
5357
}
5458

5559
@Test // DATAREST-573
@@ -82,6 +86,21 @@ public void createConfigurationShouldConstructFullCorsConfiguration() {
8286
assertThat(configuration.getMaxAge(), is(1234L));
8387
}
8488

89+
@Test // DATAREST-994
90+
public void returnsNullCorsConfigurationWithNullRepositories() {
91+
92+
accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, null);
93+
94+
ResourceMetadata resourceMetadata = mock(ResourceMetadata.class);
95+
when(resourceMetadata.getPath()).thenReturn(new Path("/people"));
96+
when(resourceMetadata.isExported()).thenReturn(true);
97+
98+
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
99+
when(mappings.iterator()).thenReturn(singletonList(resourceMetadata).iterator());
100+
101+
assertThat(accessor.findCorsConfiguration("/people"), is(nullValue()));
102+
}
103+
85104
interface PlainRepository {}
86105

87106
@CrossOrigin

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
/**
4444
* Unit tests for {@link RepositoryRestHandlerMapping}.
45-
*
45+
*
4646
* @author Oliver Gierke
4747
* @author Greg Turnquist
4848
*/
@@ -212,4 +212,9 @@ public void rejectsUnexpandedUriTemplateWithNotFound() throws Exception {
212212

213213
assertThat(handlerMapping.getHandler(mockRequest), is(nullValue()));
214214
}
215+
216+
@Test // DATAREST-994
217+
public void twoArgumentConstructorDoesNotThrowException() {
218+
new RepositoryRestHandlerMapping(mappings, configuration);
219+
}
215220
}

0 commit comments

Comments
 (0)