Skip to content

Commit

Permalink
Fixes StringProperty not detecting changes to empty string properly
Browse files Browse the repository at this point in the history
- sirius.db.mixing.properties.StringProperty#setValueToField and sirius.db.mixing.properties.StringProperty#transformValue
  transform "" be persisted as null
- this transformation must be considered when checking for null as well

Fixes: SIRI-939
  • Loading branch information
mkeckmkeck committed Mar 5, 2024
1 parent 99b654f commit c2730a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/sirius/db/mixing/properties/StringProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ 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
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

0 comments on commit c2730a7

Please sign in to comment.