Ensure each operation define at least one success response
Ensure that each operation in the OpenAPI document defines at least one success response. A success response is a response with a status code in the range of 200 to 299, indicating that the operation was completed successfully. A success response should have a description and a content property that defines the media type and the schema of the response body. Defining at least one success response for each operation helps both humans and computers to understand what kind of data they can expect from the API. It also helps to validate and document the API, as well as to generate client and server code from the OpenAPI document.
Risk Level: high
Platform: OpenAPI
Spectral Rule ID: OPENAPI020
REMEDIATION
Add or modify the responses property for each operation in the OpenAPI document and define at least one success response with a status code as declare in the table blow:
Operation | Status Codes |
---|---|
delete | 200, 201, 202, 204 |
patch | 200, 201, 202, 204 |
post | 200, 201, 202, 204 |
put | 200, 201, 202, 204 |
head | 200, 202 |
get | 200, 202 |
Status Description:
Status | Description |
---|---|
200 | OK |
201 | Created |
202 | Accepted |
204 | No Content |
For exmaple:
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
servers:
- url: https://api.example.com/v1
+ # Add or modify the responses property for each operation in the OpenAPI document
paths:
/pets:
get:
summary: List all pets
responses:
+ '200':
+ description: A list of pets
+ content:
+ application/json:
+ schema:
+ type: array
+ items:
+ $ref: '#/components/schemas/Pet'
post:
summary: Create a new pet
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
+ '201':
+ description: Pet created successfully
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Pet'
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ message:
+ type: string
Read more:
Updated over 1 year ago