fix(validation): guard spec traversal against malformed structural nodes (#259)#260
Merged
Merged
Conversation
…es (#259) A malformed spec whose `paths`, path item, operation, or `responses` node decoded to a scalar slipped past the `?? []` absence guards and raised an uncaught TypeError deep in the traversal path. Add `is_array()` guards along the full traversal: four stages in OpenApiResponseValidator (`paths`, path item, operation, `responses` map) and three in OpenApiRequestValidator (`paths`, path item, operation). Each surfaces a loud spec error ("expected object, got scalar") instead of a misleading diagnostic or a crash. This extends the per-response guards from #256/#258 across the entire traversal. Add a malformed-paths fixture and extend the malformed fixture; cover the new guards with TDD tests.
…#259) Address multi-agent review feedback on PR #260. Extend all four traversal-depth guards to also catch null structure nodes. Switch presence checks for `paths` and operations from `isset` to `array_key_exists`, since `isset` returns false for present-but-null values and silently dropped them past the guard. Route path item and responses lookups through `array_key_exists` / `?? null` so an explicit null reaches the guard instead of being masked by `?? []`. Replace the hardcoded "got scalar" error text with `get_debug_type()` so null is no longer mislabeled as a scalar and the concrete type (string, null, etc.) is reported. Correct the guard comments to describe the actual behavior: each guard prevents an uncaught TypeError on a non-array node. Add fixtures covering each level: `/null-path-item`, `/null-operation`, `/null-responses-map` and `/scalar-operation-template/{id}` in malformed.json, plus a new malformed-paths-null.json whose root `paths` is null. Add tests for the null guards at every depth and verify that, for templated paths, the message is keyed by the matched spec path.
…rds (#259) Introduce a MalformedSpecNode helper (isMalformed() / describe()) and route all 15 guard sites through it: #259 traversal (7), #258 responses[$status] (1), and #256 content/schema (7). In addition to scalars and null, JSON lists ([...] written where an object is expected) are now detected as malformed. List values previously passed is_array() and were silently mis-resolved; they are now surfaced as loud spec errors. Empty arrays remain tolerated since {} and [] are indistinguishable. Error messages now report the actual node type via describe() (string / null / list, etc.) instead of the hardcoded "got scalar". Add unit tests for the helper and list-value integration tests for each guard family, and update existing assertions that assumed "got scalar" to the real type. Extend malformed.json with list-value paths and add the malformed-paths-list.json fixture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
malformed spec の構造ノード(非配列スカラー)が spec 走査経路で未捕捉
TypeErrorを出す問題を修正し、RequestBodyValidatorと同じ粒度の loud な spec エラーとして surface するようにしました。変更内容
OpenApiResponseValidator::validate()/OpenApiRequestValidator::validate()は spec を段階的に走査しますが、各段階で構造ノードが非配列スカラーのケースをガードしていませんでした。?? []はキー不在には効くものの、スカラー値が存在する場合は素通しし、array_keys()やarray型ヒント、SpecResponseKeyResolver::resolve()に到達して未捕捉TypeError(TypeError extends Error)でクラッシュしていました。#256(content/schema)・#258(responses[$status])が確立した per-response の malformed ガードを、その上位の走査経路にも広げます。OpenApiResponseValidator::validate()に 4 段のis_array()ガードを追加(paths/ path item / operation /responsesマップ)。operation を$operation変数として抽出responsesマップのガードは skip-by-status-code チェックより前に配置。malformed なresponsesマップは構造エラーであり status-code-level の失敗ではないため、skip パターン(5xx 等)に隠さず常に loud に surface するOpenApiRequestValidator::validate()に 3 段のis_array()ガードを追加(paths/ path item / operation)。responsesマップは既存のis_array() ? : []ガードがあるため対象外Malformed 'paths[...]...' for {METHOD} {path} in '{spec}' spec: expected object, got scalar.)malformed.jsonに/scalar-path-item・/scalar-operation・/scalar-responses-mapを追加、ルートpathsがスカラーの新規malformed-paths.jsonを追加関連情報
ResponseBodyValidatorの非配列content/schemaガードresponses[$status]エントリのガード(本 PR の直下階層)