|
2 | 2 | * |
3 | 3 | * * |
4 | 4 | * * * |
5 | | - * * * * Copyright 2019-2020 the original author or authors. |
| 5 | + * * * * Copyright 2019-2022 the original author or authors. |
6 | 6 | * * * * |
7 | 7 | * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * * * * you may not use this file except in compliance with the License. |
|
23 | 23 |
|
24 | 24 | package org.springdoc.core; |
25 | 25 |
|
26 | | -import java.util.ArrayList; |
27 | | -import java.util.List; |
28 | | - |
29 | 26 | import org.springdoc.core.customizers.ActuatorOpenApiCustomizer; |
30 | 27 | import org.springdoc.core.customizers.ActuatorOperationCustomizer; |
31 | | - |
| 28 | +import org.springframework.beans.BeansException; |
32 | 29 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 30 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 33 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
33 | 34 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; |
34 | 35 | import org.springframework.boot.context.properties.bind.BindResult; |
35 | 36 | import org.springframework.boot.context.properties.bind.Binder; |
36 | | -import org.springframework.util.CollectionUtils; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.ApplicationContextAware; |
| 39 | +import org.springframework.util.ObjectUtils; |
37 | 40 |
|
38 | 41 | import static org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP; |
39 | 42 | import static org.springdoc.core.Constants.ALL_PATTERN; |
|
44 | 47 | /** |
45 | 48 | * The type Springdoc bean factory configurer. |
46 | 49 | * @author bnasslahsen |
| 50 | + * @author christophejan |
47 | 51 | */ |
48 | | -public class SpringdocActuatorBeanFactoryConfigurer extends SpringdocBeanFactoryConfigurer{ |
| 52 | +public class SpringdocActuatorBeanFactoryConfigurer implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor { |
49 | 53 |
|
50 | 54 | /** |
51 | | - * The Grouped open apis. |
| 55 | + * The ApplicationContext. |
52 | 56 | */ |
53 | | - private List<GroupedOpenApi> groupedOpenApis; |
| 57 | + protected ApplicationContext applicationContext; |
54 | 58 |
|
55 | | - /** |
56 | | - * Instantiates a new Springdoc actuator bean factory configurer. |
57 | | - * |
58 | | - * @param groupedOpenApis the grouped open apis |
59 | | - */ |
60 | | - public SpringdocActuatorBeanFactoryConfigurer(List<GroupedOpenApi> groupedOpenApis) { |
61 | | - this.groupedOpenApis = groupedOpenApis; |
| 59 | + @Override |
| 60 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 61 | + this.applicationContext = applicationContext; |
62 | 62 | } |
63 | 63 |
|
64 | 64 | @Override |
65 | | - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
66 | | - final BindResult<WebEndpointProperties> result = Binder.get(environment) |
| 65 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { |
| 66 | + final BindResult<WebEndpointProperties> result = Binder.get(applicationContext.getEnvironment()) |
67 | 67 | .bind(MANAGEMENT_ENDPOINTS_WEB, WebEndpointProperties.class); |
68 | 68 | if (result.isBound()) { |
69 | 69 | WebEndpointProperties webEndpointProperties = result.get(); |
70 | 70 |
|
71 | | - List<GroupedOpenApi> newGroups = new ArrayList<>(); |
72 | | - |
73 | | - ActuatorOpenApiCustomizer actuatorOpenApiCustomiser = new ActuatorOpenApiCustomizer(webEndpointProperties); |
74 | | - beanFactory.registerSingleton("actuatorOpenApiCustomiser", actuatorOpenApiCustomiser); |
75 | | - ActuatorOperationCustomizer actuatorCustomizer = new ActuatorOperationCustomizer(); |
76 | | - beanFactory.registerSingleton("actuatorCustomizer", actuatorCustomizer); |
77 | | - |
78 | | - GroupedOpenApi actuatorGroup = GroupedOpenApi.builder().group(ACTUATOR_DEFAULT_GROUP) |
79 | | - .pathsToMatch(webEndpointProperties.getBasePath() + ALL_PATTERN) |
80 | | - .pathsToExclude(webEndpointProperties.getBasePath() + HEALTH_PATTERN) |
81 | | - .addOperationCustomizer(actuatorCustomizer) |
82 | | - .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
83 | | - .build(); |
84 | | - // Add the actuator group |
85 | | - newGroups.add(actuatorGroup); |
86 | | - |
87 | | - if (CollectionUtils.isEmpty(groupedOpenApis)) { |
88 | | - GroupedOpenApi defaultGroup = GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME) |
89 | | - .pathsToMatch(ALL_PATTERN) |
90 | | - .pathsToExclude(webEndpointProperties.getBasePath() + ALL_PATTERN) |
91 | | - .build(); |
92 | | - // Register the default group |
93 | | - newGroups.add(defaultGroup); |
94 | | - } |
| 71 | + boolean addDefaultGroup = ObjectUtils.isEmpty(applicationContext |
| 72 | + .getBeanNamesForType(GroupedOpenApi.class, true, false)); |
| 73 | + |
| 74 | + registry.registerBeanDefinition("actuatorOpenApiCustomiser", BeanDefinitionBuilder |
| 75 | + .genericBeanDefinition(ActuatorOpenApiCustomizer.class) |
| 76 | + .addConstructorArgValue(webEndpointProperties) |
| 77 | + .getBeanDefinition()); |
| 78 | + |
| 79 | + registry.registerBeanDefinition("actuatorCustomizer", BeanDefinitionBuilder |
| 80 | + .genericBeanDefinition(ActuatorOperationCustomizer.class) |
| 81 | + .getBeanDefinition()); |
95 | 82 |
|
96 | | - newGroups.forEach(elt -> beanFactory.registerSingleton(elt.getGroup(), elt)); |
| 83 | + // register the actuator group bean definition |
| 84 | + registry.registerBeanDefinition(ACTUATOR_DEFAULT_GROUP, BeanDefinitionBuilder |
| 85 | + .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
| 86 | + .setFactoryMethod("actuatorGroupFactoryMethod") |
| 87 | + .addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class)) |
| 88 | + .addConstructorArgValue(webEndpointProperties.getBasePath()) |
| 89 | + .addConstructorArgValue(new RuntimeBeanReference(ActuatorOpenApiCustomizer.class)) |
| 90 | + .addConstructorArgValue(new RuntimeBeanReference(ActuatorOperationCustomizer.class)) |
| 91 | + .getBeanDefinition()); |
| 92 | + |
| 93 | + if (addDefaultGroup) { |
| 94 | + // register the default group bean definition |
| 95 | + registry.registerBeanDefinition(DEFAULT_GROUP_NAME, BeanDefinitionBuilder |
| 96 | + .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
| 97 | + .setFactoryMethod("defaultGroupFactoryMethod") |
| 98 | + .addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class)) |
| 99 | + .addConstructorArgValue(webEndpointProperties.getBasePath()) |
| 100 | + .getBeanDefinition()); |
| 101 | + } |
97 | 102 | } |
98 | | - initBeanFactoryPostProcessor(beanFactory); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
| 107 | + SpringdocBeanFactoryConfigurer.initBeanFactoryPostProcessor(beanFactory); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Actuator {@link GroupedOpenApi} factory method. |
| 112 | + * |
| 113 | + * @param builder the {@link GroupedOpenApi.Builder} |
| 114 | + * @param actuatorBasePath the actuator base path |
| 115 | + * @param actuatorOpenApiCustomiser the {@link ActuatorOpenApiCustomizer} |
| 116 | + * @param actuatorOperationCustomizer the {@link ActuatorOperationCustomizer} |
| 117 | + * |
| 118 | + * @return the actuator {@link GroupedOpenApi} |
| 119 | + */ |
| 120 | + public static GroupedOpenApi actuatorGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath, |
| 121 | + ActuatorOpenApiCustomizer actuatorOpenApiCustomiser, ActuatorOperationCustomizer actuatorOperationCustomizer) { |
| 122 | + return builder.group(ACTUATOR_DEFAULT_GROUP) |
| 123 | + .pathsToMatch(actuatorBasePath + ALL_PATTERN) |
| 124 | + .pathsToExclude(actuatorBasePath + HEALTH_PATTERN) |
| 125 | + .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
| 126 | + .addOperationCustomizer(actuatorOperationCustomizer) |
| 127 | + .build(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Default {@link GroupedOpenApi} factory method. |
| 132 | + * |
| 133 | + * @param builder the {@link GroupedOpenApi.Builder} |
| 134 | + * @param actuatorBasePath the actuator base path |
| 135 | + * |
| 136 | + * @return the default {@link GroupedOpenApi} |
| 137 | + */ |
| 138 | + public static GroupedOpenApi defaultGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath) { |
| 139 | + return builder.group(DEFAULT_GROUP_NAME) |
| 140 | + .pathsToMatch(ALL_PATTERN) |
| 141 | + .pathsToExclude(actuatorBasePath + ALL_PATTERN) |
| 142 | + .build(); |
99 | 143 | } |
100 | 144 |
|
101 | 145 | } |
0 commit comments