Skip to content

Commit

Permalink
Prevent AbstractTypeMapper NPE
Browse files Browse the repository at this point in the history
Prevent an NPE caused by AbstractTypeMapper returning "null", that is
abstaining from the type decision.

This is useful e.g. to have an optional type discriminator present which
is generated by the system but not needed on reparsing if parse is
called with the correct type known by other means.

Add test.
  • Loading branch information
fforw committed Oct 7, 2015
1 parent e81b875 commit 7613207
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/org/svenson/AbstractTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public abstract class AbstractTypeMapper implements TypeMapper

protected Object getPropertyValueFromTokenStream(JSONTokenizer tokenizer, String propertyName, Token first)
{
return getPropertyTokenFromTokenStream(tokenizer, propertyName, first).value();
Token token = getPropertyTokenFromTokenStream(tokenizer, propertyName, first);
return token != null ? token.value() : null;
}
protected Token getPropertyTokenFromTokenStream(JSONTokenizer tokenizer, String propertyName, Token first)
{
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/svenson/TypeMapperNeutralityTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.svenson;

import org.junit.Test;
import org.svenson.matcher.PrefixPathMatcher;
import org.svenson.matcher.TruePathMatcher;

/**
* Created by sven on 07.10.15.
*/
public class TypeMapperNeutralityTestCase
{

@Test
public void testName() throws Exception
{
JSONParser parser = new JSONParser();
Mapper typeMapper = new Mapper();
typeMapper.setPathMatcher(new TruePathMatcher());

parser.setTypeMapper(typeMapper);

parser.parse("{\"foo\":1}");

}

private static class Mapper extends AbstractPropertyValueBasedTypeMapper
{
@Override
protected Class getTypeHintFromTypeProperty(Object value) throws IllegalStateException
{
return null;
}
}
}

0 comments on commit 7613207

Please sign in to comment.