Skip to content

fix(datamodel): don't build an operation from a non-mapping value - #711

Open
Tjmstudios wants to merge 1 commit into
pb33f:mainfrom
Tjmstudios:null-operations
Open

Tjmstudios wants to merge 1 commit into
pb33f:mainfrom
Tjmstudios:null-operations

Conversation

@Tjmstudios

Copy link
Copy Markdown
Contributor

Summary

A null operation (get:, get: null, FOO: null under additionalOperations) builds into a non-nil, empty Operation, so the model reports an operation the document never declared. This skips operation values that aren't mappings, in the v3 and v2 path item builders.

Problem

paths:
  /x:
    get:
      responses: {"200": {description: ok}}
    post: null
    additionalOperations:
      FOO: null

pathItem.Post is non-nil, GetOperations() returns get, post and FOO, and nothing on the high model tells post and FOO apart from real operations with no fields. Only the YAML tag on the low RootNode (!!null) shows the difference.

v3 PathItem.Build runs BuildModel on whatever the slot holds and stores &op unconditionally, for the fixed slots (path_item.go:321) and for each additionalOperations entry (:383). A root-level additional operation is only checked for being a mapping before its $ref is resolved (:309), so FOO: {$ref: '#/x-null'} ends up as an empty operation keyed "null". The v2 PathItem.Build (datamodel/low/v2/path_item.go:114) has the same unconditional build.

Changes

  • v3 PathItem.Build skips an operation whose value, after $ref resolution, is not a mapping: fixed slots, additionalOperations entries and root-level additional operations. The slot stays empty and the high field stays nil.
  • v2 PathItem.Build gets the same check. It doesn't resolve operation $refs, so it checks the value as written.
  • A path item whose additionalOperations are all null ends up with no AdditionalOperations, the same as when they are absent.
  • get: {} is still an (empty) operation.

Effects of a null operation now being absent from the model:

  • Hash() of a path item with get: null equals the hash without get.
  • Rendering drops get: null instead of writing get: {}.
  • what-changed no longer reports put: null → absent as a removed operation.
  • An operation $ref whose target isn't a mapping is dropped as well, on render and in the bundler too. Before, render kept the $ref and the bundler wrote put: {}.

Tests

  • New, all failing on main:
    • TestPathItem_Build_NonMappingOperations, TestPathItem_Build_OnlyNullAdditionalOperations and TestPathItem_Hash_NullOperation in datamodel/low/v3/path_item_test.go
    • TestPathItem_NullOperations in datamodel/high/v3/path_item_test.go
    • TestPathItem_Build_NonMappingOperations in datamodel/low/v2/path_item_test.go
  • Cases covered:
    • every fixed slot as get: null, put:, post: ~, a scalar, an empty string or a sequence
    • an operation $ref that resolves to null, as a fixed slot, an additionalOperations entry and a root-level additional operation
    • null, scalar and empty additionalOperations entries
    • the hash of get: null against an absent and an empty get

go test ./... passes.

PathItem.Build built every operation slot with BuildModel and stored a
pointer to the result whatever the value was. "get:", "get: null" or
"FOO: null" under additionalOperations therefore came out as a non-nil,
empty Operation, the same as a real one with no fields. The high model
then reported a GET the document never declared.

Skip an operation whose value, after $ref resolution, is not a mapping.
In v3 this covers the fixed slots, additionalOperations entries and
root-level additional operations whose $ref resolves to a non-mapping.
The v2 PathItem.Build had the same defect and gets the same check. An
empty mapping ("get: {}") is still an operation.
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (88615e4) to head (9fd9215).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #711   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          315       315           
  Lines        39758     39764    +6     
=========================================
+ Hits         39758     39764    +6     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@daveshanley daveshanley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants