You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Go的kin-openapi/openapi3中为Swagger参数设置Example值

为kin-openapi路径参数设置Example值

你可以通过两种方式给路径参数添加Example值:

方式1:通过Schema设置Example

直接在创建的StringSchema上调用WithExample()方法传入示例值:

swagger.Paths = openapi3.Paths{
    "/postval/subsid/{subscriberid}/updatefle": &openapi3.PathItem{
        Post: &openapi3.Operation{
            OperationID: "postvalue",
            Parameters: []*openapi3.ParameterRef{
                {
                    Value: openapi3.NewPathParameter("subscriberid").
                        WithDescription("SubscriberIdentifier").
                        WithRequired(true).
                        WithSchema(openapi3.NewStringSchema().WithExample("SUBS-12345")),
                },
            },
        },
    },
}

方式2:直接给Parameter设置Example

也可以跳过Schema,直接调用Parameter的WithExample()方法绑定示例:

swagger.Paths = openapi3.Paths{
    "/postval/subsid/{subscriberid}/updatefle": &openapi3.PathItem{
        Post: &openapi3.Operation{
            OperationID: "postvalue",
            Parameters: []*openapi3.ParameterRef{
                {
                    Value: openapi3.NewPathParameter("subscriberid").
                        WithDescription("SubscriberIdentifier").
                        WithRequired(true).
                        WithSchema(openapi3.NewStringSchema()).
                        WithExample("SUBS-12345"),
                },
            },
        },
    },
}

两种方式都能生成符合OpenAPI规范的示例值,可根据实际需求选择。

内容的提问来源于stack exchange,提问作者Arun M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:10:41