-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Require explicit representation configuration for UUID
, BigDecimal
, and BigInteger
#5044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
* Base class for Spring Data MongoDB to be extended for JavaConfiguration usage. | ||
* | ||
* @author Mark Paluch | ||
* @author Hyunsang Han | ||
* @since 2.0 | ||
*/ | ||
public abstract class MongoConfigurationSupport { | ||
|
@@ -226,9 +227,15 @@ protected boolean autoIndexCreation() { | |
protected MongoClientSettings mongoClientSettings() { | ||
|
||
MongoClientSettings.Builder builder = MongoClientSettings.builder(); | ||
builder.uuidRepresentation(UuidRepresentation.STANDARD); | ||
configureClientSettings(builder); | ||
return builder.build(); | ||
|
||
MongoClientSettings settings = builder.build(); | ||
|
||
if (settings.getUuidRepresentation() == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would break applications for users that do not use UUIDs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You're right. I realize I totally went in the wrong direction here. Thanks for pointing out that forcing configuration for everyone is too invasive. I’ve been thinking this week about how to make the validation smarter, only checking when these types are actually in use. Here's what I'm considering:
Does this direction make more sense? If so, I can refine it quickly and reopen the PR. 💪 |
||
throw new IllegalStateException("UUID representation must be explicitly configured."); | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ | |
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.bson.UuidRepresentation; | ||
import org.jspecify.annotations.Nullable; | ||
import org.springframework.beans.factory.config.AbstractFactoryBean; | ||
import org.springframework.dao.DataAccessException; | ||
|
@@ -52,6 +51,7 @@ | |
* | ||
* @author Christoph Strobl | ||
* @author Mark Paluch | ||
* @author Hyunsang Han | ||
*/ | ||
public class MongoClientFactoryBean extends AbstractFactoryBean<MongoClient> implements PersistenceExceptionTranslator { | ||
|
||
|
@@ -162,7 +162,6 @@ protected MongoClientSettings computeClientSetting() { | |
getOrDefault(port, "" + ServerAddress.defaultPort()))); | ||
|
||
Builder builder = MongoClientSettings.builder().applyConnectionString(connectionString); | ||
builder.uuidRepresentation(UuidRepresentation.STANDARD); | ||
|
||
if (mongoClientSettings != null) { | ||
|
||
|
@@ -305,7 +304,13 @@ protected MongoClientSettings computeClientSetting() { | |
}); | ||
} | ||
|
||
return builder.build(); | ||
MongoClientSettings settings = builder.build(); | ||
|
||
if (settings.getUuidRepresentation() == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
throw new IllegalStateException("UUID representation must be explicitly configured."); | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
private <T> void applySettings(Consumer<T> settingsBuilder, @Nullable T value) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
* | ||
* @author Mark Paluch | ||
* @author Christoph Strobl | ||
* @author Hyunsang Han | ||
* @since 2.0 | ||
* @see org.springframework.data.convert.CustomConversions | ||
* @see org.springframework.data.mapping.model.SimpleTypeHolder | ||
|
@@ -159,7 +160,7 @@ public static class MongoConverterConfigurationAdapter { | |
private static final Set<Class<?>> JAVA_DRIVER_TIME_SIMPLE_TYPES = Set.of(LocalDate.class, LocalTime.class, LocalDateTime.class); | ||
|
||
private boolean useNativeDriverJavaTimeCodecs = false; | ||
private BigDecimalRepresentation bigDecimals = BigDecimalRepresentation.DECIMAL128; | ||
private @Nullable BigDecimalRepresentation bigDecimals; | ||
private final List<Object> customConverters = new ArrayList<>(); | ||
|
||
private final PropertyValueConversions internalValueConversion = PropertyValueConversions.simple(it -> {}); | ||
|
@@ -313,8 +314,8 @@ public MongoConverterConfigurationAdapter useSpringDataJavaTimeCodecs() { | |
} | ||
|
||
/** | ||
* Configures the representation to for {@link java.math.BigDecimal} and {@link java.math.BigInteger} values in | ||
* MongoDB. Defaults to {@link BigDecimalRepresentation#DECIMAL128}. | ||
* Configures the representation for {@link java.math.BigDecimal} and {@link java.math.BigInteger} values in | ||
* MongoDB. This configuration is required and must be explicitly set. | ||
* | ||
* @param representation the representation to use. | ||
* @return this. | ||
|
@@ -375,6 +376,10 @@ ConverterConfiguration createConverterConfiguration() { | |
svc.init(); | ||
} | ||
|
||
if (bigDecimals == null) { | ||
throw new IllegalStateException("BigDecimal representation must be explicitly configured."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same breaking behavior as for UUIDs. We would also break applications for users that do not use big decimals. |
||
} | ||
|
||
List<Object> converters = new ArrayList<>(STORE_CONVERTERS.size() + 7); | ||
|
||
if (bigDecimals == BigDecimalRepresentation.STRING) { | ||
|