Skip to content

Commit b95b40c

Browse files
committed
#365 Corrected Model.hydrate() to be case-insensitive
using the case-insensitive attributeNames set of the model
1 parent 0ac46f1 commit b95b40c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Model.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,14 @@ public <T extends Model> T fromMap(Map input) {
155155
* @param attributesMap map containing values for this instance.
156156
*/
157157
protected void hydrate(Map<String, Object> attributesMap) {
158-
for (String attrName : getMetaModelLocal().getAttributeNames()) {
159-
Object value = null;
160-
boolean contains = false;
161-
if (attributesMap.containsKey(attrName.toLowerCase())) {
162-
value = attributesMap.get(attrName.toLowerCase());
163-
contains = true;
164-
} else if (attributesMap.containsKey(attrName.toUpperCase())) {
165-
value = attributesMap.get(attrName.toUpperCase());
166-
contains = true;
167-
}
168-
if (contains) {
169-
if (value instanceof Clob && getMetaModelLocal().cached()) {
170-
this.attributes.put(attrName, Convert.toString(value));
171-
}else {
172-
this.attributes.put(attrName, getMetaModelLocal().getDialect().overrideDriverTypeConversion(
173-
getMetaModelLocal(), attrName, value));
158+
Set<String> attributeNames = getMetaModelLocal().getAttributeNames();
159+
for (Map.Entry<String, Object> entry : attributesMap.entrySet()) {
160+
if (attributeNames.contains(entry.getKey())) {
161+
if (entry.getValue() instanceof Clob && getMetaModelLocal().cached()) {
162+
this.attributes.put(entry.getKey(), Convert.toString(entry.getValue()));
163+
} else {
164+
this.attributes.put(entry.getKey(), getMetaModelLocal().getDialect().overrideDriverTypeConversion(
165+
getMetaModelLocal(), entry.getKey(), entry.getValue()));
174166
}
175167
}
176168
}

activejdbc/src/test/java/org/javalite/activejdbc/ModelTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,13 @@ public void testNewFromMap() {
618618
a(p.isNew()).shouldBeTrue();
619619
a(p.isValid()).shouldBeTrue();
620620
}
621+
622+
@Test
623+
public void testNewFromMapCaseInsensive() {
624+
Person p = new Person().fromMap(map("NAME", "Joe", "Last_Name", "Schmoe", "dob", "2003-06-15"));
625+
626+
a(p.get("name")).shouldNotBeNull();
627+
a(p.get("last_name")).shouldNotBeNull();
628+
a(p.get("dob")).shouldNotBeNull();
629+
}
621630
}

0 commit comments

Comments
 (0)