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.
This commit is contained in:
Jens Schauder
2017-02-02 09:48:07 +01:00
parent d2386dc64e
commit e94ba4a1fa
3 changed files with 36 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
* {@link org.springframework.data.repository.Repository} is exported under that URL path segment. Also ensures the
* {@link OpenEntityManagerInViewInterceptor} is registered in the application context. The OEMIVI is required for the
* REST exporter to function properly.
*
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Mark Paluch
@@ -75,7 +75,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
/**
* Creates a new {@link RepositoryRestHandlerMapping} for the given {@link ResourceMappings} and
* {@link RepositoryRestConfiguration}.
*
*
* @param mappings must not be {@literal null}.
* @param config must not be {@literal null}.
*/
@@ -102,8 +102,8 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
this.mappings = mappings;
this.configuration = config;
this.repositories = repositories;
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories,
NoOpStringValueResolver.INSTANCE);
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings,
NoOpStringValueResolver.INSTANCE, repositories);
}
/**
@@ -122,8 +122,8 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
super.setEmbeddedValueResolver(resolver);
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories,
resolver == null ? NoOpStringValueResolver.INSTANCE : resolver);
this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings,
resolver == null ? NoOpStringValueResolver.INSTANCE : resolver, repositories);
}
/*
@@ -223,7 +223,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
/**
* Returns the first segment of the given repository lookup path.
*
*
* @param repositoryLookupPath must not be {@literal null}.
* @return
*/
@@ -266,14 +266,14 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
static class RepositoryCorsConfigurationAccessor {
private final @NonNull ResourceMappings mappings;
private final @NonNull Repositories repositories;
private final @NonNull StringValueResolver embeddedValueResolver;
private final Repositories repositories;
CorsConfiguration findCorsConfiguration(String lookupPath) {
ResourceMetadata resource = getResourceMetadata(getRepositoryBasePath(lookupPath));
return resource != null ? createConfiguration(
return resource != null && repositories != null ? createConfiguration(
repositories.getRepositoryInformationFor(resource.getDomainType()).getRepositoryInterface()) : null;
}

View File

@@ -15,8 +15,10 @@
*/
package org.springframework.data.rest.webmvc;
import static java.util.Collections.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
@@ -24,7 +26,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.NoOpStringValueResolver;
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor;
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -49,7 +53,7 @@ public class RepositoryCorsConfigurationAccessorUnitTests {
@Before
public void before() throws Exception {
accessor = new RepositoryCorsConfigurationAccessor(mappings, repositories, NoOpStringValueResolver.INSTANCE);
accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, repositories);
}
@Test // DATAREST-573
@@ -82,6 +86,21 @@ public class RepositoryCorsConfigurationAccessorUnitTests {
assertThat(configuration.getMaxAge(), is(1234L));
}
@Test // DATAREST-994
public void returnsNullCorsConfigurationWithNullRepositories() {
accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, null);
ResourceMetadata resourceMetadata = mock(ResourceMetadata.class);
when(resourceMetadata.getPath()).thenReturn(new Path("/people"));
when(resourceMetadata.isExported()).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
when(mappings.iterator()).thenReturn(singletonList(resourceMetadata).iterator());
assertThat(accessor.findCorsConfiguration("/people"), is(nullValue()));
}
interface PlainRepository {}
@CrossOrigin

View File

@@ -42,7 +42,7 @@ import org.springframework.web.method.HandlerMethod;
/**
* Unit tests for {@link RepositoryRestHandlerMapping}.
*
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
@@ -212,4 +212,9 @@ public class RepositoryRestHandlerMappingUnitTests {
assertThat(handlerMapping.getHandler(mockRequest), is(nullValue()));
}
@Test // DATAREST-994
public void twoArgumentConstructorDoesNotThrowException() {
new RepositoryRestHandlerMapping(mappings, configuration);
}
}