From 81c28166935fb59666aef2d0736d4f5fd8bfce5c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 8 Mar 2017 12:35:30 +0000 Subject: [PATCH] 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 --- .../restdocs/payload/JsonContentHandler.java | 6 ++- .../payload/JsonContentHandlerTests.java | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java index f1fff9ce..ae569937 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java @@ -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); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java new file mode 100644 index 00000000..b5f74523 --- /dev/null +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java @@ -0,0 +1,54 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.payload; + +import java.io.IOException; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link JsonContentHandler}. + * + * @author Andy Wilkinson + */ +public class JsonContentHandlerTests { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void typeForFieldWithNullValueMustMatch() throws IOException { + this.thrown.expect(FieldTypesDoNotMatchException.class); + new JsonContentHandler("{\"a\": null}".getBytes()) + .determineFieldType(new FieldDescriptor("a").type(JsonFieldType.STRING)); + } + + @Test + public void typeForOptionalFieldWithNullValueDoesNotHaveToMatch() throws IOException { + Object fieldType = new JsonContentHandler("{\"a\": null}".getBytes()) + .determineFieldType( + new FieldDescriptor("a").type(JsonFieldType.STRING).optional()); + assertThat((JsonFieldType) fieldType, is(equalTo(JsonFieldType.STRING))); + } + +}