Skip to content

Commit 3c19d44

Browse files
committed
Changes report #1881.
1 parent 3593af0 commit 3c19d44

File tree

7 files changed

+208
-50
lines changed

7 files changed

+208
-50
lines changed

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

+55-50
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434

3535
import com.fasterxml.jackson.databind.ObjectMapper;
36+
import io.swagger.v3.oas.annotations.Hidden;
3637
import io.swagger.v3.oas.models.OpenAPI;
3738
import org.apache.commons.lang3.reflect.MethodUtils;
3839
import org.slf4j.Logger;
@@ -45,6 +46,7 @@
4546

4647
import org.springframework.context.ApplicationContext;
4748
import org.springframework.core.annotation.AnnotatedElementUtils;
49+
import org.springframework.core.annotation.AnnotationUtils;
4850
import org.springframework.data.mapping.PersistentEntity;
4951
import org.springframework.data.mapping.PersistentProperty;
5052
import org.springframework.data.mapping.SimpleAssociationHandler;
@@ -219,58 +221,60 @@ public List<RouterOperation> getRouterOperations(OpenAPI openAPI, Locale locale)
219221
final PersistentEntity<?, ?> entity = persistentEntities.getRequiredPersistentEntity(domainType);
220222
dataRestRepository.setPersistentEntity(entity);
221223
final JacksonMetadata jackson = new JacksonMetadata(mapper, domainType);
222-
223-
if (resourceMetadata.isExported()) {
224-
for (HandlerMapping handlerMapping : handlerMappingList) {
225-
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
226-
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
227-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
228-
// Entity controllers lookup first
229-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
230-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
231-
.getValue().getBeanType().getName()))
232-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
233-
dataRestRepository.setControllerType(ControllerType.ENTITY);
234-
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
235-
236-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
237-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
238-
.getValue().getBeanType().getName()))
239-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
240-
241-
entity.doWithAssociations((SimpleAssociationHandler) association -> {
242-
PersistentProperty<?> property = association.getInverse();
243-
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
244-
dataRestRepository.setRelationName(resourceMetadata.getMappingFor(property).getRel().value());
245-
dataRestRepository.setControllerType(ControllerType.PROPERTY);
246-
dataRestRepository.setCollectionLike(property.isCollectionLike());
247-
dataRestRepository.setMap(property.isMap());
248-
dataRestRepository.setPropertyType(property.getActualType());
249-
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
250-
}
251-
});
252-
}
253-
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
254-
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
255-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
256-
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
257-
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
258-
.getValue().getBeanType().getName()))
259-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
260-
dataRestRepository.setControllerType(ControllerType.SCHEMA);
261-
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
262-
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
263-
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
264-
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
265-
.getValue().getBeanType()))
266-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
267-
dataRestRepository.setControllerType(ControllerType.GENERAL);
268-
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+
}
269273
}
270274
}
275+
// search
276+
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
271277
}
272-
// search
273-
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
274278
}
275279
return routerOperationList;
276280
}
@@ -323,7 +327,8 @@ private List<HandlerMapping> getHandlerMappingList() {
323327
try {
324328
handlerMappingList = (List<HandlerMapping>) MethodUtils.invokeMethod(object, "getDelegates");
325329
}
326-
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
330+
catch (NoSuchMethodException | IllegalAccessException |
331+
InvocationTargetException e) {
327332
LOGGER.warn(e.getMessage());
328333
}
329334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package test.org.springdoc.api.app35;
2+
3+
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import lombok.Data;
9+
10+
@Entity
11+
public @Data
12+
class ChildProperty {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private long id;
17+
18+
private String name;
19+
20+
public long getId() {
21+
return id;
22+
}
23+
24+
public void setId(long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import io.swagger.v3.oas.annotations.Hidden;
4+
5+
import org.springframework.data.repository.PagingAndSortingRepository;
6+
7+
@Hidden
8+
public interface ChildPropertyRepository extends PagingAndSortingRepository<ChildProperty, Long> {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package test.org.springdoc.api.app35;
2+
3+
4+
5+
import jakarta.persistence.Entity;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.GenerationType;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.JoinColumn;
10+
import jakarta.persistence.ManyToOne;
11+
import lombok.Data;
12+
13+
@Entity
14+
public @Data
15+
class Property {
16+
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.AUTO)
19+
private long id;
20+
21+
private String name;
22+
23+
@ManyToOne
24+
@JoinColumn(name = "child_property_id")
25+
private ChildProperty myChildPropertyName;
26+
27+
public long getId() {
28+
return id;
29+
}
30+
31+
public void setId(long id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public ChildProperty getMyChildPropertyName() {
44+
return myChildPropertyName;
45+
}
46+
47+
public void setMyChildPropertyName(ChildProperty myChildPropertyName) {
48+
this.myChildPropertyName = myChildPropertyName;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test.org.springdoc.api.app35;
2+
3+
import io.swagger.v3.oas.annotations.Hidden;
4+
5+
import org.springframework.data.repository.PagingAndSortingRepository;
6+
7+
@Hidden
8+
public interface PropertyRepository extends PagingAndSortingRepository<Property, Long> {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.app35;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp35Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {},
14+
"components": {
15+
"schemas": {}
16+
}
17+
}

0 commit comments

Comments
 (0)