Skip to content

Commit

Permalink
[#1287] fixed swagger scan @scope("request") annotation controller pr…
Browse files Browse the repository at this point in the history
…oblem (#1297)
  • Loading branch information
chengyouling authored Jul 8, 2024
1 parent f110041 commit 4db9425
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved.
* 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
*
* http://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 com.huaweicloud.sample;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Scope("request")
public class ScopeRequestController {
@GetMapping("testScopeRequest")
public String testScopeRequest() {
return "Hello test scope request";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.huaweicloud.swagger;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -33,8 +34,11 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.ScopeNotActiveException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -59,7 +63,7 @@ public class ServiceCombSwaggerHandlerImpl implements ServiceCombSwaggerHandler,

private ApplicationContext applicationContext;

@Value("${spring.cloud.servicecomb.swagger.enableJavaChassisAdapter:true}")
@Value("${spring.cloud.servicecomb.swagger.enableJavaChassisAdapter:false}")
protected boolean withJavaChassis;

private OpenApiResourceWrapper openApiResource;
Expand Down Expand Up @@ -96,7 +100,7 @@ private void runSpringDocScanner() {
}

private void runJavaChassisScanner() {
Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(RestController.class);
Map<String, Object> controllers = getControllers();
controllers.forEach((k, v) -> {
try {
SpringmvcSwaggerGenerator generator = new SpringmvcSwaggerGenerator(v.getClass());
Expand All @@ -111,6 +115,26 @@ private void runJavaChassisScanner() {
swaggerSummary = calcSchemaSummary();
}

private Map<String, Object> getControllers() {
Map<String, Object> mappingsMap = new HashMap<>();
buildControllerMapping(mappingsMap, RequestMapping.class);
buildControllerMapping(mappingsMap, RestController.class);
buildControllerMapping(mappingsMap, Controller.class);
return mappingsMap;
}

private void buildControllerMapping(Map<String, Object> mappingsMap, Class<? extends Annotation> clazz) {
final String[] restControllerNames = applicationContext.getBeanNamesForAnnotation(clazz);
for (String beanName : restControllerNames) {
try {
Object beanInstance = applicationContext.getBean(beanName);
mappingsMap.put(beanName, beanInstance);
} catch (ScopeNotActiveException e) {
LOGGER.warn("bean [{}] is not active in current scope, ignore it.", beanName);
}
}
}

private Map<String, String> calcSchemaContent() {
return swaggerMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> {
try {
Expand Down

0 comments on commit 4db9425

Please sign in to comment.