Fix access to ScmPathInfoStore as Provider to prevent injection errors (#1889)

Fix access to ScmPathInfoStore as Provider to prevent injection errors. Errors might happen, if beans using ScmPathInfoStore (like the BranchLinkProvider) are injected in beans that are singletons or that have be created outside of request scope otherwise. We do so knowing that this might lead to runtime errors (eg. when links shall be build in a thread that has no request scope). We have decided nonetheless that injection errors on startup are worse for plugin developers, that may have no clue how to solve injection problems.
This commit is contained in:
René Pfeuffer
2021-12-09 07:05:12 +01:00
committed by GitHub
parent 49a2a90c0d
commit 65d1e4ffd2
10 changed files with 74 additions and 63 deletions

View File

@@ -24,6 +24,7 @@
package sonia.scm.api.v2.resources;
import com.google.inject.util.Providers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sonia.scm.repository.Repository;
@@ -44,7 +45,7 @@ class BranchDetailsMapperTest {
void configureMapper() {
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.set(() -> URI.create("/scm/api/"));
mapper.setResourceLinks(new ResourceLinks(scmPathInfoStore));
mapper.setResourceLinks(new ResourceLinks(Providers.of(scmPathInfoStore)));
}
@Test

View File

@@ -24,6 +24,7 @@
package sonia.scm.api.v2.resources;
import com.google.inject.util.Providers;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -74,7 +75,7 @@ class BranchDetailsResourceTest extends RepositoryTestBase {
dispatcher.addSingletonResource(getRepositoryRootResource());
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.set(() -> URI.create("/scm/api/"));
mapper.setResourceLinks(new ResourceLinks(scmPathInfoStore));
mapper.setResourceLinks(new ResourceLinks(Providers.of(scmPathInfoStore)));
}
@Test

View File

@@ -24,6 +24,7 @@
package sonia.scm.api.v2.resources;
import com.google.inject.util.Providers;
import de.otto.edison.hal.HalRepresentation;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
@@ -53,7 +54,7 @@ public class GroupCollectionToDtoMapperTest {
private final ScmPathInfo uriInfo = mock(ScmPathInfo.class);
private final ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
private final ResourceLinks resourceLinks = new ResourceLinks(scmPathInfoStore);
private final ResourceLinks resourceLinks = new ResourceLinks(Providers.of(scmPathInfoStore));
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
private final Subject subject = mock(Subject.class);
private final ThreadState subjectThreadState = new SubjectThreadState(subject);

View File

@@ -25,6 +25,7 @@
package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableSet;
import com.google.inject.util.Providers;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.apache.shiro.subject.Subject;
@@ -69,7 +70,7 @@ class MetricsIndexEnricherTest {
void setUpResourceLinks() {
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.set(() -> URI.create("/"));
resourceLinks = () -> new ResourceLinks(scmPathInfoStore);
resourceLinks = () -> new ResourceLinks(Providers.of(scmPathInfoStore));
}
@BeforeEach

View File

@@ -24,6 +24,8 @@
package sonia.scm.api.v2.resources;
import com.google.inject.util.Providers;
import java.net.URI;
import static org.mockito.Mockito.spy;
@@ -35,7 +37,7 @@ public class ResourceLinksMock {
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.set(pathInfo);
ResourceLinks resourceLinks = new ResourceLinks(scmPathInfoStore);
ResourceLinks resourceLinks = new ResourceLinks(Providers.of(scmPathInfoStore));
return spy(resourceLinks);
}
}

View File

@@ -26,15 +26,17 @@ package sonia.scm.api.v2.resources;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.net.URI;
import static com.google.inject.util.Providers.of;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
public class ResourceLinksTest {
private static final String BASE_URL = "http://example.com/";
@@ -44,7 +46,6 @@ public class ResourceLinksTest {
@Mock
private ScmPathInfo uriInfo;
@InjectMocks
private ResourceLinks resourceLinks;
@Test
@@ -229,8 +230,8 @@ public class ResourceLinksTest {
@Before
public void initUriInfo() {
initMocks(this);
when(scmPathInfoStore.get()).thenReturn(uriInfo);
when(uriInfo.getApiRestUri()).thenReturn(URI.create(BASE_URL));
resourceLinks = new ResourceLinks(of(scmPathInfoStore));
}
}

View File

@@ -25,6 +25,7 @@
package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.inject.util.Providers;
import de.otto.edison.hal.HalRepresentation;
import lombok.Getter;
import lombok.Setter;
@@ -95,7 +96,7 @@ class SearchResourceTest {
QueryResultMapper queryResultMapper = Mappers.getMapper(QueryResultMapper.class);
queryResultMapper.setRepositoryManager(repositoryManager);
queryResultMapper.setResourceLinks(new ResourceLinks(scmPathInfoStore));
queryResultMapper.setResourceLinks(new ResourceLinks(Providers.of(scmPathInfoStore)));
SearchableTypeMapper searchableTypeMapper = Mappers.getMapper(SearchableTypeMapper.class);
queryResultMapper.setRegistry(enricherRegistry);