Skip to content

Commit

Permalink
Changes report #1881.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Oct 14, 2022
1 parent 3593af0 commit 3c19d44
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 50 deletions.
Expand Up @@ -33,6 +33,7 @@
import java.util.stream.Stream;

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

import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
Expand Down Expand Up @@ -219,58 +221,60 @@ public List<RouterOperation> getRouterOperations(OpenAPI openAPI, Locale locale)
final PersistentEntity<?, ?> entity = persistentEntities.getRequiredPersistentEntity(domainType);
dataRestRepository.setPersistentEntity(entity);
final JacksonMetadata jackson = new JacksonMetadata(mapper, domainType);

if (resourceMetadata.isExported()) {
for (HandlerMapping handlerMapping : handlerMappingList) {
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
// Entity controllers lookup first
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.ENTITY);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);

Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));

entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
dataRestRepository.setRelationName(resourceMetadata.getMappingFor(property).getRel().value());
dataRestRepository.setControllerType(ControllerType.PROPERTY);
dataRestRepository.setCollectionLike(property.isCollectionLike());
dataRestRepository.setMap(property.isMap());
dataRestRepository.setPropertyType(property.getActualType());
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
}
});
}
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.SCHEMA);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.GENERAL);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
boolean hiddenRepository = (AnnotationUtils.findAnnotation(repository, Hidden.class) != null);
if (!hiddenRepository) {
if (resourceMetadata.isExported()) {
for (HandlerMapping handlerMapping : handlerMappingList) {
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
// Entity controllers lookup first
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.ENTITY);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);

Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));

entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
dataRestRepository.setRelationName(resourceMetadata.getMappingFor(property).getRel().value());
dataRestRepository.setControllerType(ControllerType.PROPERTY);
dataRestRepository.setCollectionLike(property.isCollectionLike());
dataRestRepository.setMap(property.isMap());
dataRestRepository.setPropertyType(property.getActualType());
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
}
});
}
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.SCHEMA);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.GENERAL);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
}
}
}
// search
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
}
// search
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
}
return routerOperationList;
}
Expand Down Expand Up @@ -323,7 +327,8 @@ private List<HandlerMapping> getHandlerMappingList() {
try {
handlerMappingList = (List<HandlerMapping>) MethodUtils.invokeMethod(object, "getDelegates");
}
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
LOGGER.warn(e.getMessage());
}
}
Expand Down
@@ -0,0 +1,35 @@
package test.org.springdoc.api.app35;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

@Entity
public @Data
class ChildProperty {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String name;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,9 @@
package test.org.springdoc.api.app35;

import io.swagger.v3.oas.annotations.Hidden;

import org.springframework.data.repository.PagingAndSortingRepository;

@Hidden
public interface ChildPropertyRepository extends PagingAndSortingRepository<ChildProperty, Long> {
}
@@ -0,0 +1,50 @@
package test.org.springdoc.api.app35;



import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Data;

@Entity
public @Data
class Property {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String name;

@ManyToOne
@JoinColumn(name = "child_property_id")
private ChildProperty myChildPropertyName;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public ChildProperty getMyChildPropertyName() {
return myChildPropertyName;
}

public void setMyChildPropertyName(ChildProperty myChildPropertyName) {
this.myChildPropertyName = myChildPropertyName;
}
}
@@ -0,0 +1,9 @@
package test.org.springdoc.api.app35;

import io.swagger.v3.oas.annotations.Hidden;

import org.springframework.data.repository.PagingAndSortingRepository;

@Hidden
public interface PropertyRepository extends PagingAndSortingRepository<Property, Long> {
}
@@ -0,0 +1,33 @@
/*
*
* *
* * *
* * * * Copyright 2019-2020 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package test.org.springdoc.api.app35;

import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp35Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {}
}
@@ -0,0 +1,17 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {},
"components": {
"schemas": {}
}
}

0 comments on commit 3c19d44

Please sign in to comment.