Relax field type validation when marked as optional and value is null

Previously, if a field was marked as optional and it appeared in a
payload with a null value a failure would occur if the field's type
was configured to be a JsonFieldType other than NULL.

This commit relaxes this restriction so that a null value is tolerated
when a field has been marked as optional.

Closes gh-365
This commit is contained in:
Andy Wilkinson
2017-03-08 12:35:30 +00:00
parent 281abb2e8b
commit 81c2816693
2 changed files with 58 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -108,7 +108,9 @@ class JsonContentHandler implements ContentHandler {
JsonFieldType actualFieldType = this.fieldTypeResolver
.resolveFieldType(fieldDescriptor.getPath(), readContent());
if (descriptorFieldType == JsonFieldType.VARIES
|| descriptorFieldType == actualFieldType) {
|| descriptorFieldType == actualFieldType
|| (fieldDescriptor.isOptional()
&& actualFieldType == JsonFieldType.NULL)) {
return descriptorFieldType;
}
throw new FieldTypesDoNotMatchException(fieldDescriptor, actualFieldType);