Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[Bug]: content.examples $ref not resolved in request bodies or parameters

未关闭
#2,292 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@jacobps1 已经在做这个了。

开始于 2026年3月24日。

  • #2296 来自 @jacobps1 —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
java
领域
api

调研方向

首先使用启用 resolve 和 resolveFully 的 ParseOptions,通过 OpenAPIParser.readContents 重现该问题,并使用所示的 request-body 和 parameter 示例。跟踪解析器如何处理 content 示例,并比较这两个位置;完成的标准是:解析后的 OpenAPI 结果中每个示例引用都被替换为被引用示例的数据,并且两个情况都有回归测试覆盖。

由索引模型根据 Issue 内容生成。

描述

Bug

Description

When using resolveFully(true) to parse OpenAPI 3.0.x specs, $ref pointers within content.examples (in request bodies and parameters) are not resolved. The $ref remains as a pointer instead of being replaced with the referenced example.

Affected Version

  • 2.1.36

Steps to Reproduce

You can reproduce this issue by passing in an Open API 3.0.x spec with content examples into a parser with resolve options enabled.

String spec = """
   {
  "openapi": "3.0.3",
  "info": {
    "title": "Example Reference Demo API",
    "version": "1.0"
  },
  "paths": {
    "/users": {
      "post": {
        "summary": "Create a user",
        "parameters": [
          {
            "name": "filter",
            "in": "query",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "examples": {
                  "simpleFilter": {
                    "$ref": "#/components/examples/FilterExample"
                  }
                }
              }
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/User"
              },
              "examples": {
                "defaultUser": {
                  "$ref": "#/components/examples/UserExample"
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "examples": {
      "UserExample": {
        "summary": "A sample user",
        "value": {
          "id": "12345",
          "name": "John Doe"
        }
      },
      "FilterExample": {
        "summary": "A sample filter",
        "value": {
          "status": "active"
        }
      }
    }
  }
}
    """;

ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);

SwaggerParseResult openAPIresult = new OpenAPIParser().readContents(spec, null, options);
OpenAPI result = openAPIResult.getOpenAPI();
System.out.println("OpenAPI result: " + result);

Expected Behavior

Content examples are resolved:

{
  "paths": {
    "/users": {
      "post": {
        "summary": "Create a user",
        "parameters": [
          {
            "name": "filter",
            "in": "query",
            "content": {
              "application/json": {
                "schema": { "type": "object" },
                "examples": {
                  "simpleFilter": {
                    "summary": "A sample filter",
                    "value": { "status": "active" }
                  }
                }
              }
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": { "type": "string" },
                  "name": { "type": "string" }
                }
              },
              "examples": {
                "defaultUser": {
                  "summary": "A sample user",
                  "value": { "id": "12345", "name": "John Doe" }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "Created" } }
      }
    }
  }
}

Actual Behavior

Content examples remain as $ref pointers:

{
  "paths": {
    "/users": {
      "post": {
        "summary": "Create a user",
        "parameters": [
          {
            "name": "filter",
            "in": "query",
            "content": {
              "application/json": {
                "schema": { "type": "object" },
                "examples": {
                  "simpleFilter": {
                    "$ref": "#/components/examples/FilterExample"
                  }
                }
              }
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": { "type": "string" },
                  "name": { "type": "string" }
                }
              },
              "examples": {
                "defaultUser": {
                  "$ref": "#/components/examples/UserExample"
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "Created" } }
      }
    }
  }
}

Logs / Stack Traces

N/A

Environment

  • Java version: 21
  • Build tool: Maven 3.9
  • OS: macOS

Additional Context

N/A

Checklist

  • I have searched the existing issues and this is not a duplicate.
  • I have provided sufficient information for maintainers to reproduce the issue.
主要语言
Java
星标
870
派生
565
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

swagger-api/swagger-parser 的其他 Issue

查看 swagger-api/swagger-parser 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。