Skip to content

feat: clear $ref fields across entire OpenAPI document#928

Open
guan404ming wants to merge 3 commits intomeshery:masterfrom
guan404ming:fix/clear-refs-entire-doc
Open

feat: clear $ref fields across entire OpenAPI document#928
guan404ming wants to merge 3 commits intomeshery:masterfrom
guan404ming:fix/clear-refs-entire-doc

Conversation

@guan404ming
Copy link
Member

@guan404ming guan404ming commented Mar 7, 2026

Description

This PR fixes #926

Notes for Reviewers

Why

The getResolvedManifest function only cleared $ref fields from components/schemas, leaving refs in paths, parameters, responses, request bodies, headers, and callbacks unresolved.

How

  • Extract ref-clearing into clearDocRefs that traverses paths, all component types, and nested
    callbacks
  • Add helpers for clearing refs in parameters, headers, request bodies, responses, operations, and
    content
  • Add tests verifying ref resolution in paths, request bodies, responses, and headers

Signed commits

  • Yes, I signed my commits.

@welcome
Copy link

welcome bot commented Mar 7, 2026

Yay, your first pull request! 👍 A contributor will be by to give feedback soon. In the meantime, you can find updates in the #github-notifications channel in the community Slack.
Be sure to double-check that you have signed your commits. Here are instructions for making signing an implicit activity while performing a commit.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the OpenAPI document processing by expanding the $ref field clearing mechanism. Previously, only schema references within components/schemas were resolved. The updated implementation now systematically traverses and clears $ref fields across the entire OpenAPI document, including paths, parameters, headers, request bodies, responses, and callbacks, ensuring a fully inlined and resolved OpenAPI specification.

Highlights

  • Expanded Ref Resolution: The $ref clearing logic in getResolvedManifest was extended to cover the entire OpenAPI document, beyond just components/schemas.
  • New Helper Functions: Introduced a suite of new functions (clearDocRefs, clearContentRefs, clearParameterRefs, clearHeaderRefs, clearRequestBodyRefs, clearResponseRefs, clearCallbackRefs, clearPathItemRefs, clearOperationRefs) to systematically traverse and clear $ref fields in various OpenAPI elements.
  • Comprehensive Testing: Added new test cases (TestGetResolvedManifest_PathsAndComponents, TestGetResolvedManifest_HeaderRefs) to validate the correct resolution of $ref fields in paths, parameters, request bodies, responses, and headers.
Changelog
  • utils/component/openapi_generator.go
    • Modified getResolvedManifest to delegate the comprehensive $ref clearing to a new clearDocRefs function.
    • Implemented clearDocRefs to orchestrate the clearing of $ref fields across all relevant parts of an OpenAPI document, including components and paths.
    • Added several new helper functions (clearContentRefs, clearParameterRefs, clearHeaderRefs, clearRequestBodyRefs, clearResponseRefs, clearCallbackRefs, clearPathItemRefs, clearOperationRefs) to handle specific OpenAPI object types.
  • utils/component/openapi_generator_test.go
    • Introduced TestGetResolvedManifest_PathsAndComponents to verify that $ref fields in paths, parameters, request bodies, and responses are correctly resolved.
    • Added TestGetResolvedManifest_HeaderRefs to specifically test the resolution of $ref fields within headers.
Activity
  • The pull request addresses an existing issue, indicated by "This PR fixes #".
  • The author, guan404ming, has signed their commits.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the OpenAPI reference clearing logic into a new, more comprehensive clearDocRefs function, designed to traverse various parts of the OpenAPI document and clear $ref fields for full inlining. However, it introduces a potential infinite recursion vulnerability in the handling of Callbacks, PathItems, and Operations due to a lack of cycle detection. This could lead to a stack overflow and Denial of Service if circular references are present. It is recommended to implement tracking of visited objects during recursive traversal, similar to the existing cycle detection in clearSchemaRefs. Additionally, consider expanding clearDocRefs to handle a few more component types for full completeness.

Comment on lines +294 to +334
func clearCallbackRefs(cr *openapi3.CallbackRef, stack map[*openapi3.Schema]bool) {
if cr == nil {
return
}
cr.Ref = ""
if cr.Value != nil {
for _, pathItem := range cr.Value.Map() {
clearPathItemRefs(pathItem, stack)
}
}
}

func clearPathItemRefs(pathItem *openapi3.PathItem, stack map[*openapi3.Schema]bool) {
if pathItem == nil {
return
}
for _, pr := range pathItem.Parameters {
clearParameterRefs(pr, stack)
}
for _, op := range pathItem.Operations() {
clearOperationRefs(op, stack)
}
}

func clearOperationRefs(op *openapi3.Operation, stack map[*openapi3.Schema]bool) {
if op == nil {
return
}
for _, pr := range op.Parameters {
clearParameterRefs(pr, stack)
}
clearRequestBodyRefs(op.RequestBody, stack)
if op.Responses != nil {
for _, rr := range op.Responses.Map() {
clearResponseRefs(rr, stack)
}
}
for _, cr := range op.Callbacks {
clearCallbackRefs(cr, stack)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

security-high high

The clearDocRefs function and its helpers (clearCallbackRefs, clearPathItemRefs, clearOperationRefs) recursively traverse the OpenAPI document to clear $ref fields. However, they do not implement a mechanism to detect or break circular references between Callbacks, PathItems, and Operations.

In OpenAPI 3, a Callback contains PathItems, a PathItem contains Operations, and an Operation can contain Callbacks. If an OpenAPI document contains a circular reference between these components (e.g., a Callback that eventually refers back to itself), the recursive calls will continue indefinitely until a stack overflow occurs, causing the application to crash. This represents a Denial of Service (DoS) vulnerability.

While clearSchemaRefs correctly uses a stack map to track and break circular references in schemas, no such protection exists for the Callback-PathItem-Operation recursion loop.

@guan404ming guan404ming force-pushed the fix/clear-refs-entire-doc branch from 4450557 to e7d0a7e Compare March 7, 2026 02:44
Signed-off-by: GUAN MING <guanmingchiu@gmail.com>
Signed-off-by: GUAN MING <guanmingchiu@gmail.com>
@guan404ming guan404ming force-pushed the fix/clear-refs-entire-doc branch 2 times, most recently from 7bd89b0 to 36f43b5 Compare March 7, 2026 06:06
Signed-off-by: GUAN MING <guanmingchiu@gmail.com>
@guan404ming guan404ming force-pushed the fix/clear-refs-entire-doc branch from 36f43b5 to c624be1 Compare March 7, 2026 06:08
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.

Increase robustness of manifest handling in model generation.

1 participant