io.elsci.springutils.SpringDocContext Maven / Gradle / Ivy
package io.elsci.springutils;
import org.springdoc.core.configuration.SpringDocRequiredModule;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties
public class SpringDocContext {
/**
* SpringBoot is horrible, but when combined with SpringDoc/Swagger, it seems to be at its worst:
* 1. This bean is simultaneously marked with @ConfigurationProperties and @Configuration. I don't know
* what these geniuses tried to achieve with this.
* 2. This causes the bean defined in XML to be used as a @Configuration, and therefore the bean itself gets
* deregistered and can't be injected into other SpringDoc classes. Hence, we need to declare it in Java
* configuration - for some reason this keeps the bean in the BeanFactory (why?!).
* 3. To make things worse the class is marked with @Conditional's - if we declare it as
* {@code @EnableConfigurationProperties} (so that it could be used as the source of properties), it still can
* act as @Configuration, but again - the bean gets deregistered.
* 4. This all makes it possible to register bean only if we put {@code @EnableConfigurationProperties} without
* declaring the class there, but at the same time define it explicitly as {@code @Bean} here.
*/
@Bean
public SpringDocConfigProperties springDocConfigProperties() {
return new SpringDocConfigProperties();
}
@Bean
public ObjectMapperProvider objectMapperProvider(SpringDocConfigProperties props) {
ObjectMapperProvider result = new ObjectMapperProvider(props);
result.jsonMapper().registerModule(new SpringDocRequiredModule());
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy