4646import org .springdoc .core .fn .RouterOperation ;
4747import org .springdoc .core .utils .SpringDocDataRestUtils ;
4848
49+ import org .springframework .beans .BeansException ;
4950import org .springframework .context .ApplicationContext ;
51+ import org .springframework .context .ApplicationContextAware ;
5052import org .springframework .core .annotation .AnnotatedElementUtils ;
5153import org .springframework .core .annotation .AnnotationUtils ;
5254import org .springframework .data .mapping .PersistentEntity ;
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