Skip to content

Commit 68b9191

Browse files
dnltskderXear
authored andcommitted
unknown fields and null-values getting ignored
1 parent eabaf90 commit 68b9191

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/main/java/com/dasburo/spring/cache/dynamo/rootattribute/RootAttributeReader.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class RootAttributeReader {
1919
public RootAttribute readRootAttribute(@NonNull RootAttributeConfig rootAttributeConfig, @NonNull Object object) {
2020
try {
2121
Object value = PropertyUtils.getProperty(object, rootAttributeConfig.getName());
22+
if (value == null) {
23+
return null;
24+
}
2225
AttributeValue attributeValue = mapValueToAttributeValue(value, rootAttributeConfig.getType());
2326
return new RootAttribute(rootAttributeConfig.getName(), attributeValue);
2427
}
@@ -31,13 +34,13 @@ public RootAttribute readRootAttribute(@NonNull RootAttributeConfig rootAttribut
3134
@Nullable
3235
private AttributeValue mapValueToAttributeValue(@NonNull Object value, @NonNull ScalarAttributeType type) {
3336
switch (type) {
34-
case S:
35-
return new AttributeValue().withS(String.valueOf(value));
3637
case N:
3738
return new AttributeValue().withN(String.valueOf(value));
3839
case B:
3940
return new AttributeValue().withB((ByteBuffer) value);
41+
case S:
42+
default:
43+
return new AttributeValue().withS(String.valueOf(value));
4044
}
41-
return null;
4245
}
4346
}

src/test/java/com/dasburo/spring/cache/dynamo/rootattribute/RootAttributeReaderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.LinkedHashMap;
1010

1111
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNull;
1213

1314
public class RootAttributeReaderTest {
1415

@@ -138,4 +139,31 @@ public void testRootAttributeReader_LinkedHashMapCanBeHandled() {
138139
assertEquals("stringField", rootAttribute.getName());
139140
assertEquals("dummy-text", rootAttribute.getAttributeValue().getS());
140141
}
142+
143+
@Test
144+
public void testRootAttributeReader_UnknownFieldIsGetsIgnored() {
145+
//given
146+
RootAttributeConfig rootAttributeConfig = new RootAttributeConfig("unknownField", ScalarAttributeType.S);
147+
SampleTestClass sampleInstance = new SampleTestClass();
148+
149+
//when
150+
RootAttribute rootAttribute = rootAttributeReader.readRootAttribute(rootAttributeConfig, sampleInstance);
151+
152+
//then
153+
assertNull(rootAttribute);
154+
}
155+
156+
@Test
157+
public void testRootAttributeReader_NullValueOfKnownFieldsGetsIgnored() {
158+
//given
159+
RootAttributeConfig rootAttributeConfig = new RootAttributeConfig("stringField", ScalarAttributeType.S);
160+
SampleTestClass sampleInstance = new SampleTestClass();
161+
sampleInstance.setStringField(null);
162+
163+
//when
164+
RootAttribute rootAttribute = rootAttributeReader.readRootAttribute(rootAttributeConfig, sampleInstance);
165+
166+
//then
167+
assertNull(rootAttribute);
168+
}
141169
}

0 commit comments

Comments
 (0)