Swagger List Not Mapping in Example Java

py49o6xq  于 2023-10-18  发布在  Java
关注(0)|答案(1)|浏览(130)

我用的是Swagger 3.0。我无法在swagger-ui中Map示例请求中的对象列表。附上截图以供参考。同样的情况也发生在其他数据类型上,比如String和Map。

请求结构-

"requestBody": {
  "description": "MyPartner Cards Request",
  "content": {
    "application/json": {
      "schema": {
        "$ref": "#/components/schemas/MyPartnerCardInfoRequest",
        "exampleSetFlag": false
      },
      "example": {
        "cards": [
          {
            "cardId": "HERO_LOYALTY_WELCOME_CARD",
            "templateId": "partner_welcome_card",
            "cardVariantId": "partner_welcome_card",
            "componentId": "basesheet"
          },
          {
            "cardId": "HERO_LOYALTY_BANNER_CARD",
            "templateId": "partner_welcome_card",
            "cardVariantId": "partner_welcome_card",
            "componentId": "basesheet"
          }
        ]
      },
      "exampleSetFlag": true
    }
  },
  "required": true
}

应正确Map数据类型。

mm9b1k5b

mm9b1k5b1#

这里有一个有效的样本,你的确切描述。它似乎在swagger-ui上工作得很好。但是,您的payload中有一个额外的属性,它会抛出exampleSetFlag的错误。这不是OpenAPI架构中的有效关键字。
不过,您的示例似乎加载正常

{
  "openapi" : "3.0.3",
  "info" : {
    "title" : "test",
    "version" : "1.0.0"
  },
  "servers" : [ ],
  "paths" : {
    "/endpoint" : {
      "post" : {
        "summary" : "stack",
        "parameters" : [ ],
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/MyPartnerCardInfoRequest",
                "exampleSetFlag": false
              },
              "example" : {
                "cards" : [ {
                  "cardId" : "HERO_LOYALTY_WELCOME_CARD",
                  "templateId" : "partner_welcome_card",
                  "cardVariantId" : "partner_welcome_card",
                  "componentId" : "basesheet"
                }, {
                  "cardId" : "HERO_LOYALTY_BANNER_CARD",
                  "templateId" : "partner_welcome_card",
                  "cardVariantId" : "partner_welcome_card",
                  "componentId" : "basesheet"
                } ]
              },
              "exampleSetFlag": true
            }
          },
          "required" : true
        },
        "responses" : {
          "201" : {
            "description" : "Created",
            "headers" : {
              "location" : {
                "schema" : {
                  "type" : "string"
                }
              }
            },
            "content" : {
              "application/json" : {
                "schema" : { }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : {
      "MyPartnerCardInfoRequest" : {
        "type" : "object",
        "properties" : {
          "cards" : {
            "type" : "array",
            "items" : { }
          }
        }
      }
    }
  }
}

OpenAPI 3.x.x的首选关键字是examples(复数)关键字,它接受模式的集合。这允许相同有效载荷的多个示例。

{
  "openapi": "3.0.3",
  "info": {
    "title": "test",
    "version": "1.0.0"
  },
  "servers": [],
  "paths": {
    "/endpoint": {
      "post": {
        "summary": "stack",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MyPartnerCardInfoRequest"
              },
              "examples": {
                "example_1": {
                  "value": {
                    "cards": [
                      {
                        "cardId": "HERO_LOYALTY_WELCOME_CARD",
                        "templateId": "partner_welcome_card",
                        "cardVariantId": "partner_welcome_card",
                        "componentId": "basesheet"
                      },
                      {
                        "cardId": "HERO_LOYALTY_BANNER_CARD",
                        "templateId": "partner_welcome_card",
                        "cardVariantId": "partner_welcome_card",
                        "componentId": "basesheet"
                      }
                    ]
                  }
                }
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created",
            "headers": {
              "location": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyPartnerCardInfoRequest": {
        "type": "object",
        "properties": {
          "cards": {
            "type": "array",
            "items": {}
          }
        }
      }
    }
  }
}

相关问题