How to specify multiple 404 causes in OpenAPI (Swagger)? - swagger

I'm defining a path for a nested resource (content belonging to a delivery). If the client gets a 404 it could either because the delivery ID was not found, or the delivery did not contain any content of the specified type.
How do I model that using OpenAPI (YAML)?
I've got this right now...
paths:
'/deliveries/{id}/content/articles':
get:
summary: Retrieves articles from a delivery
description: Retrieves all articles from a single delivery
[...]
responses:
'200':
description: articles found
schema:
$ref: '#/definitions/Article'
'404':
description: delivery not found
schema:
$ref: '#/definitions/Error'
'404':
description: delivery did not contain any articles
schema:
$ref: '#/definitions/Error'
... but when I save the JSON from the Swagger Editor, it dropps the all 404 responses except the last one ("delivery did not contain any articles").

Multiple response types per status code are not allowed in OpenAPI/Swagger 2.0, but are supported in OpenAPI 3.0 by using oneOf.
In OpenAPI 2.0, you can only have a single schema for a 404 response:
responses:
'404':
description: delivery not found, or delivery did not contain any articles
schema:
$ref: '#/definitions/Error'
...
definitions:
Error:
type: object
properties:
status:
type: integer
type:
type: string
message:
type: string
where the Error payload can be, say:
{
"status": 404,
"type": "DeliveryNotFoundError",
"message": "delivery not found"
}
{
"status": 404,
"type": "NoArticlesInDeliveryError",
"message": "delivery did not contain any articles"
}

Related

Swagger openapi spec 3.0 resulting None is not of type object

I am trying to use openapi spec 3.0.
My login end point is specified as
/login:
post:
description: User Login.
operationId: swagger_server.controllers.default_controller.login_post
requestBody:
required: false
content:
application/json:
schema:
$ref: "#/components/schemas/Login"
description: Login object
parameters:
- name: Authorization
in: header
description: an authorization header
schema:
type: string
- name: x-api-key
in: header
description: an authorization header
schema:
type: string
responses:
"200":
description: Successfully logged in.
I am trying to define an endpoint where the requestbody is optional (I read that it is the default behavior). I seem to get an error None is not of type object. Is this expected behavior? What am I doing wrong? Appreciate your input.

Swagger editor is throwing error while specifying the response "Not a valid response definition"

I am trying to generate the API documentation using swagger editor. I specified my API specification as follow
paths:
/opendata/v1/{index}:
get:
tags: [verification]
description: Verify the person information
parameters:
- name: index
in: path
description: specific data index
required: true
type: string
- name: name
in: query
description: name of a person
required: false
type: string
- name: company name
in: query
description: name of a company
required: false
type: string
responses:
'200':
description: Success
content:
application/json:
schemas:
$ref: '#/responses/200'
responses:
'200':
description: Success
schema:
type: object
properties:
verification:
type: string
But its always showing error in the editor that "Not a valid response definition". I checked the specification for response from here. What changes I should do so that error will not come.
Note: I want the response in the json form like below:
{
verification:string
}
You are mixing OpenAPI/Swagger 2.0 and OpenAPI 3.0 syntax. Your spec seems to be swagger: '2.0', so you should use:
paths:
/opendata/v1/{index}:
get:
...
produces:
- application/json
responses:
200:
$ref: '#/responses/200'
Here's a related OpenAPI/Swagger 2.0 guide: Describing Responses

Swagger-codegen generated code having issues Routing requests with parameters in path

I created my api definition using swagger and have generated the server code using Swagger-tools in NodeJs. The SwaggerRouter handles all routes properly excepts the routes with ID.
For example , routes like
/v1/factoris/self
/v1/factoris/create
are directed to the right controller
but calls with IDs
/v1/factoris/{factoris_id}
are returned as invalid routes.
Any idea what I might be missing ?
Here is a sample of the swagger specs
/factoris/create:
post:
tags:
- "Factoris"
summary: "Create New Factori"
description: ""
operationId: "factorisCreatePOST"
parameters:
- in: "body"
name: "FactoriCreate"
description: "Create a new factory"
required: true
schema:
$ref: "#/definitions/FactoriCreate"
responses:
201:
description: "A single factori object"
schema:
$ref: "#/definitions/inline_response_201"
405:
description: "Method not allowed"
schema:
$ref: "#/definitions/inline_response_405"
security:
- oauth2:
- "admin"
x-swagger-router-controller: "Factoris"
/factoris/{factori-id}:
get:
tags:
- "Factoris"
summary: "Factori Information"
description: ""
operationId: "factorisFactori_idGET"
parameters:
- name: "factori-id"
in: "path"
description: "The factori identifier string"
required: true
type: "string"
- name: "expand"
in: "query"
description: "Provide expanded information on Assemblies or Products or Users"
required: true
type: "string"
responses:
200:
description: "A single factori object"
schema:
$ref: "#/definitions/inline_response_201"
404:
description: "Factori not found"
schema:
$ref: "#/definitions/inline_response_404"
security:
- oauth2:
- "codeadmin"
x-swagger-router-controller: "Factoris"
After some trial and error, I figured out that path parameter does not work if its has a "-" (factori-id)
so changing factori-id to factori_id fixed it. But its still a bug since the path parameter cannot handle "-"
here is the open issue created in Swagger-codegen
https://github.com/swagger-api/swagger-codegen/issues/5763

