Repository navigation
feat: stricter document path validation - #329
ySnoopyDogy wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #329 +/- ##
==========================================
+ Coverage 98.66% 98.67% +0.01%
==========================================
Files 94 95 +1
Lines 12310 12423 +113
==========================================
+ Hits 12146 12259 +113
Misses 139 139
Partials 25 25
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for adding this. The opt-in approach makes sense, and the full test suite passes at In I reproduced this with
Both cases also reproduce with this change applied to current Please make these changes:
The existing checks are green, but they don't cover this failure path yet. Thanks for working on this — let's close that gap before it goes in :) |
ef761b3 to
d26c829
Compare
|
Good catch! Now the validation runs even if building the model returns some errors. If the model can't be built, an explicit error is returned. I'm ignoring the build error when the model is still available, because ValidateDocument never checked whether |
| } | ||
|
|
||
| if options.ValidateDocumentPathParams { | ||
| model, modelErr := doc.BuildV3Model() |
There was a problem hiding this comment.
Please make model initialization safe for concurrent calls. BuildV3Model mutates the shared document without synchronization: eight concurrent ValidateOpenAPIDocument calls fail under -race with this option enabled; the same test passes without it. Add a regression test for concurrent validation of the same document.
There was a problem hiding this comment.
Sorry for the delay, I was on vacation. I’ve just pushed a new commit with that
d26c829 to
1baab9f
Compare
daveshanley
left a comment
There was a problem hiding this comment.
Thanks for fixing the concurrent validation case. I found two remaining edge cases and a small diagnostic-order issue.
| } | ||
|
|
||
| if options.ValidateDocumentPathParams { | ||
| buildModelMu.Lock() |
There was a problem hiding this comment.
Could we scope this lock to each document? A slow BuildV3Model call currently blocks validation of every other document using this option. I reproduced this with two separate documents. Please keep the same-document race test and add a test that unrelated documents can proceed independently.
| hasOperations := operations != nil && operations.Len() > 0 | ||
| hasPathLevelParams := len(pathItem.Parameters) > 0 | ||
|
|
||
| if !hasOperations && !hasPathLevelParams { |
There was a problem hiding this comment.
Could we limit this exemption to an actually empty Path Item? /pets/{id} with only a summary currently passes without an id parameter. The OpenAPI exemption applies to empty items. A test for a summary-only item alongside the existing {} case would cover this.
| for name := range pathLevelParams { | ||
| effective[name] = true | ||
| } | ||
| for name := range templateVars { |
There was a problem hiding this comment.
Could we emit these errors in path order, or sort the names first? Map iteration changes the error order between runs; I saw four orders for the same document. Please apply the same ordering to the branch below so reports stay stable.
While using the lib, I notice that the document validation is not strict enough. The OpenAPI spec does not allow to have path parameters defined that diverge from the path templates of an operation.
I checked against the kin-openapi, and their lib correctly validate the document that do not include the correct path templates:
To make the libopenapi-validator more strict following the OpenAPI spec without adding a breaking change, there is a new option called
ValidateDocumentPathParams, with its functionWithPathParameterDocumentValidationto enable the config.With this config, we run another validation just to catch the missing and extra path parameters.
All tests pass, and the new ones ensure that the stricter path checking is working as intended.