JDK-4921212 : XMLEncoder does NOT encode NULL (value) entries in HashMap
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 1.4.0,1.4.2,1.4.2_01
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux,windows_2000
  • CPU: x86
  • Submitted: 2003-09-11
  • Updated: 2005-11-09
  • Resolved: 2005-11-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b60Fixed
Related Reports
Relates :  
Description
Name: gm110360			Date: 09/11/2003


FULL PRODUCT VERSION :
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195] SP 3

A DESCRIPTION OF THE PROBLEM :
If you Encode a java.util.HashMap containing en entry with
a "Key" and "null" for the value, then this entry will not
be included in the XML output.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a new java.util.HashMap
2. Add some entries using the "put" method
3. Add one entry like the following put("Key1", null);
4. decode the map using the java.beans.XMLEncoder

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="java.util.HashMap">
  <void method="put">
   <string>Key4</string>
   <string>4</string>
  </void>
  <void method="put">
   <string>Key3</string>
   <null/>
  </void>
  <void method="put">
   <string>Key1</string>
   <string>1</string>
  </void>
  <void method="put">
   <string>Key5</string>
   <string>5</string>
  </void>
  <void method="put">
   <string>Key2</string>
   <string>2</string>
  </void>
  <void method="put">
   <null/>
   <string>6</string>
  </void>
  <void method="put">
   <string>Key7</string>
   <string>7</string>
  </void>
 </object>
</java>




Actual Result:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="java.util.HashMap">
  <void method="put">
   <string>Key4</string>
   <string>4</string>
  </void>
  <void method="put">
   <string>Key1</string>
   <string>1</string>
  </void>
  <void method="put">
   <string>Key5</string>
   <string>5</string>
  </void>
  <void method="put">
   <string>Key2</string>
   <string>2</string>
  </void>
  <void method="put">
   <null/>
   <string>6</string>
  </void>
  <void method="put">
   <string>Key7</string>
   <string>7</string>
  </void>
 </object>
</java>


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
		Map myMap;

		myMap = new HashMap();

		myMap.put("Key1", "1");
		myMap.put("Key2", "2");
		myMap.put("Key3", null);
		myMap.put("Key4", "4");
		myMap.put("Key5", "5");
		myMap.put(null, "6");
		myMap.put("Key7", "7");

		try {

			XMLEncoder encoder = new XMLEncoder(new
BufferedOutputStream(new FileOutputStream("c:\\Test.xml")));
			encoder.writeObject(myMap);
			encoder.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
---------- END SOURCE ----------
(Incident Review ID: 181400) 
======================================================================

Comments
EVALUATION At comparison of contents of two maps it is necessary to check not only equality of values, but also presence of a keys if values are equal to null. Nine possible combinations are listed in the table below. Recommended actions are presented for each combination. In the previous version the sixth combination was processed as the fifth one. ----------------------------- | oldInstance | newInstance | | key | value | key | value | ----------------------------- 1.| yes | !null | yes | !null | put( oldKey, oldValue ) if value changed 2.| yes | !null | yes | null | put( oldKey, oldValue ) 3.| yes | !null | no | null | put( oldKey, oldValue ) ----------------------------- 4.| yes | null | yes | !null | put( oldKey, null ) 5.| yes | null | yes | null | 6.| yes | null | no | null | put( oldKey, null ) ----------------------------- 7.| no | null | yes | !null | remove( newKey ) 8.| no | null | yes | null | remove( newKey ) 9.| no | null | no | null | -----------------------------
14-10-2005

EVALUATION I have found out, that algorithm for checking the changes in the state of java.util.Map is wrong. If values returned for some key is null, then this algorithm considers that the condition has not changed. However, it is necessary to be convinced, that methods containsKey() return identical values. See line 283 in file java/beans/MetaData.java.
11-10-2005

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: dragon
14-06-2004

EVALUATION the null value in the Map/HashMap is not honored by the persistence mechanism. This is a valid value for the HashMap. Probably requires tweaking the Map persistence delegate. Commit to 1.5.1. May escalate depending on need. ###@###.### 2003-09-16
16-09-2003