Skip to content

Commit 48415f8

Browse files
committed
Required a bean of type 'org.springframework.data.rest.webmvc.mapping.Associations' that could not be found. Fixes #2828
1 parent efd2f74 commit 48415f8

File tree

2 files changed

+24
-53
lines changed

2 files changed

+24
-53
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocDataRestConfiguration.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,16 @@
5555
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
5656
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
5757
import org.springframework.boot.autoconfigure.hateoas.HateoasProperties;
58-
import org.springframework.context.ApplicationContext;
5958
import org.springframework.context.annotation.Bean;
6059
import org.springframework.context.annotation.Configuration;
6160
import org.springframework.context.annotation.ImportRuntimeHints;
6261
import org.springframework.context.annotation.Lazy;
6362
import org.springframework.context.annotation.Primary;
6463
import org.springframework.data.domain.Sort;
65-
import org.springframework.data.mapping.context.PersistentEntities;
66-
import org.springframework.data.repository.support.Repositories;
6764
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
68-
import org.springframework.data.rest.core.mapping.ResourceMappings;
6965
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
7066
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping;
7167
import org.springframework.data.rest.webmvc.RootResourceInformation;
72-
import org.springframework.data.rest.webmvc.mapping.Associations;
7368
import org.springframework.data.rest.webmvc.support.DefaultedPageable;
7469
import org.springframework.data.rest.webmvc.support.ETag;
7570
import org.springframework.hateoas.server.LinkRelationProvider;
@@ -125,25 +120,19 @@ static class SpringRepositoryRestResourceProviderConfiguration {
125120
/**
126121
* Spring repository rest resource provider spring repository rest resource provider.
127122
*
128-
* @param mappings the mappings
129-
* @param repositories the repositories
130-
* @param associations the associations
131-
* @param applicationContext the application context
132123
* @param dataRestRouterOperationService the data rest router operation service
133-
* @param persistentEntities the persistent entities
134124
* @param mapper the mapper
135125
* @param springDocDataRestUtils the spring doc data rest utils
136126
* @return the spring repository rest resource provider
137127
*/
138128
@Bean
139129
@ConditionalOnMissingBean
140130
@Lazy(false)
141-
SpringRepositoryRestResourceProvider springRepositoryRestResourceProvider(ResourceMappings mappings,
142-
Repositories repositories, Associations associations, ApplicationContext applicationContext,
143-
DataRestRouterOperationService dataRestRouterOperationService, PersistentEntities persistentEntities,
144-
ObjectMapper mapper, SpringDocDataRestUtils springDocDataRestUtils) {
145-
return new SpringRepositoryRestResourceProvider(mappings, repositories, associations, applicationContext,
146-
dataRestRouterOperationService, persistentEntities, mapper, springDocDataRestUtils);
131+
SpringRepositoryRestResourceProvider springRepositoryRestResourceProvider(DataRestRouterOperationService dataRestRouterOperationService,
132+
ObjectMapper mapper,
133+
SpringDocDataRestUtils springDocDataRestUtils) {
134+
return new SpringRepositoryRestResourceProvider(
135+
dataRestRouterOperationService, mapper, springDocDataRestUtils);
147136
}
148137

149138
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/SpringRepositoryRestResourceProvider.java

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
import org.springdoc.core.fn.RouterOperation;
4747
import org.springdoc.core.utils.SpringDocDataRestUtils;
4848

49+
import org.springframework.beans.BeansException;
4950
import org.springframework.context.ApplicationContext;
51+
import org.springframework.context.ApplicationContextAware;
5052
import org.springframework.core.annotation.AnnotatedElementUtils;
5153
import org.springframework.core.annotation.AnnotationUtils;
5254
import org.springframework.data.mapping.PersistentEntity;
@@ -75,7 +77,7 @@
7577
*
7678
* @author bnasslahsen
7779
*/
78-
public class SpringRepositoryRestResourceProvider implements RepositoryRestResourceProvider {
80+
public class SpringRepositoryRestResourceProvider implements RepositoryRestResourceProvider, ApplicationContextAware {
7981

8082
/**
8183
* The constant SPRING_DATA_REST_PACKAGE.
@@ -136,31 +138,11 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
136138
}
137139
}
138140

139-
/**
140-
* The Mappings.
141-
*/
142-
private final ResourceMappings mappings;
143-
144-
/**
145-
* The Repositories.
146-
*/
147-
private final Repositories repositories;
148-
149-
/**
150-
* The Associations.
151-
*/
152-
private final Associations associations;
153-
154141
/**
155142
* The Data rest router operation builder.
156143
*/
157144
private final DataRestRouterOperationService dataRestRouterOperationService;
158145

159-
/**
160-
* The Persistent entities.
161-
*/
162-
private final PersistentEntities persistentEntities;
163-
164146
/**
165147
* The Mapper.
166148
*/
@@ -169,7 +151,7 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
169151
/**
170152
* The Application context.
171153
*/
172-
private final ApplicationContext applicationContext;
154+
private ApplicationContext applicationContext;
173155

174156
/**
175157
* The Spring doc data rest utils.
@@ -184,24 +166,13 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
184166
/**
185167
* Instantiates a new Spring repository rest resource provider.
186168
*
187-
* @param mappings the mappings
188-
* @param repositories the repositories
189-
* @param associations the associations
190-
* @param applicationContext the application context
191169
* @param dataRestRouterOperationService the data rest router operation builder
192-
* @param persistentEntities the persistent entities
193170
* @param mapper the mapper
194171
* @param springDocDataRestUtils the spring doc data rest utils
195172
*/
196-
public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositories repositories,
197-
Associations associations, ApplicationContext applicationContext, DataRestRouterOperationService dataRestRouterOperationService,
198-
PersistentEntities persistentEntities, ObjectMapper mapper, SpringDocDataRestUtils springDocDataRestUtils) {
199-
this.mappings = mappings;
200-
this.repositories = repositories;
201-
this.associations = associations;
202-
this.applicationContext = applicationContext;
173+
public SpringRepositoryRestResourceProvider(DataRestRouterOperationService dataRestRouterOperationService,
174+
ObjectMapper mapper, SpringDocDataRestUtils springDocDataRestUtils) {
203175
this.dataRestRouterOperationService = dataRestRouterOperationService;
204-
this.persistentEntities = persistentEntities;
205176
this.mapper = mapper;
206177
this.springDocDataRestUtils = springDocDataRestUtils;
207178
}
@@ -217,6 +188,10 @@ public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositor
217188
public List<RouterOperation> getRouterOperations(OpenAPI openAPI, Locale locale) {
218189
List<RouterOperation> routerOperationList = new ArrayList<>();
219190
handlerMappingList = getHandlerMappingList();
191+
Associations associations = applicationContext.getBean(Associations.class);
192+
ResourceMappings mappings = applicationContext.getBean(ResourceMappings.class);
193+
PersistentEntities persistentEntities = applicationContext.getBean(PersistentEntities.class);
194+
Repositories repositories = applicationContext.getBean(Repositories.class);
220195
for (Class<?> domainType : repositories) {
221196
Class<?> repository = repositories.getRequiredRepositoryInformation(domainType).getRepositoryInterface();
222197
DataRestRepository dataRestRepository = new DataRestRepository(domainType, repository, locale);
@@ -274,7 +249,7 @@ else if (handlerMapping instanceof BasePathAwareHandlerMapping beanBasePathAware
274249
}
275250
}
276251
// search
277-
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
252+
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata, associations);
278253
}
279254
}
280255
return routerOperationList;
@@ -312,6 +287,8 @@ public Map getHandlerMethods() {
312287
*/
313288
@Override
314289
public void customize(OpenAPI openAPI) {
290+
ResourceMappings mappings = applicationContext.getBean(ResourceMappings.class);
291+
PersistentEntities persistentEntities = applicationContext.getBean(PersistentEntities.class);
315292
springDocDataRestUtils.customise(openAPI, mappings, persistentEntities);
316293
}
317294

@@ -346,9 +323,10 @@ private List<HandlerMapping> getHandlerMappingList() {
346323
* @param handlerMappingList the handler mapping list
347324
* @param dataRestRepository the repository data rest
348325
* @param resourceMetadata the resource metadata
326+
* @param associations the associations
349327
*/
350328
private void findSearchResourceMappings(OpenAPI openAPI, List<RouterOperation> routerOperationList, List<HandlerMapping> handlerMappingList,
351-
DataRestRepository dataRestRepository, ResourceMetadata resourceMetadata) {
329+
DataRestRepository dataRestRepository, ResourceMetadata resourceMetadata, Associations associations) {
352330
for (HandlerMapping handlerMapping : handlerMappingList) {
353331
if (handlerMapping instanceof RepositoryRestHandlerMapping repositoryRestHandlerMapping) {
354332
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
@@ -406,4 +384,8 @@ private List<RouterOperation> findControllers(List<RouterOperation> routerOperat
406384
return routerOperationList;
407385
}
408386

387+
@Override
388+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
389+
this.applicationContext = applicationContext;
390+
}
409391
}

0 commit comments

Comments
 (0)