Skip to content

Commit d71f0b4

Browse files
committed
Merge pull request #44290 from nosan
* pr/44290: Polish "Auto-configure ObjectDirectoryMapper" Auto-configure ObjectDirectoryMapper Closes gh-44290
2 parents ca1bd3f + 3ed0e02 commit d71f0b4

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,13 +26,17 @@
2626
import org.springframework.boot.autoconfigure.ldap.LdapProperties.Template;
2727
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2828
import org.springframework.boot.context.properties.PropertyMapper;
29+
import org.springframework.boot.convert.ApplicationConversionService;
2930
import org.springframework.context.annotation.Bean;
3031
import org.springframework.core.env.Environment;
32+
import org.springframework.ldap.convert.ConverterUtils;
3133
import org.springframework.ldap.core.ContextSource;
3234
import org.springframework.ldap.core.LdapOperations;
3335
import org.springframework.ldap.core.LdapTemplate;
3436
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
3537
import org.springframework.ldap.core.support.LdapContextSource;
38+
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
39+
import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper;
3640

3741
/**
3842
* {@link EnableAutoConfiguration Auto-configuration} for LDAP.
@@ -70,12 +74,24 @@ public LdapContextSource ldapContextSource(LdapConnectionDetails connectionDetai
7074
return source;
7175
}
7276

77+
@Bean
78+
@ConditionalOnMissingBean
79+
public ObjectDirectoryMapper objectDirectoryMapper() {
80+
ApplicationConversionService conversionService = new ApplicationConversionService();
81+
ConverterUtils.addDefaultConverters(conversionService);
82+
DefaultObjectDirectoryMapper objectDirectoryMapper = new DefaultObjectDirectoryMapper();
83+
objectDirectoryMapper.setConversionService(conversionService);
84+
return objectDirectoryMapper;
85+
}
86+
7387
@Bean
7488
@ConditionalOnMissingBean(LdapOperations.class)
75-
public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource) {
89+
public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource,
90+
ObjectDirectoryMapper objectDirectoryMapper) {
7691
Template template = properties.getTemplate();
7792
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
7893
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
94+
ldapTemplate.setObjectDirectoryMapper(objectDirectoryMapper);
7995
propertyMapper.from(template.isIgnorePartialResultException())
8096
.to(ldapTemplate::setIgnorePartialResultException);
8197
propertyMapper.from(template.isIgnoreNameNotFoundException()).to(ldapTemplate::setIgnoreNameNotFoundException);

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
1616

1717
package org.springframework.boot.autoconfigure.ldap;
1818

19+
import javax.naming.Name;
20+
21+
import org.assertj.core.api.InstanceOfAssertFactories;
1922
import org.junit.jupiter.api.Test;
2023

2124
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.convert.ApplicationConversionService;
2226
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2327
import org.springframework.context.annotation.Bean;
2428
import org.springframework.context.annotation.Configuration;
@@ -27,6 +31,7 @@
2731
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
2832
import org.springframework.ldap.core.support.LdapContextSource;
2933
import org.springframework.ldap.core.support.SimpleDirContextAuthenticationStrategy;
34+
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
3035
import org.springframework.ldap.pool2.factory.PoolConfig;
3136
import org.springframework.ldap.pool2.factory.PooledContextSource;
3237
import org.springframework.ldap.support.LdapUtils;
@@ -132,6 +137,21 @@ void usesCustomConnectionDetailsWhenDefined() {
132137
});
133138
}
134139

140+
@Test
141+
void objectDirectoryMapperExists() {
142+
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389").run((context) -> {
143+
assertThat(context).hasSingleBean(ObjectDirectoryMapper.class);
144+
ObjectDirectoryMapper objectDirectoryMapper = context.getBean(ObjectDirectoryMapper.class);
145+
ApplicationConversionService conversionService = assertThat(objectDirectoryMapper)
146+
.extracting("converterManager")
147+
.extracting("conversionService")
148+
.asInstanceOf(InstanceOfAssertFactories.type(ApplicationConversionService.class))
149+
.actual();
150+
assertThat(conversionService.canConvert(String.class, Name.class)).isTrue();
151+
assertThat(conversionService.canConvert(Name.class, String.class)).isTrue();
152+
});
153+
}
154+
135155
@Test
136156
void templateExists() {
137157
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389").run((context) -> {
@@ -140,9 +160,23 @@ void templateExists() {
140160
assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", false);
141161
assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreNameNotFoundException", false);
142162
assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreSizeLimitExceededException", true);
163+
assertThat(ldapTemplate).extracting("objectDirectoryMapper")
164+
.isSameAs(context.getBean(ObjectDirectoryMapper.class));
143165
});
144166
}
145167

168+
@Test
169+
void templateCanBeConfiguredWithCustomObjectDirectoryMapper() {
170+
ObjectDirectoryMapper objectDirectoryMapper = mock(ObjectDirectoryMapper.class);
171+
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")
172+
.withBean(ObjectDirectoryMapper.class, () -> objectDirectoryMapper)
173+
.run((context) -> {
174+
assertThat(context).hasSingleBean(LdapTemplate.class);
175+
LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class);
176+
assertThat(ldapTemplate).extracting("objectDirectoryMapper").isSameAs(objectDirectoryMapper);
177+
});
178+
}
179+
146180
@Test
147181
void templateConfigurationCanBeCustomized() {
148182
this.contextRunner

0 commit comments

Comments
 (0)