Swagger: Additional properties not allowed: allOf

I'm trying to figure out this swagger API inheritance stuff by using allOf. This is my swagger yaml file.
swagger: '2.0'
info:
title: Test API
version: '1'
basePath: /api/v1
schemes:
- https
produces:
- application/json
paths:
/users:
get:
summary: Collection of users
tags:
- users
responses:
200:
description: A list of Users
schema:
$ref: "#/definitions/Users"
500:
$ref: "#/responses/BadRequest"
definitions:
User:
required:
- username
properties:
firstName:
type: string
lastName:
type: string
username:
type: string
Users:
type: array
items:
$ref: "#/definitions/User"
responses:
NonSuccess:
description: Generic response for all non-success responses
schema:
type: object
required:
- code
- message
properties:
code:
type: integer
description: The success code, 0 or -1.
message:
type: string
description: The description message for this success code
errors:
type: array
description: A map of errors within the request. Keyed by the parameter name and the values are the error details
BadRequest:
description: Invalid request parameters
allOf:
- $ref: "#/responses/NonSuccess"
When I paste this into the online editor, I get the following errors that I'm having a real hard time trying to figure out.
✖ Swagger Error
Additional properties not allowed: allOf
Jump to line 60
✖ Swagger Error
Not a valid response definition
Jump to line 22
The main problem seems to be Additional properties not allowed: allOf and I'm can't seem to figure out what I'm doing wrong in this case. I was trying to declare a basic non-success response so that all non-200 responses will inherit so that the API will have a very standard looking non-success response. I was under the impression I could do this with allOf and then add or overwrite the fields from that response. What exactly am I doing wrong?
The allOf tag can only be used on Schema objects. You can definitely use it on the Schema portion of the response, though. Here's an example of that.
swagger: '2.0'
info:
title: Test API
version: '1'
basePath: /api/v1
schemes:
- https
produces:
- application/json
paths:
/users:
get:
summary: Collection of users
tags:
- users
responses:
200:
description: A list of Users
schema:
$ref: "#/definitions/Users"
500:
$ref: "#/responses/BadRequest"
definitions:
User:
required:
- username
properties:
firstName:
type: string
lastName:
type: string
username:
type: string
Users:
type: array
items:
$ref: "#/definitions/User"
Response:
type: object
required:
- code
- message
properties:
code:
type: integer
description: The success code, 0 or -1.
message:
type: string
description: The description message for this success code
errors:
type: array
description: A map of errors within the request. Keyed by the parameter name and the values are the error details
BadRequest:
type: object
required:
- validationErrors
properties:
validationErrors:
type: array
items:
type: string
responses:
NonSuccess:
description: Generic response for a non-success
schema:
$ref: "#/definitions/Response"
BadRequest:
description: Invalid request parameters
schema:
allOf:
- $ref: "#/definitions/Response"
- $ref: "#/definitions/BadRequest"

How do I refactor this Swagger API Spec

I have a few endpoints where I have some standard error responses like 404, 401, 403 and default. I want to refactor these responses to a Swagger definition but I am not able to achieve this. I tried a few tricks but it always resulted in parsing errors. Here is the yaml I have.
swagger: '2.0'
info:
title: My API
description: My API description
version: 0.0.1
host: api.example.com
schemes:
- https
basePath: /
produces:
- application/json
paths:
/users:
get:
operationId: getUsers
summary: Get users
description: Get all users
tags:
- Users
responses:
'200':
description: An array of users
schema:
type: array
items:
$ref: '#/definitions/User'
'401':
description: Authentication required
schema:
$ref: '#/definitions/Error'
'402':
description: Payment required
schema:
$ref: '#/definitions/Error'
'403':
description: Unauthorized access
schema:
$ref: '#/definitions/Error'
'404':
description: Not found
schema:
$ref: '#/definitions/Error'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
/games:
get:
operationId: getGames
summary: Get games
description: Get all games
tags:
- Games
responses:
'200':
description: An array of games
schema:
type: array
items:
$ref: '#/definitions/Game'
'401':
description: Authentication required
schema:
$ref: '#/definitions/Error'
'402':
description: Payment required
schema:
$ref: '#/definitions/Error'
'403':
description: Unauthorized access
schema:
$ref: '#/definitions/Error'
'404':
description: Not found
schema:
$ref: '#/definitions/Error'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
User:
type: object
properties:
id:
type: integer
description: Unique id of user
name:
type: string
description: Name of user
Game:
type: object
properties:
id:
type: integer
description: Unique id of game
name:
type: string
description: Name of game
Error:
type: object
properties:
code:
type: integer
description: HTTP status code
message:
type: string
description: Message describing error
Observe the repeating responses in /users and /games. How do I refactor and move them to definitions?
You can use shared responses object to define the responses. Then refer them in the individual operations. From the spec about shared responses object:
An object to hold responses to be reused across operations. Response
definitions can be referenced to the ones defined here.
While this would be a valid spec, Swagger-UI won't be able to parse from the shared responses except default response. Here is the issue related to this.

Resources