Skip to content
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

Fixes StringProperty not detecting changes to empty string properly #647

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/sirius/db/mixing/properties/StringProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sirius.db.mixing.Mixing;
import sirius.db.mixing.Property;
import sirius.db.mixing.PropertyFactory;
import sirius.db.mixing.annotations.DefaultValue;
import sirius.db.mixing.annotations.Lob;
import sirius.db.mixing.annotations.LowerCase;
import sirius.db.mixing.annotations.Trim;
Expand Down Expand Up @@ -124,6 +125,25 @@ protected void setValueToField(Object value, Object target) {
super.setValueToField(value, target);
}

@Override
protected boolean isConsideredNull(Object propertyValue) {
if (trim) {
return Strings.isEmpty(Strings.trim(propertyValue));
}
return Strings.isEmpty(propertyValue);
}

@Override
protected void determineDefaultValue() {
DefaultValue defaultValueAnnotation = field.getAnnotation(DefaultValue.class);
if (defaultValueAnnotation != null) {
this.defaultValue = Value.of(transformValueFromImport(Value.of(defaultValueAnnotation.value())));
} else {
Object initialValue = getValue(getDescriptor().getReferenceInstance());
this.defaultValue = Value.of(initialValue);
}
}

@Override
public Object transformValue(Value value) {
if (value.isFilled()) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/sirius/db/jdbc/DataTypesEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sirius.db.mixing.annotations.Length;
import sirius.db.mixing.annotations.NullAllowed;
import sirius.db.mixing.annotations.Numeric;
import sirius.db.mixing.annotations.Trim;
import sirius.kernel.commons.Amount;

import java.time.LocalDate;
Expand All @@ -36,6 +37,11 @@ public enum TestEnum {
@NullAllowed
private String stringValue;

@Trim
@Length(255)
@NullAllowed
private String stringValueNoDefault;

@DefaultValue("300")
@NullAllowed
@Numeric(precision = 20, scale = 3)
Expand Down Expand Up @@ -87,6 +93,14 @@ public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

public String getStringValueNoDefault() {
return stringValueNoDefault;
}

public void setStringValueNoDefault(String stringValueNoDefault) {
this.stringValueNoDefault = stringValueNoDefault;
}

public Amount getAmountValue() {
return amountValue;
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/sirius/db/jdbc/DataTypesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import sirius.kernel.commons.Value
import sirius.kernel.di.std.Part
import java.time.Duration
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@ExtendWith(SiriusExtension::class)
Expand All @@ -33,6 +34,18 @@ class DataTypesTest {
assertEquals(Long.MAX_VALUE, test.longValue)
}

@Test
fun `StringProperty detects changes to empty values properly`() {
var test = DataTypesEntity()
test.stringValueNoDefault = ""

oma.update(test)
val afterFirstSave = oma.refreshOrFail(test)

afterFirstSave.stringValueNoDefault = ""
assertFalse { afterFirstSave.isAnyMappingChanged }
}

@Test
fun `default values work when using parseValue()`() {
var test = DataTypesEntity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class DefaultValuesTest {
@MethodSource("provideParametersForValuesTransformed")
fun `column default values are properly transformed`(propertyName: String, expectedDefault: String?) {
assertEquals(
expectedDefault,
mixing.getDescriptor(SQLDefaultValuesEntity::class.java).findProperty(propertyName)
?.getColumnDefaultValue()
?.getColumnDefaultValue(),
expectedDefault,
)
}

Expand Down