Skip to content

A documented field beneath an optional field with a null value is incorrectly considered to be missing #557

@tomaszalusky

Description

@tomaszalusky

Hi,

I ran into issue which can be best summarized by following test method in org.springframework.restdocs.payload.JsonContentHandlerTests class.
I would like test to pass but test fails since a.b is considered missing unless FieldDescriptor a.b is explicitly marked as .optional().

	@Test
	public void describedMissingFieldThatIsChildOfOptionalObjectThatIsNullIsNotConsideredMissing() {
		List<FieldDescriptor> missingFields = new JsonContentHandler(
				"{\"a\":null}".getBytes()).findMissingFields(
						Arrays.asList(new FieldDescriptor("a").optional(),
								new FieldDescriptor("a.b")));
		//System.out.println(missingFields.get(0).getPath());
		assertThat(missingFields.size()).isEqualTo(0);
	}

I consider this approach inconsistent with describedMissingFieldThatIsChildOfNestedOptionalArrayThatIsEmptyIsNotConsideredMissing test where thanks to parent being optional, absence of c field is no problem although it is not optional itself. Why the b field in my example can't be treat the same way?

I appreciate that the support of inheriting optional flag was added in 1.2.5/2.0.2. It is great feature since it makes possible to control optionality only for root of potentially complex tree. However it seems to me as related only to arrays rather than objects. Am I missing something? Thanks for response.

(Note: The test passes on empty json {}. I intentionally don't reveal my investigation of source code, at first I'm interested in what should be correct at conceptual level.)

(Note: IMHO the test case is worth adding no matter what is correct behavior from your point of view.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions