The OpenAPI definition below will generate the URL as http://localhost:9200/GetCourse?id=1. How to change the definition to generate the URL as http://localhost:9200/GetCourse/1?
/GetCourse:
get:
summary: Get Courses
description: >
The GetCourse details.
tags:
- Content information
parameters:
- name: id
in: query
description: The ID of the HEI being queried
required: true
type: integer
responses:
'200':
description: An array of courses
schema:
$ref: '#/definitions/Courses'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
Change the parameter location (in) from query to path.
Add /{id} at the end of the path name.
paths:
/GetCourse/{id}: # <----
get:
...
parameters:
- name: id
in: path # <----
description: The ID of the HEI being queried
required: true
type: integer
Related
I'm trying to add the request body to a swagger file, but when I use requestBody, it keeps saying Additional properties not allowed: requestBody I tried multple -in parameters like this
parameters
- in: body
name: email
description: The user to create.
schema:
$ref: "#/definitions/User"
- in: body
name: password
description: The user to create.
schema:
$ref: "#/definitions/User"
but then it says Operation cannot have multiple body parameters So I'm not sure how to reference all the req.body values. Also what if I had multiple body parameters and an /:id path as well?
I'm still really new to swagger so I appreciate any help with this.
swagger: "2.0"
info:
version: "1.0.0"
title: Hello World App during dev, should point to your local machine
basePath: /v1
schemes:
# tip: remove http to make production-grade
- http
- https
paths:
/user/signup:
x-swagger-router-controller: user
post:
description: signup POST
operationId: signup
parameters:
- in: body
name: email
description: The user to create.
schema:
$ref: "#/definitions/User"
- in: body
name: password
description: The user to create.
schema:
$ref: "#/definitions/User"
responses:
"200":
description: Success got all the listings
schema:
$ref: "/definitions/User"
"500":
description: Unexpected Error
schema:
type: object
properties:
message:
type: string
/user/login:
x-swagger-router-controller: user
post:
description: Login request
operationId: login
parameters:
- in: body
name: login
description: The user to create.
schema:
$ref: "#/definitions/Login"
responses:
"200":
description: Success got all the listings
schema:
$ref: "/definitions/Login"
"500":
description: Unexpected Error
schema:
type: object
properties:
message:
type: string
definitions:
User:
properties:
id:
type: integer
email:
type: string
password:
type: string
instagramName:
type: string
over21:
type: boolean
role:
type: string
fullName:
type: string
address1:
type: string
address2:
type: string
city:
type: string
state:
type: string
zip:
type: string
passwordCreated:
type: string
Login:
properties:
id:
type: string
email:
type: string
password:
type: string
You don't need multiple in: body parameters, you have them all defined already in the User schema (each request has just one body anyways).
That is exactly how it should be done. Just remove the second 'body' and maybe rename the other one:
parameters:
- in: body
name: user
description: The user to create.
schema:
$ref: "#/definitions/User"
If you require a path parameter you can define it as in: path. You need to add it to the path itself as well:
paths:
/user/signup/{id}:
x-swagger-router-controller: user
post:
description: signup POST
operationId: signup
parameters:
- in: path
name: id
description: User id
type: string
required: true
- in: body
name: user
description: The user to create.
schema:
$ref: "#/definitions/User"
In contradiction to in: body you can have multiple in: path parameters. Path parameters must include required: true.
I tried CRUD Operation in Swagger api using Node js. I tried PUT method using Swagger api, it's Successfully updated in db but it's throwing content type error. I tried many ways. I added application/json, application/xml, text/plain but it's still throwing same error. How to fix it can give me any solution.
Swagger.yaml
swagger: "2.0"
info:
version: "0.0.1"
title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/movies:
# binds a127 app logic to a route
x-swagger-router-controller: movies
post:
description: Creates a new movie entry
operationId: create
security:
- Bearer: []
parameters:
- name: movie
required: true
in: body
description: a new movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: a successfully stored movie details
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/movies/{id}:
x-swagger-router-controller: movies
get:
description: get movie
operationId: show
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
description: get particular movie details
type: string
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
put:
produces:
- '*/*'
description: Update Movie
operationId: update
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
type: string
- name: movie
required: true
in: body
description: an updated movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: Sucess
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
delete:
description: Delete Single Record
operationId: deleted
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
description: remove single record in db
type: string
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/login:
x-swagger-router-controller: movies
post:
description: Get Jwt Authentication Token
operationId: login
produces:
- application/json
- application/xml
parameters:
- name: Userdetails
required: true
in: body
description: Jwt Auth token
schema:
$ref: "#/definitions/LoginBody"
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/LoginBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/get:
x-swagger-router-controller: movies
get:
description: Get all Data
operationId: get
security:
- Bearer: []
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/MovieListBody"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
definitions:
MovieListBody:
required:
- id
- movies
properties:
id:
type: integer
movies:
type: array
items:
$ref: "#/definitions/Movie"
Movie:
required:
- title
- gener
- year
properties:
title:
type: string
gener:
type: string
year:
type: integer
Login:
required:
- id
- name
- company
properties:
id:
type: integer
name:
type: string
company:
type: string
MovieBody:
required:
- movies
properties:
movies:
$ref: "#/definitions/Movie"
LoginBody:
required:
- details
properties:
details:
$ref: "#/definitions/Login"
ErrorResponse:
required:
- message
properties:
message:
type: string
Facing Issue
Error: Response validation failed: invalid content type (text/html). These are valid: application/json, */*
Swagger.yml
swagger: "2.0"
info:
version: "0.0.1"
title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/movies:
# binds a127 app logic to a route
x-swagger-router-controller: movies
get:
description: Returns 'Hello' to the caller
# used as the method name of the controller
operationId: index
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/MovieListBody"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
post:
description: Creates a new movie entry
operationId: create
parameters:
- name: movie
required: true
in: body
description: a new movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: a successfully stored movie details
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
definitions:
MovieListBody:
required:
- movies
properties:
movies:
type: array
items:
$ref: "#/definitions/Movie"
Movie:
required:
- title
- gener
- year
properties:
title:
type: string
gener:
type: string
year:
type: integer
MovieBody:
required:
- movie
properties:
movie:
$ref: "#/definitions/Movie"
ErrorResponse:
required:
- message
properties:
message:
type: string
I Get this error:
Route defined in Swagger specification (/movies) but there is no defined post operation
I am new to this concept of Swagger API. I tried crud operation in Swagger API. The get method is working fine, but I tried post showing this type of issue. I tried step by step watching Swagger API videos. I tried get function is data successfully retrieved in db.but I tried post data to mongodb using Swagger API it's throwing this type of error....
How to fix it? Can anyone suggest any solution?
You don't need the /swagger node, just a post node at the same level as the get node under /movies path.
POST and GET are operations that can be performed on the 'movies' endpoint.
At present, your swagger supports GET /movies/ and POST /swagger/ as you've got a path called 'swagger'.
The structure should become:
paths:
/movies:
get:
# All the get properties
post:
# All the post properties
definitions:
# All the definitions you need
And here's an updated copy of your swagger:
swagger: "2.0"
info:
version: "0.0.1"
title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/movies:
# binds a127 app logic to a route
x-swagger-router-controller: movies
get:
description: Returns 'Hello' to the caller
# used as the method name of the controller
operationId: index
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/MovieListBody"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
post:
description: Creates a new movie entry
operationId: create
parameters:
- name: movie
required: true
in: body
description: a new movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: a successfully stored movie details
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
definitions:
MovieListBody:
required:
- movies
properties:
movies:
type: array
items:
$ref: "#/definitions/Movie"
Movie:
required:
- title
- gener
- year
properties:
title:
type: string
gener:
type: string
year:
type: integer
MovieBody:
required:
- movie
properties:
movie:
$ref: "#/definitions/Movie"
ErrorResponse:
required:
- message
properties:
message:
type: string
I am using swagger with Open API 3.0.0
Following is my Api definition:
paths:
/offerers:
get:
summary: give all offerers back
operationId: allOfferers
description: give you all offerers back
responses:
'200':
description: oferers results
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/OfferersId'
links:
GetSingleOffererById:
$ref: '#/components/links/GetSingleOffererById'
'400':
description: something went terribly wrong
/offerers/{offererId}:
parameters:
- in: path
name: offererId
schema:
$ref: '#/components/schemas/OfferersId'
required: true
get:
parameters:
- $ref: '#/components/schemas/OfferersId'
summary: give one specified offerer back
operationId: singleOfferer
description: give one offerer back. Specified by its id
responses:
'200':
description: offerers results
content:
application/json:
schema:
$ref: '#/components/schemas/Offerers'
'400':
description: something went terribly wrong
components:
schemas:
OfferersId:
type: number
example: 123
Offerers:
type: object
required:
- offererId
- name
properties:
id:
$ref: '#/components/schemas/OfferersId'
name:
type: string
example: "Mark Mustermann"
location:
type: string
example: "90449 Nürnberg"
experience:
type: string
example: "8 Jahre"
openingHours:
type: string
example: "Werktags: 10:15-18:30/tWochenende: geschlossen."
links:
GetSingleOffererById:
operationId: singleOfferer
description: the offererId in the response will be used as offererId in the request
paramters:
offererId: $reponse.body#/OfferersId
Most of this Definition is error free. But the last section components/links gives me an error at line "operationId: singleOfferer":
should NOT have additional properties additionalProperty: operationId,
paramters
So my question:
How do I have to correct my definition, so that the reusable link is valid?
"should NOT have additional properties" error in the Swagger Editor usually means one of the following:
a keyword is misspelt,
syntax/structure is incorrect.
In your example it's (1) - paraMTers should be paraMETers.
I Have this YAML code of swagger and I need to create my own type (named MyOwnType).
If I use "MyOwnType" a compilation error occurs.
paths:
/in/total:
get:
summary: My summary.
description: My description.
parameters:
- name: total
in: query
description: Total.
required: true
type: MyOwnType # -- THIS LINE OCCURS ERROR --
format: MyOwnType
responses:
201:
description: Respose
schema:
$ref: '#/definitions/MyOwnType'
definitions:
MyOwnType:
properties:
var:
type: string
description: data.
I created a definition "MyOwnType" and I can use like that: "$ref: '#/definitions/MyOwnType'" in schema.
But how can I use the "MyOwnType" definition on a parameter type?
A query parameter can not have a JSON Schema. If you want to have schema for your parameter you should change in of your parameter to body or formData and use schema key:
swagger: '2.0'
info:
version: 0.0.0
title: '<enter your title>'
paths:
/in/total:
get:
summary: My summary.
description: My description.
parameters:
- name: total
in: body
description: Total.
required: true
schema:
$ref: '#/definitions/MyOwnType'
responses:
201:
description: Respose
schema:
$ref: '#/definitions/MyOwnType'
definitions:
MyOwnType:
properties:
var:
type: string
description: data.