Example Not Compliant With Schema Type (v3)

  • Query id: 881a6e71-c2a7-4fe2-b9c3-dfcf08895331
  • Query name: Example Not Compliant With Schema Type (v3)
  • Platform: OpenAPI
  • Severity: Info
  • Category: Best Practices
  • URL: Github

Description

Examples values and fields should be compliant with the schema type
Documentation

Code samples

Code samples with security vulnerabilities

Positive test num. 1 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MyObject"
                },
                "examples": {
                  "object": {
                    "$ref": "#/components/examples/objectExample"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "examples": {
      "objectExample": {
        "value": {
          "id": 1,
          "name": "new object"
        },
        "summary": "A sample object"
      }
    }
  }
}
Positive test num. 2 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
      parameters:
        - name: limit2
          in: body
          description: max records to return
          required: true
          schema:
            type: string
            example: 132
Positive test num. 3 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "number",
              "example": 132
            }
          }
        ]
      }
    }
  },
  "definitions": {
    "Tag": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "name": {
          "type": "string"
        }
      },
      "example": {
        "name": "Puma",
        "id": "1"
      }
    }
  }
}

Positive test num. 4 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
      parameters:
        - name: limit2
          in: body
          description: max records to return
          required: true
          schema:
            type: number
            example: 132
definitions:
  Tag:
    type: object
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
    example:
      name: "Puma"
      id: "1"
Positive test num. 5 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/MyObject"
              examples:
                object:
                  "$ref": "#/components/examples/objectExample"
components:
  schemas:
    MyObject:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
  examples:
    objectExample:
      value:
        id: 1
        name: new object
      summary: A sample object
Positive test num. 6 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/my_schema"
                },
                "examples": {
                  "foo": {
                    "value": "this is a string"
                  },
                  "foo_2": {
                    "value": true
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "my_schema": {
        "type": "string"
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Positive test num. 7 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/my_schema"
              examples:
                foo:
                  value: "this is a string"
                foo_2:
                  value: true
components:
  schemas:
    my_schema:
      type: string
security:
  - exampleSecurity: []
Positive test num. 8 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/my_schema"
                },
                "example": true
              }
            }
          },
          "400": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "integer"
                  }
                },
                "example": [
                  1,
                  2,
                  "3",
                  4
                ]
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "my_schema": {
        "type": "string"
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Positive test num. 9 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/my_schema"
              example: true
        "400":
          description: 200 response
          content:
            application/json:
              schema:
                type: array
                items:
                  type: integer
              example:
                - 1
                - 2
                - "3"
                - 4
components:
  schemas:
    my_schema:
      type: string
security:
  - exampleSecurity: []
Positive test num. 10 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "examples": {
                  "foo": {
                    "value": [
                      true,
                      "test2",
                      "test3"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Positive test num. 11 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
              examples:
                foo:
                  value:
                    - true
                    - "test2"
                    - "test3"
security:
  - exampleSecurity: []
Positive test num. 12 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "string",
              "example": 132
            }
          }
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "body",
      "description": "max records to return",
      "required": true,
      "schema": {
        "type": "string"
      }
    }
  }
}

Code samples without security vulnerabilities

Negative test num. 1 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MyObject"
                },
                "examples": {
                  "object": {
                    "$ref": "#/components/examples/objectExample"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "examples": {
      "objectExample": {
        "value": {
          "id": "1",
          "name": "new object"
        },
        "summary": "A sample object"
      }
    }
  }
}
Negative test num. 2 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "number",
              "example": 132
            }
          }
        ]
      }
    }
  },
  "definitions": {
    "Tag": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "name": {
          "type": "string"
        }
      },
      "example": {
        "name": "Puma",
        "id": 1
      }
    }
  }
}
Negative test num. 3 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
      parameters:
        - name: limit2
          in: body
          description: max records to return
          required: true
          schema:
            type: number
            example: 132
definitions:
  Tag:
    type: object
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
    example:
      name: "Puma"
      id: 1

Negative test num. 4 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/MyObject"
              examples:
                object:
                  "$ref": "#/components/examples/objectExample"
components:
  schemas:
    MyObject:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
  examples:
    objectExample:
      value:
        id: "1"
        name: new object
      summary: A sample object
Negative test num. 5 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/my_schema"
                },
                "examples": {
                  "foo": {
                    "value": "this is a string"
                  },
                  "foo_2": {
                    "value": "true"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "my_schema": {
        "type": "string"
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Negative test num. 6 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/my_schema"
              examples:
                foo:
                  value: "this is a string"
                foo_2:
                  value: "true"
components:
  schemas:
    my_schema:
      type: string
security:
  - exampleSecurity: []
Negative test num. 7 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/my_schema"
                },
                "example": "true"
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "my_schema": {
        "type": "string"
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Negative test num. 8 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/my_schema"
              example: "true"
components:
  schemas:
    my_schema:
      type: string
security:
  - exampleSecurity: []
Negative test num. 9 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "examples": {
                  "foo": {
                    "value": [
                      "true",
                      "test2",
                      "test3"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "security": [
    {
      "exampleSecurity": []
    }
  ]
}
Negative test num. 10 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
              examples:
                foo:
                  value:
                    - "true"
                    - "test2"
                    - "test3"
security:
  - exampleSecurity: []
Negative test num. 11 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
      parameters:
        - name: limit2
          in: body
          description: max records to return
          required: true
          schema:
            type: number
            example: 132