Skip to content

Consider empty array documented when a field beneath it is documented - #1064

Open
sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:gh-626-mixed-presence-field-documentation
Open

sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:gh-626-mixed-presence-field-documentation

Conversation

@sharanggupta

Copy link
Copy Markdown

Fixes gh-626

When a field beneath an array is documented, for example items[].k2[].name, removing it from the payload cascades upwards and removes any container that the removal leaves empty. An items[] entry whose k2 array is empty to begin with is never reached by this cascade because it has no name entry to remove, so the empty array is left behind and the payload is reported as having undocumented content ({"items":[{"k2":[]}]}) even though every field in it has been documented.

This change makes JsonFieldProcessor notify its callback when traversal reaches an empty collection through a non-leaf array segment. The removal callbacks remove the empty collection as they would a matched leaf, so it and any containers it leaves empty are removed. The callbacks that determine a field's presence (hasField) and extract values for type discovery (extract) ignore the notification, so their behaviour is unchanged.

Scope is deliberately limited to empty arrays, as described in the issue. The analogous mixed-presence cases of a null or an empty object on the path are left as they are; happy to look at those too if you think they belong in the same change.

Tests cover remove and removeSubsection on sometimes-empty and always-empty arrays, the exact payload from the issue via JsonContentHandler#getUndocumentedContent, and an end-to-end ResponseFieldsSnippet rendering. The spring-restdocs-core suite (907 tests), checkFormat and checkstyle pass.

When a field beneath an array is documented, for example
items[].k2[].name, removing it from the payload cascades upwards and
removes any container that the removal leaves empty. An items[] entry
whose k2 array is empty to begin with is never reached by this cascade
as it has no name entry to remove. The empty array is left behind and
the payload is reported as having undocumented content even though
every field in it has been documented.

When traversal reaches an empty collection through a non-leaf array
segment, it now notifies the callback. The callbacks used for removal
remove the empty collection as they would a matched leaf, so that it
and any containers it leaves empty are removed. The callbacks used to
determine a field's presence and to extract its values ignore the
notification so their behavior is unchanged.

Fixes spring-projectsgh-626

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
@sharanggupta
sharanggupta force-pushed the gh-626-mixed-presence-field-documentation branch from c083e03 to aa2bde4 Compare October 6, 2026 22:03

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When a payload has fields with mixed presence documenting those fields may still result in a failure due to undocumented fields

2 participants