Skip to content

Commit 3715b1c

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.
1 parent 06ebcb9 commit 3715b1c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

Lines changed: 3 additions & 3 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.
@@ -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;
269+
private final Repositories repositories;
270270
private final @NonNull StringValueResolver embeddedValueResolver;
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 & 0 deletions
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;
@@ -82,6 +86,22 @@ public void createConfigurationShouldConstructFullCorsConfiguration() {
8286
assertThat(configuration.getMaxAge(), is(1234L));
8387
}
8488

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

87107
@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)