Skip to content

Conversation

@saschagrunert
Copy link
Member

@saschagrunert saschagrunert commented Dec 10, 2025

When the requiredfields linter is configured with omitempty policy set to 'Ignore', violations for missing omitempty tags were still reported because checkFieldPropertiesWithoutOmitEmpty was ignoring the configured policy.

The fix ensures that when omitEmptyPolicy is Ignore, all checks for fields without omitempty are skipped, respecting the user's configuration choice.

Fixes #195

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 10, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: saschagrunert
Once this PR has been reviewed and has the lgtm label, please assign everettraven for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch from 9d9985c to 56359ee Compare December 10, 2025 10:08
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch from 56359ee to 25c08c5 Compare December 10, 2025 10:11
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch from 25c08c5 to 0304415 Compare December 10, 2025 10:21
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch 2 times, most recently from 189bf7b to 34a1c28 Compare December 10, 2025 10:28
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 10, 2025
@saschagrunert saschagrunert changed the title WIP: Fix requiredfields linter to respect OmitEmpty Ignore policy Fix requiredfields linter to respect OmitEmpty Ignore policy Dec 10, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch from 34a1c28 to 6f4488a Compare December 10, 2025 10:36
analysistest.RunWithSuggestedFixes(t, testdata, a, "b")
}

func TestWhenRequiredWithOmitEmptyIgnorePreferenceConfiguration(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't make sense anymore?

Comment on lines 185 to 187
// Force the omitempty policy to suggest a fix. We can only get to this function when the policy is configured to Ignore.
// Since we absolutely have to add the omitempty tag, we can report it as a suggestion.
reportShouldAddOmitEmpty(pass, field, OmitEmptyPolicySuggestFix, qualifiedFieldName, "field %s does not allow the zero value. It must have the omitempty tag.", jsonTags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this isn't a valid case?

}), "pointers_when_required")
}

func TestPointersWhenRequiredOmitEmptyIgnore(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer makes sense?

The OmitEmpty Ignore policy is designed to suppress style-related
omitempty suggestions, but it must still enforce correctness
requirements. When a field doesn't allow the zero value (e.g., has
MinLength=1 validation), the field absolutely requires the omitempty
tag to function correctly - without it, unstructured clients will
marshal the zero value which will fail validation.

This is a correctness issue, not a style preference, so it must be
reported even when the policy is set to Ignore.

Changes:
- Restored original behavior in checkFieldPropertiesWithoutOmitEmpty
  to always report fields that don't allow zero values
- Force OmitEmptyPolicySuggestFix for these cases regardless of policy
- Restored test cases that verify this behavior
- Updated test expectations to match correct behavior

Fixes kubernetes-sigs#195

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
@saschagrunert saschagrunert changed the title Fix requiredfields linter to respect OmitEmpty Ignore policy WIP: Fix requiredfields linter to respect OmitEmpty Ignore policy Dec 10, 2025
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 10, 2025
@saschagrunert saschagrunert force-pushed the fix-195-omitempty-ignore-policy branch from 6f4488a to 543dfc4 Compare December 10, 2025 12:01
@saschagrunert
Copy link
Member Author

Closing this one for now, I assume we don't have to fix anything if the behavior is intended.

@saschagrunert saschagrunert deleted the fix-195-omitempty-ignore-policy branch December 10, 2025 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

requiredfields linter config with policy ignore makes the linter to not ignore

3 participants