Skip to content

Commit 7d4b5f5

Browse files
committed
Add global customizers and filters
1 parent f140ed9 commit 7d4b5f5

File tree

21 files changed

+1370
-12
lines changed

21 files changed

+1370
-12
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@
4949
import org.springdoc.core.customizers.ActuatorOperationCustomizer;
5050
import org.springdoc.core.customizers.DataRestDelegatingMethodParameterCustomizer;
5151
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
52+
import org.springdoc.core.customizers.GlobalOpenApiCustomiser;
53+
import org.springdoc.core.customizers.GlobalOperationCustomizer;
5254
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
5355
import org.springdoc.core.customizers.OpenApiCustomiser;
5456
import org.springdoc.core.customizers.PropertyCustomizer;
5557
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
58+
import org.springdoc.core.filters.GlobalOpenApiMethodFilter;
5659
import org.springdoc.core.providers.ActuatorProvider;
5760
import org.springdoc.core.providers.CloudFunctionProvider;
5861
import org.springdoc.core.providers.JavadocProvider;
@@ -84,6 +87,7 @@
8487
import org.springframework.context.annotation.Conditional;
8588
import org.springframework.context.annotation.Configuration;
8689
import org.springframework.context.annotation.Lazy;
90+
import org.springframework.context.annotation.Scope;
8791
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
8892
import org.springframework.core.convert.support.GenericConversionService;
8993
import org.springframework.data.domain.Pageable;
@@ -397,6 +401,18 @@ SpringDocProviders springDocProviders(Optional<ActuatorProvider> actuatorProvide
397401
return new SpringDocProviders(actuatorProvider, springCloudFunctionProvider, springSecurityOAuth2Provider, repositoryRestResourceProvider, routerFunctionProvider, springWebProvider);
398402
}
399403

404+
@Bean
405+
@Scope("prototype")
406+
@ConditionalOnMissingBean
407+
public GroupedOpenApi.Builder groupedOpenApiBuilder(List<GlobalOpenApiCustomiser> globalOpenApiCustomisers, List<GlobalOperationCustomizer> globalOperationCustomizers,
408+
List<GlobalOpenApiMethodFilter> globalOpenApiMethodFilters) {
409+
GroupedOpenApi.Builder builder = GroupedOpenApi.builder();
410+
globalOpenApiCustomisers.forEach(builder::addOpenApiCustomiser);
411+
globalOperationCustomizers.forEach(builder::addOperationCustomizer);
412+
globalOpenApiMethodFilters.forEach(builder::addOpenApiMethodFilter);
413+
return builder;
414+
}
415+
400416
/**
401417
* The type Open api resource advice.
402418
* @author bnasslashen

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringdocActuatorBeanFactoryConfigurer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
8484
registry.registerBeanDefinition(ACTUATOR_DEFAULT_GROUP, BeanDefinitionBuilder
8585
.genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class)
8686
.setFactoryMethod("actuatorGroupFactoryMethod")
87+
.addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class))
8788
.addConstructorArgValue(webEndpointProperties.getBasePath())
8889
.addConstructorArgValue(new RuntimeBeanReference(ActuatorOpenApiCustomizer.class))
8990
.addConstructorArgValue(new RuntimeBeanReference(ActuatorOperationCustomizer.class))
@@ -94,6 +95,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
9495
registry.registerBeanDefinition(DEFAULT_GROUP_NAME, BeanDefinitionBuilder
9596
.genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class)
9697
.setFactoryMethod("defaultGroupFactoryMethod")
98+
.addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class))
9799
.addConstructorArgValue(webEndpointProperties.getBasePath())
98100
.getBeanDefinition());
99101
}
@@ -108,15 +110,16 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
108110
/**
109111
* Actuator {@link GroupedOpenApi} factory method.
110112
*
113+
* @param builder the {@link GroupedOpenApi.Builder}
111114
* @param actuatorBasePath the actuator base path
112115
* @param actuatorOpenApiCustomiser the {@link ActuatorOpenApiCustomizer}
113116
* @param actuatorOperationCustomizer the {@link ActuatorOperationCustomizer}
114117
*
115118
* @return the actuator {@link GroupedOpenApi}
116119
*/
117-
public static GroupedOpenApi actuatorGroupFactoryMethod(String actuatorBasePath,
120+
public static GroupedOpenApi actuatorGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath,
118121
ActuatorOpenApiCustomizer actuatorOpenApiCustomiser, ActuatorOperationCustomizer actuatorOperationCustomizer) {
119-
return GroupedOpenApi.builder().group(ACTUATOR_DEFAULT_GROUP)
122+
return builder.group(ACTUATOR_DEFAULT_GROUP)
120123
.pathsToMatch(actuatorBasePath + ALL_PATTERN)
121124
.pathsToExclude(actuatorBasePath + HEALTH_PATTERN)
122125
.addOpenApiCustomiser(actuatorOpenApiCustomiser)
@@ -127,12 +130,13 @@ public static GroupedOpenApi actuatorGroupFactoryMethod(String actuatorBasePath,
127130
/**
128131
* Default {@link GroupedOpenApi} factory method.
129132
*
133+
* @param builder the {@link GroupedOpenApi.Builder}
130134
* @param actuatorBasePath the actuator base path
131135
*
132136
* @return the default {@link GroupedOpenApi}
133137
*/
134-
public static GroupedOpenApi defaultGroupFactoryMethod(String actuatorBasePath) {
135-
return GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME)
138+
public static GroupedOpenApi defaultGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath) {
139+
return builder.group(DEFAULT_GROUP_NAME)
136140
.pathsToMatch(ALL_PATTERN)
137141
.pathsToExclude(actuatorBasePath + ALL_PATTERN)
138142
.build();

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringdocBeanFactoryConfigurer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springdoc.core.SpringDocConfigProperties.GroupConfig;
2929
import org.springframework.beans.BeansException;
3030
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31+
import org.springframework.beans.factory.config.RuntimeBeanReference;
3132
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3233
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3334
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
@@ -67,6 +68,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
6768
registry.registerBeanDefinition(groupConfig.getGroup(), BeanDefinitionBuilder
6869
.genericBeanDefinition(SpringdocBeanFactoryConfigurer.class)
6970
.setFactoryMethod("groupedOpenApisFactoryMethod")
71+
.addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class))
7072
.addConstructorArgValue(groupConfig)
7173
.getBeanDefinition())
7274
);
@@ -81,12 +83,12 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
8183
/**
8284
* {@link GroupedOpenApi} factory method from {@link GroupConfig}.
8385
*
86+
* @param builder the {@link GroupedOpenApi.Builder}
8487
* @param groupConfig the {@link GroupConfig}
8588
*
8689
* @return the {@link GroupedOpenApi}
8790
*/
88-
public static GroupedOpenApi groupedOpenApisFactoryMethod(GroupConfig groupConfig) {
89-
GroupedOpenApi.Builder builder = GroupedOpenApi.builder();
91+
public static GroupedOpenApi groupedOpenApisFactoryMethod(GroupedOpenApi.Builder builder, GroupConfig groupConfig) {
9092
if (!CollectionUtils.isEmpty(groupConfig.getPackagesToScan()))
9193
builder.packagesToScan(groupConfig.getPackagesToScan().toArray(new String[0]));
9294
if (!CollectionUtils.isEmpty(groupConfig.getPathsToMatch()))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2022 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.core.customizers;
22+
23+
/**
24+
* Implement and register a bean of type {@link GlobalOpenApiCustomiser} to
25+
* customize Open api on default OpenAPI description and groups.
26+
*
27+
* @author christophejan
28+
* @see OpenApiCustomiser to customize default OpenAPI description but not
29+
* groups
30+
*/
31+
public interface GlobalOpenApiCustomiser extends OpenApiCustomiser {
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2022 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.core.customizers;
22+
23+
/**
24+
* Implement and register a bean of type {@link GlobalOperationCustomizer} to
25+
* customize an operation based on the handler method input on default OpenAPI
26+
* description and groups
27+
*
28+
* @author christophejan
29+
* @see OperationCustomizer to customize operations on default OpenAPI
30+
* description but not groups
31+
*/
32+
public interface GlobalOperationCustomizer extends OperationCustomizer {
33+
}

springdoc-openapi-common/src/main/java/org/springdoc/core/customizers/OpenApiCustomiser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
33
* *
4-
* * * Copyright 2019-2020 the original author or authors.
4+
* * * Copyright 2019-2022 the original author or authors.
55
* * *
66
* * * Licensed under the Apache License, Version 2.0 (the "License");
77
* * * you may not use this file except in compliance with the License.
@@ -23,8 +23,12 @@
2323
import io.swagger.v3.oas.models.OpenAPI;
2424

2525
/**
26-
* The interface Open api customiser.
26+
* Implement and register a bean of type {@link OpenApiCustomiser} to customize
27+
* Open api on default OpenAPI description but not on groups
28+
*
2729
* @author bnasslahsen
30+
* @see GlobalOpenApiCustomiser to customize default OpenAPI description and
31+
* groups
2832
*/
2933
@FunctionalInterface
3034
public interface OpenApiCustomiser {

springdoc-openapi-common/src/main/java/org/springdoc/core/customizers/OperationCustomizer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
33
* *
4-
* * * Copyright 2019-2020 the original author or authors.
4+
* * * Copyright 2019-2022 the original author or authors.
55
* * *
66
* * * Licensed under the Apache License, Version 2.0 (the "License");
77
* * * you may not use this file except in compliance with the License.
@@ -26,8 +26,12 @@
2626

2727
/**
2828
* Implement and register a bean of type {@link OperationCustomizer} to customize an operation
29-
* based on the handler method input
29+
* based on the handler method input on default OpenAPI descriptions but not
30+
* groups
31+
*
3032
* @author bnasslahsen
33+
* @see GlobalOperationCustomizer to customize operations on default OpenAPI
34+
* description and groups
3135
*/
3236
@FunctionalInterface
3337
public interface OperationCustomizer {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2022 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.core.filters;
22+
23+
/**
24+
* Implement and register a bean of type {@link GlobalOpenApiMethodFilter} to
25+
* conditionally including any detected methods in default OpenAPI description
26+
* and groups.
27+
*
28+
* @author michael.clarke
29+
* @see OpenApiMethodFilter to filter methods in default OpenAPI description but
30+
* not groups
31+
*/
32+
@FunctionalInterface
33+
public interface GlobalOpenApiMethodFilter extends OpenApiMethodFilter {
34+
}

springdoc-openapi-common/src/main/java/org/springdoc/core/filters/OpenApiMethodFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
33
* *
4-
* * * Copyright 2019-2020 the original author or authors.
4+
* * * Copyright 2019-2022 the original author or authors.
55
* * *
66
* * * Licensed under the Apache License, Version 2.0 (the "License");
77
* * * you may not use this file except in compliance with the License.
@@ -23,8 +23,13 @@
2323
import java.lang.reflect.Method;
2424

2525
/**
26-
* A filter to allow conditionally including any detected methods in an OpenApi definition.
26+
* Implement and register a bean of type {@link OpenApiMethodFilter} to
27+
* conditionally include any detected methods in default OpenAPI descriptions
28+
* but not groups
29+
*
2730
* @author michael.clarke
31+
* @see GlobalOpenApiMethodFilter to filter methods in default OpenAPI
32+
* description and groups
2833
*/
2934
@FunctionalInterface
3035
public interface OpenApiMethodFilter {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* * Copyright 2019-2022 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app184;
20+
21+
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
@RestController
26+
public class HelloController {
27+
28+
@GetMapping("/globalBeanFiltered")
29+
public String globalBeanFiltered() {
30+
return "globalBeanFiltered";
31+
}
32+
33+
@GetMapping("/beanFiltered")
34+
public String beanFiltered() {
35+
return "beanFiltered";
36+
}
37+
38+
@GetMapping("/group1Filtered")
39+
public String group1Filtered() {
40+
return "group1Filtered";
41+
}
42+
43+
@GetMapping("/group2Filtered")
44+
public String group2Filtered() {
45+
return "group2Filtered";
46+
}
47+
48+
@GetMapping("/group3Filtered")
49+
public String group3Filtered() {
50+
return "group3Filtered";
51+
}
52+
53+
}

0 commit comments

Comments
 (0)