Skip to content

Commit

Permalink
Fix NPE on single resource read
Browse files Browse the repository at this point in the history
When read an object instance without content-type and no model,
fallback to text
  • Loading branch information
jvermillard committed Apr 27, 2015
1 parent a971a78 commit 57539f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath p
if (rDesc != null && rDesc.multiple) {
format = ContentFormat.TLV;
} else {
if (rDesc.type == Type.OPAQUE) {
if (rDesc != null && rDesc.type == Type.OPAQUE) {
format = ContentFormat.OPAQUE;
} else {
format = ContentFormat.TEXT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.core.node.codec;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;

import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -67,6 +65,18 @@ public void text_manufacturer_resource() throws InvalidValueException {
assertEquals(value, resource.getValue().value);
}

@Test
public void no_model_and_no_content_type_then_fallback_to_text() throws InvalidValueException {
String value = "MyManufacturer";
LwM2mResource resource = (LwM2mResource) LwM2mNodeDecoder.decode(value.getBytes(Charsets.UTF_8), null,
new LwM2mPath(666, 0, 0), model);

assertEquals(0, resource.getId());
assertFalse(resource.isMultiInstances());
assertEquals(DataType.STRING, resource.getValue().type);
assertEquals(value, resource.getValue().value);
}

@Test
public void text_battery_resource() throws InvalidValueException {
LwM2mResource resource = (LwM2mResource) LwM2mNodeDecoder.decode("100".getBytes(Charsets.UTF_8),
Expand Down

0 comments on commit 57539f9

Please sign in to comment.