Skip to content

Commit 89bc6e8

Browse files
committed
springdoc-openapi-webflux-ui 2.0.0-M7 + spring actuator + spring cloud crashes at startup. Fixes #1904.
1 parent cf2908c commit 89bc6e8

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
import org.springframework.context.annotation.Configuration;
104104
import org.springframework.context.annotation.Lazy;
105105
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
106-
import org.springframework.core.convert.support.GenericConversionService;
107106
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
108107
import org.springframework.http.HttpStatus;
109108
import org.springframework.http.ResponseEntity;
@@ -502,13 +501,12 @@ static class WebConversionServiceConfiguration {
502501
/**
503502
* Web conversion service provider web conversion service provider.
504503
*
505-
* @param mvcConversionService the web conversion service optional
506504
* @return the web conversion service provider
507505
*/
508506
@Bean
509507
@Lazy(false)
510-
WebConversionServiceProvider webConversionServiceProvider(Optional<GenericConversionService> mvcConversionService) {
511-
return new WebConversionServiceProvider(mvcConversionService);
508+
WebConversionServiceProvider webConversionServiceProvider() {
509+
return new WebConversionServiceProvider();
512510
}
513511
}
514512

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/WebConversionServiceProvider.java

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,63 @@
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434

35+
import org.springframework.beans.BeansException;
36+
import org.springframework.beans.factory.InitializingBean;
37+
import org.springframework.context.ApplicationContext;
38+
import org.springframework.context.ApplicationContextAware;
3539
import org.springframework.core.convert.TypeDescriptor;
3640
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
3741
import org.springframework.core.convert.support.GenericConversionService;
3842
import org.springframework.format.support.DefaultFormattingConversionService;
43+
import org.springframework.format.support.FormattingConversionService;
3944
import org.springframework.lang.Nullable;
45+
import org.springframework.util.ClassUtils;
46+
4047

4148
/**
4249
* The type Web conversion service provider.
4350
* @author bnasslashen
4451
*/
45-
public class WebConversionServiceProvider {
52+
public class WebConversionServiceProvider implements InitializingBean, ApplicationContextAware {
4653

54+
/**
55+
* The constant CONVERTERS.
56+
*/
4757
private static final String CONVERTERS = "converters";
4858

4959
/**
5060
* The constant LOGGER.
5161
*/
5262
private static final Logger LOGGER = LoggerFactory.getLogger(WebConversionServiceProvider.class);
63+
5364
/**
5465
* The Formatting conversion service.
5566
*/
56-
private final GenericConversionService formattingConversionService;
67+
private GenericConversionService formattingConversionService;
5768

5869
/**
59-
* Instantiates a new Web conversion service provider.
60-
*
61-
* @param webConversionServiceOptional the web conversion service optional
70+
* The Application context.
71+
*/
72+
private ApplicationContext applicationContext;
73+
74+
/**
75+
* The constant SERVLET_APPLICATION_CONTEXT_CLASS.
76+
*/
77+
private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
78+
79+
/**
80+
* The constant REACTIVE_APPLICATION_CONTEXT_CLASS.
6281
*/
63-
public WebConversionServiceProvider(Optional<GenericConversionService> webConversionServiceOptional) {
64-
if (webConversionServiceOptional.isPresent())
65-
this.formattingConversionService = webConversionServiceOptional.get();
82+
private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";
83+
84+
@Override
85+
public void afterPropertiesSet() {
86+
if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) {
87+
this.formattingConversionService = applicationContext.getBean("mvcConversionService", FormattingConversionService.class);
88+
}
89+
else if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) {
90+
this.formattingConversionService = applicationContext.getBean("webFluxConversionService", FormattingConversionService.class);
91+
}
6692
else
6793
formattingConversionService = new DefaultFormattingConversionService();
6894
}
@@ -106,4 +132,25 @@ public Class<?> getSpringConvertedType(Class<?> clazz) {
106132
}
107133
return result;
108134
}
135+
136+
/**
137+
* Is assignable boolean.
138+
*
139+
* @param target the target
140+
* @param type the type
141+
* @return the boolean
142+
*/
143+
private boolean isAssignable(String target, Class<?> type) {
144+
try {
145+
return ClassUtils.resolveClassName(target, null).isAssignableFrom(type);
146+
}
147+
catch (Throwable ex) {
148+
return false;
149+
}
150+
}
151+
@Override
152+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
153+
this.applicationContext = applicationContext;
154+
}
155+
109156
}

0 commit comments

Comments
 (0)