feat(document): add GetOperationById to search operations across path… - #3025
feat(document): add GetOperationById to search operations across path…#3025Mahdigln wants to merge 1 commit into
Conversation
Vincent Biret (baywet)
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
| { | ||
| if (pathItem.Operations is not null) | ||
| { | ||
| foreach (var operation in pathItem.Operations.Values) |
There was a problem hiding this comment.
this is running at o(n) (n being the number of operations) and going to be really slow for larger API descriptions. Could you look into optimizing the search please?
There was a problem hiding this comment.
Thanks for the feedback! To address the O(n) concern, I'm thinking of building a lazy-initialized Dictionary<string, OpenApiOperation> index on the first call to GetOperationById, making subsequent lookups O(1). The trade-off is that the cache becomes stale if Paths or Webhooks are mutated after the index is built — but since OpenApiDocument is typically read-only after parsing, this seems acceptable.
Does this approach work for you, or do you have a different optimization in mind?
There was a problem hiding this comment.
That should work, but before we read into that index, we should be able to correlate whether the sources have changed since the last time we built the index and rebuild it if required.
There was a problem hiding this comment.
Thanks for the clarification! To implement change detection, I need to understand how to track mutations to Paths and Webhooks. A few options I'm considering:
Count-based: Compare Paths.Count and Webhooks?.Count — simple but misses replacements (same count, different items)
Version counter: Increment a counter whenever Paths or Webhooks is mutated — but OpenApiPaths doesn't expose change notifications today
Wrap collections: Replace OpenApiPaths with an observable/versioned collection that notifies the document on change
Do you have a preferred approach, or is there existing infrastructure in the codebase I should leverage?
There was a problem hiding this comment.
have you considered storing the hash code for those values next to the index. And ahead of querying the index, if the current hash codes don't match, recompute + update the values. ??
There was a problem hiding this comment.
Before implementing, one question: how should the hash be computed for Paths and Webhooks? Should I use the existing GetHashCodeAsync on the document, or compute something lighter like combining Paths.Count, Webhooks?.Count, and the hash codes of the keys?
Description
Adds a
GetOperationById(string operationId)method toOpenApiDocumentthat searches for an operation by itsoperationIdacross bothPathsandWebhooks.Closes #1654
Type of Change
Related Issue(s)
Closes #1654
Changes Made
GetOperationById(string operationId)method toOpenApiDocumentPathsandWebhooksto cover the full documentOpenApiPathItemReference(when the reference is resolved)GetOperationByIdentry toPublicAPI.Unshipped.txtTesting
Tests cover:
PathsWebhooksnullwhen not foundgetUser≠GetUser)$refpath itemoperationIdexists (spec violation)ArgumentNullExceptionfor null or emptyoperationIdChecklist
Versions applicability
Additional Notes
GetOperationByRef(resolvingoperationRefvia JSON Pointer) is not included in this PR and can be addressed in a follow-up.