Skip to content

DATAREST-994 - Fixed two argument constructor of RepositoryRestHandlerMapping #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -55,7 +55,7 @@
* {@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
Expand All @@ -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}.
*/
Expand All @@ -102,8 +102,8 @@ public RepositoryRestHandlerMapping(ResourceMappings mappings, RepositoryRestCon
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);
}

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

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);
}

/*
Expand Down Expand Up @@ -223,7 +223,7 @@ protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequ

/**
* Returns the first segment of the given repository lookup path.
*
*
* @param repositoryLookupPath must not be {@literal null}.
* @return
*/
Expand Down Expand Up @@ -266,14 +266,14 @@ public String resolveStringValue(String value) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
*/
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;
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;
Expand All @@ -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
Expand Down Expand Up @@ -82,6 +86,21 @@ public void createConfigurationShouldConstructFullCorsConfiguration() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

/**
* Unit tests for {@link RepositoryRestHandlerMapping}.
*
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
Expand Down Expand Up @@ -212,4 +212,9 @@ public void rejectsUnexpandedUriTemplateWithNotFound() throws Exception {

assertThat(handlerMapping.getHandler(mockRequest), is(nullValue()));
}

@Test // DATAREST-994
public void twoArgumentConstructorDoesNotThrowException() {
new RepositoryRestHandlerMapping(mappings, configuration);
}
}