Skip to content

Commit 277b3c7

Browse files
committed
Support @hidden annotation on REST repositories. Fixes #1881
1 parent 12a047f commit 277b3c7

File tree

2 files changed

+832
-48
lines changed

2 files changed

+832
-48
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/SpringRepositoryRestResourceProvider.java

+53-48
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.stream.Stream;
3232

3333
import com.fasterxml.jackson.databind.ObjectMapper;
34+
import io.swagger.v3.oas.annotations.Hidden;
3435
import io.swagger.v3.oas.models.OpenAPI;
3536
import org.apache.commons.lang3.reflect.MethodUtils;
3637
import org.slf4j.Logger;
@@ -44,6 +45,7 @@
4445

4546
import org.springframework.context.ApplicationContext;
4647
import org.springframework.core.annotation.AnnotatedElementUtils;
48+
import org.springframework.core.annotation.AnnotationUtils;
4749
import org.springframework.data.mapping.PersistentEntity;
4850
import org.springframework.data.mapping.PersistentProperty;
4951
import org.springframework.data.mapping.SimpleAssociationHandler;
@@ -219,57 +221,60 @@ public List<RouterOperation> getRouterOperations(OpenAPI openAPI, Locale locale)
219221
dataRestRepository.setPersistentEntity(entity);
220222
final JacksonMetadata jackson = new JacksonMetadata(mapper, domainType);
221223

222-
if (resourceMetadata.isExported()) {
223-
for (HandlerMapping handlerMapping : handlerMappingList) {
224-
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
225-
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
226-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
227-
// Entity controllers lookup first
228-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
229-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
230-
.getValue().getBeanType().getName()))
231-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
232-
dataRestRepository.setControllerType(ControllerType.ENTITY);
233-
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
234-
235-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
236-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
237-
.getValue().getBeanType().getName()))
238-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
239-
240-
entity.doWithAssociations((SimpleAssociationHandler) association -> {
241-
PersistentProperty<?> property = association.getInverse();
242-
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
243-
dataRestRepository.setRelationName(resourceMetadata.getMappingFor(property).getRel().value());
244-
dataRestRepository.setControllerType(ControllerType.PROPERTY);
245-
dataRestRepository.setCollectionLike(property.isCollectionLike());
246-
dataRestRepository.setMap(property.isMap());
247-
dataRestRepository.setPropertyType(property.getActualType());
248-
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
249-
}
250-
});
251-
}
252-
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
253-
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
254-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
255-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
256-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
257-
.getValue().getBeanType().getName()))
258-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
259-
dataRestRepository.setControllerType(ControllerType.SCHEMA);
260-
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
261-
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
262-
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
263-
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
264-
.getValue().getBeanType()))
265-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
266-
dataRestRepository.setControllerType(ControllerType.GENERAL);
267-
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
224+
boolean hiddenRepository = (AnnotationUtils.findAnnotation(repository, Hidden.class) != null);
225+
if(!hiddenRepository) {
226+
if (resourceMetadata.isExported()) {
227+
for (HandlerMapping handlerMapping : handlerMappingList) {
228+
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
229+
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
230+
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
231+
// Entity controllers lookup first
232+
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
233+
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
234+
.getValue().getBeanType().getName()))
235+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
236+
dataRestRepository.setControllerType(ControllerType.ENTITY);
237+
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
238+
239+
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
240+
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
241+
.getValue().getBeanType().getName()))
242+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
243+
244+
entity.doWithAssociations((SimpleAssociationHandler) association -> {
245+
PersistentProperty<?> property = association.getInverse();
246+
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
247+
dataRestRepository.setRelationName(resourceMetadata.getMappingFor(property).getRel().value());
248+
dataRestRepository.setControllerType(ControllerType.PROPERTY);
249+
dataRestRepository.setCollectionLike(property.isCollectionLike());
250+
dataRestRepository.setMap(property.isMap());
251+
dataRestRepository.setPropertyType(property.getActualType());
252+
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
253+
}
254+
});
255+
}
256+
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
257+
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
258+
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
259+
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
260+
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
261+
.getValue().getBeanType().getName()))
262+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
263+
dataRestRepository.setControllerType(ControllerType.SCHEMA);
264+
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
265+
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
266+
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
267+
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
268+
.getValue().getBeanType()))
269+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
270+
dataRestRepository.setControllerType(ControllerType.GENERAL);
271+
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
272+
}
268273
}
269274
}
275+
// search
276+
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
270277
}
271-
// search
272-
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
273278
}
274279
return routerOperationList;
275280
}

0 commit comments

Comments
 (0)