Skip to content

Commit

Permalink
lazy initialization bug. Fixes #873.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Sep 21, 2020
1 parent 8c1e3d5 commit cb758b7
Show file tree
Hide file tree
Showing 29 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;

import static org.springdoc.core.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
Expand All @@ -35,6 +36,7 @@
* The type Spring doc config properties.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = Constants.SPRINGDOC_PREFIX)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
* The type Spring doc configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;

import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
Expand All @@ -44,6 +45,7 @@
* The type Swagger ui config parameters.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
@ConditionalOnBean(SpringDocConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;

Expand All @@ -32,6 +33,7 @@
* The type Swagger ui config properties.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "springdoc.swagger-ui")
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;

import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
Expand All @@ -39,6 +40,7 @@
* to get the idea what each parameter does.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "springdoc.swagger-ui.oauth")
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
* The type Spring doc data rest configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocDataRestConfiguration {
Expand Down Expand Up @@ -143,6 +144,7 @@ QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomize
* The type Spring repository rest resource provider configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RepositoryRestHandlerMapping.class)
static class SpringRepositoryRestResourceProviderConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# * limitations under the License.
# */
#
spring.main.banner-mode="off"
spring.main.banner-mode=off
logging.level.root=ERROR
logging.level.test.org.springdoc.api=ERROR
spring.main.lazy-initialization=true
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* The type Spring doc groovy configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocGroovyConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# */
#
spring.main.banner-mode:"off"
logging.level.root=OFF
logging.level.root=OFF
spring.main.lazy-initialization=true
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* The type Spring doc hateoas configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
@ConditionalOnClass(LinkRelationProvider.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@

package test.org.springdoc.api.app6;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.SimpleMixInResolver;
import com.fasterxml.jackson.databind.type.ClassKey;
import io.swagger.v3.core.util.Json;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -30,5 +43,33 @@ public class SpringDocApp6Test extends AbstractSpringDocTest {
static class SpringDocTestApp {
}

private Map<ClassKey, Class<?>> springMixins = new HashMap<>();

@BeforeEach
void init() throws IllegalAccessException {
Field convertersField2 = FieldUtils.getDeclaredField(ObjectMapper.class, "_mixIns", true);
SimpleMixInResolver _mixIns = (SimpleMixInResolver) convertersField2.get(Json.mapper());
Field convertersField3 = FieldUtils.getDeclaredField(SimpleMixInResolver.class, "_localMixIns", true);
Map<ClassKey, Class<?>> _localMixIns = (Map<ClassKey, Class<?>>) convertersField3.get(_mixIns);
Iterator<Entry<ClassKey, Class<?>>> it = _localMixIns.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<ClassKey, Class<?>> entry = it.next();
if (entry.getKey().toString().startsWith("org.springframework")) {
springMixins.put(entry.getKey(), entry.getValue());
it.remove();
}
}

}

@AfterEach
private void clean() throws IllegalAccessException {
Field convertersField2 = FieldUtils.getDeclaredField(ObjectMapper.class, "_mixIns", true);
SimpleMixInResolver _mixIns = (SimpleMixInResolver) convertersField2.get(Json.mapper());
Field convertersField3 = FieldUtils.getDeclaredField(SimpleMixInResolver.class, "_localMixIns", true);
Map<ClassKey, Class<?>> _localMixIns = (Map<ClassKey, Class<?>>) convertersField3.get(_mixIns);
_localMixIns.putAll(springMixins);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# * limitations under the License.
# */
#
spring.main.banner-mode="off"
spring.main.banner-mode=off
logging.level.root=ERROR
logging.level.test.org.springdoc.api=ERROR
spring.main.lazy-initialization=true
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* The type Spring doc kotlin configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocKotlinConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
import static org.springdoc.core.SpringDocUtils.getConfig;
Expand All @@ -33,6 +34,7 @@
* The type Spring doc kotlinx configuration.
* @author bnasslahsen
*/
@Lazy(false)
@ConditionalOnClass(Flow.class)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
spring:
main:
banner-mode: "off"
banner-mode: "off"
lazy-initialization: true
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
Expand All @@ -48,6 +49,7 @@
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
import static org.springdoc.core.Constants.SPRINGDOC_SHOW_LOGIN_ENDPOINT;
Expand All @@ -57,6 +59,7 @@
* The type Spring doc security configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocSecurityConfiguration {
Expand All @@ -71,6 +74,7 @@ public class SpringDocSecurityConfiguration {
* The type Spring security o auth 2 provider configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(FrameworkEndpointHandlerMapping.class)
class SpringSecurityOAuth2ProviderConfiguration {
Expand All @@ -87,14 +91,16 @@ SpringSecurityOAuth2Provider springSecurityOAuth2Provider(FrameworkEndpointHandl
}
}

@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(javax.servlet.Filter.class)
class SpringSecurityLoginEndpointConfiguration {

@Bean
@ConditionalOnProperty(SPRINGDOC_SHOW_LOGIN_ENDPOINT)
@Lazy(false)
OpenApiCustomiser springSecurityLoginEndpointCustomiser(FilterChainProxy filterChainProxy) {
OpenApiCustomiser springSecurityLoginEndpointCustomiser(ApplicationContext applicationContext) {
FilterChainProxy filterChainProxy = applicationContext.getBean(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, FilterChainProxy.class);
return openAPI -> {
for (SecurityFilterChain filterChain : filterChainProxy.getFilterChains()) {
Optional<UsernamePasswordAuthenticationFilter> optionalFilter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ static class SpringDocTestApp {
MyUserDetailsService userDetailsService() {
return new MyUserDetailsService();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
# * limitations under the License.
# */
#
spring.main.banner-mode:"off"
logging.level.root=OFF
spring.main.banner-mode=off
logging.level.root=OFF
spring.main.lazy-initialization=true
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;

Expand All @@ -40,6 +41,7 @@
* The type Swagger config.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
@ConditionalOnBean(SpringDocConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
spring:
main:
banner-mode: "off"
lazy-initialization: true
autoconfigure:
exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* The type Multiple open api web flux configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* The type Spring doc web flux configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
spring:
main:
banner-mode: "off"
lazy-initialization: true
logging:
level:
root: OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.reactive.config.WebFluxConfigurer;

import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
Expand All @@ -41,6 +42,7 @@
* The type Swagger config.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
@ConditionalOnBean(SpringDocConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.main.banner-mode=off
spring.main.lazy-initialization=true

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* The type Multiple open api support configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* The type Spring doc web mvc configuration.
* @author bnasslahsen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
# * limitations under the License.
# */
#
spring.main.banner-mode:"off"
logging.level.root=OFF
spring.main.banner-mode=off
logging.level.root=OFF
spring.main.lazy-initialization=true

0 comments on commit cb758b7

Please sign in to comment.