Skip to content

Commit b6396f8

Browse files
committed
improve compatibility with older spring/spring-boot versions
1 parent b0c1e2d commit b6396f8

File tree

4 files changed

+84
-27
lines changed

4 files changed

+84
-27
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
import org.slf4j.LoggerFactory;
5151
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
5252

53-
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
54-
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
55-
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties.Format;
5653
import org.springframework.core.MethodParameter;
5754
import org.springframework.core.ResolvableType;
5855
import org.springframework.core.io.Resource;
@@ -78,7 +75,7 @@ public class GenericParameterService {
7875
/**
7976
* The Web conversion service.
8077
*/
81-
private final WebConversionService webConversionService;
78+
private final Optional<WebConversionServiceProvider> optionalWebConversionServiceProvider;
8279

8380
/**
8481
* The constant LOGGER.
@@ -99,18 +96,12 @@ public class GenericParameterService {
9996
* Instantiates a new Generic parameter builder.
10097
* @param propertyResolverUtils the property resolver utils
10198
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
102-
* @param webConversionServiceOptional the web conversion service optional
99+
* @param optionalWebConversionServiceProvider
103100
*/
104-
public GenericParameterService(PropertyResolverUtils propertyResolverUtils, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer, Optional<WebConversionService> webConversionServiceOptional) {
101+
public GenericParameterService(PropertyResolverUtils propertyResolverUtils, Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer, Optional<WebConversionServiceProvider> optionalWebConversionServiceProvider) {
105102
this.propertyResolverUtils = propertyResolverUtils;
106103
this.optionalDelegatingMethodParameterCustomizer = optionalDelegatingMethodParameterCustomizer;
107-
if (webConversionServiceOptional.isPresent())
108-
this.webConversionService = webConversionServiceOptional.get();
109-
else {
110-
final Format format = new Format();
111-
this.webConversionService = new WebConversionService(new DateTimeFormatters()
112-
.dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
113-
}
104+
this.optionalWebConversionServiceProvider = optionalWebConversionServiceProvider;
114105
}
115106

116107
/**
@@ -491,13 +482,7 @@ public PropertyResolverUtils getPropertyResolverUtils() {
491482
return propertyResolverUtils;
492483
}
493484

494-
495-
/**
496-
* Gets web conversion service.
497-
*
498-
* @return the web conversion service
499-
*/
500-
public WebConversionService getWebConversionService() {
501-
return webConversionService;
485+
public Optional<WebConversionServiceProvider> getOptionalWebConversionServiceProvider() {
486+
return optionalWebConversionServiceProvider;
502487
}
503488
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2626
import org.apache.commons.lang3.StringUtils;
2727

28-
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
2928
import org.springframework.core.MethodParameter;
3029
import org.springframework.web.bind.annotation.CookieValue;
3130
import org.springframework.web.bind.annotation.PathVariable;
@@ -101,8 +100,8 @@ else if (cookieValue != null)
101100
this.pName = propertyResolverUtils.resolve(this.pName);
102101
if (this.defaultValue !=null && !ValueConstants.DEFAULT_NONE.equals(this.defaultValue.toString())){
103102
this.defaultValue = propertyResolverUtils.resolve(this.defaultValue.toString());
104-
WebConversionService conversionService = parameterBuilder.getWebConversionService();
105-
this.defaultValue= conversionService.convert(this.defaultValue, methodParameter.getParameterType());
103+
parameterBuilder.getOptionalWebConversionServiceProvider()
104+
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, methodParameter.getParameterType()));
106105
}
107106

108107
this.required = this.required && !methodParameter.isOptional();

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,16 @@ ReturnTypeParser genericReturnTypeParser() {
282282
*
283283
* @param propertyResolverUtils the property resolver utils
284284
* @param optionalDelegatingMethodParameterCustomizer the optional delegating method parameter customizer
285-
* @param webConversionServiceOptional the web conversion service
285+
* @param optionalWebConversionServiceProvider the optional web conversion service provider
286286
* @return the generic parameter builder
287287
*/
288288
@Bean
289289
@ConditionalOnMissingBean
290290
GenericParameterService parameterBuilder(PropertyResolverUtils propertyResolverUtils,
291291
Optional<DelegatingMethodParameterCustomizer> optionalDelegatingMethodParameterCustomizer,
292-
Optional<WebConversionService> webConversionServiceOptional) {
293-
return new GenericParameterService(propertyResolverUtils,optionalDelegatingMethodParameterCustomizer, webConversionServiceOptional);
292+
Optional<WebConversionServiceProvider> optionalWebConversionServiceProvider) {
293+
return new GenericParameterService(propertyResolverUtils, optionalDelegatingMethodParameterCustomizer,
294+
optionalWebConversionServiceProvider);
294295
}
295296

296297
/**
@@ -340,6 +341,7 @@ static BeanFactoryPostProcessor springdocBeanFactoryPostProcessor2() {
340341

341342
/**
342343
* The type Open api resource advice.
344+
* @author bnasslashen
343345
*/
344346
@RestControllerAdvice
345347
@Hidden
@@ -379,6 +381,7 @@ static class SpringDocActuatorConfiguration {
379381
static BeanFactoryPostProcessor springdocBeanFactoryPostProcessor3(List<GroupedOpenApi> groupedOpenApis) {
380382
return new SpringdocActuatorBeanFactoryConfigurer(groupedOpenApis);
381383
}
384+
382385
/**
383386
* Actuator customizer operation customizer.
384387
*
@@ -393,6 +396,7 @@ OperationCustomizer actuatorCustomizer() {
393396
/**
394397
* Actuator customizer OpenAPI customiser.
395398
*
399+
* @param webEndpointProperties the web endpoint properties
396400
* @return the OpenAPI customiser
397401
*/
398402
@Bean
@@ -402,4 +406,25 @@ OpenApiCustomiser actuatorOpenApiCustomiser(WebEndpointProperties webEndpointPro
402406
}
403407

404408
}
409+
410+
/**
411+
* The type Web conversion service configuration.
412+
* @author bnasslashen
413+
*/
414+
@ConditionalOnClass(WebConversionService.class)
415+
static class WebConversionServiceConfiguration {
416+
417+
/**
418+
* Web conversion service provider web conversion service provider.
419+
*
420+
* @param webConversionServiceOptional the web conversion service optional
421+
* @return the web conversion service provider
422+
*/
423+
@Bean
424+
@Lazy(false)
425+
WebConversionServiceProvider webConversionServiceProvider(Optional<WebConversionService> webConversionServiceOptional) {
426+
return new WebConversionServiceProvider(webConversionServiceOptional);
427+
}
428+
}
429+
405430
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.springdoc.core;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
6+
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
7+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties.Format;
8+
import org.springframework.lang.Nullable;
9+
10+
/**
11+
* The type Web conversion service provider.
12+
* @author bnasslashen
13+
*/
14+
public class WebConversionServiceProvider {
15+
16+
/**
17+
* The Web conversion service.
18+
*/
19+
private final WebConversionService webConversionService;
20+
21+
/**
22+
* Instantiates a new Web conversion service provider.
23+
*
24+
* @param webConversionServiceOptional the web conversion service optional
25+
*/
26+
public WebConversionServiceProvider(Optional<WebConversionService> webConversionServiceOptional) {
27+
if (webConversionServiceOptional.isPresent())
28+
this.webConversionService = webConversionServiceOptional.get();
29+
else {
30+
final Format format = new Format();
31+
this.webConversionService = new WebConversionService(new DateTimeFormatters()
32+
.dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
33+
}
34+
}
35+
36+
/**
37+
* Convert t.
38+
*
39+
* @param <T> the type parameter
40+
* @param source the source
41+
* @param targetType the target type
42+
* @return the t
43+
*/
44+
@Nullable
45+
public <T> T convert(@Nullable Object source, Class<T> targetType) {
46+
return webConversionService.convert(source, targetType);
47+
}
48+
}

0 commit comments

Comments
 (0)