J L
J L

Reputation: 87

How to provide a response example with this structure in OpenAPI 3?

Is it possible to provide a response example that is an object with an array attribute and a string attribute?

I have the following OpenAPI 3 definition:

        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {


                "examples": {
                  "results": [
                    {
                        "abc": 20
                    }
                  ],
                  "totalCount": 69
                },

                "schema": {
                  "results":"array",
                  "totalCount": "integer"
                }
              }
            }
          }
        }

But Swagger UI shows an error when I put "totalCount": 69 inside the examples object.

error image

Upvotes: 3

Views: 6773

Answers (1)

Helen
Helen

Reputation: 97677

Change examples (plural) to example (singular), and also provide a proper schema for the response.

  "responses": {
    "200": {
      "description": "OK",
      "content": {
        "application/json": {
          "example": {
            "results": [
              {
                "abc": 20
              }
            ],
            "totalCount": 69
          },

          "schema": {
            "type": "object",
            "properties": {
              "results": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "abc": {
                      "type": "integer"
                    }
                  }
                }
              },
              "totalCount": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  }

Upvotes: 3

Related Questions