I am using Ruby on Rails to architect a Rest API for a client and trying to document it by using SwaggerHub.
I would like to know how to document a post action with the correct parameters in the BODY. So far I have the action created and need to describe the following parameters for my developers.
{"user": {"email":"999999#gmail.com", "password":"12345678", "password_confirmation":"12345678"}}
I have started out with this for my post action:
"post": {
"description": "Creates a user",
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"responses": {
"201": {
"description": "Creates an instance of User",
},
"422": {
"description": "Bad syntax, user not created"
}
}
}
It's easy to document with the parameters " in PATH like the Get or Delete action which has the user ID in the params like this
"delete": {
"description": "Destroy a user",
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "destory a single user",
},
"404": {
"description": "user not found"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of user to use for action",
"required": true,
"type": "array",
"items": {
"type": "integer"
}
How can I document the parameters with params "in": "body" ?
You just need to add a parameters list within the post and add a parameter with in set to body.
swagger: "2.0"
info:
version: 1.0.0
title: Simple API
description: A parameter body example
paths:
/users:
post:
description: creates a user
produces:
- application/json
consumes:
- application/json
parameters:
- name: user
in: body
schema:
properties:
user:
properties:
email:
type: string
password:
type: string
password_confirmation:
type: string
responses:
201:
description: Creates an instance of User
422:
description: Bad syntax, user not created
You can find many information about the OpenAPI (fka. Swagger) Specification by reading the specification description and this tutorial (disclosure: I wrote it).
Related
I am new in swagger and want to deploy an API which is having query string. This is the API I am getting encoded URL after passing the parameter in the GET method.
API URL with Parameter should be:-
baseurl/v1/auth/getOTP?email=somename#email.com
but I am getting something like:-
baseurl/v1/auth/getOTP?email=somename%40email.com
what I have tried is:-
"/auth/getOTP": {
"get": {
"tags": [
"pet"
],
"summary": "",
"description": "",
"operationId": "findPetsByStatus",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "email",
"in": "path",
"description": "",
"required": true,
"type": "string",
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
},
"400": {
"description": "Invalid value"
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
},
Swagger OpenAPI has Specified: in this GitHub Issue-1840, It is specifically disallowed on in: path parameters for the same reason they don't allow optional path parameters, e.g. {/foo} By having variable path segments it become difficult to resolve a URL to an operation.
If you want to use some kind of hack then follow this GitHub Issue-230.
If you really needed then Its support in in: query parameters, as below,
The allowReserved keyword specifies whether the reserved characters :/?#[]#!$&'()*+,;= in parameter values are allowed to be sent as they are, or should be percent-encoded. By default, allowReserved is false, and reserved characters are percent-encoded.
Here you need to set it to true,
"parameters": [
{
"name": "email",
"in": "query",
"description": "",
"required": true,
"type": "string",
"allowReserved": true
}
],
Look at the Example Swagger Describing Parameters
and For more details follow Swagger Serialization.
Is there any way to set up example for request body in swagger 2.0?
My scheme is the same like in https://petstore.swagger.io/
My json param:
"parameters": [
{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": true,
"schema": {
"$ref": "#/definitions/Pet"
},
"x-example": "x-example": "{\r\n\"id\":123123123123123123,\r\n\"category\":{\r\n\"id\":44,\r\n\"name\":\"A12312312312312AA\"\r\n},\r\n\"name\":\"X123123123123XX\",\r\n\"photoUrls\":[\r\n\"st123123123ring\"\r\n],\r\n\"tags\":[\r\n{\r\n\"id\":0,\r\n\"name\":\"str123123ing\"\r\n}\r\n],\r\n\"status\":\"avai123123lable\"\r\n}""
}
],
I tried everything to setup my own example.
x-example:
default/(application/json):
and so on. No result.
I am working on swagger file, To create the authorizer in aws apigateway.But that time I 'll mention some function and api in swagger file.But it not effecting in the Apigateway.Once I deleted the stack,It will effect the APIgateway.If any changes is did that not effecting the APIgateway.Below I mentioning the template.yml file and swagger file.Please anyone will help to solve this issue.
template.yml file
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: TestApi
Resources:
ApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionUri: s3://devdeliforcemumbailambda/swagger-json-testapi.json
DriverDeleteF:
Type: AWS::Serverless::Function
Properties:
FunctionName: driver_delete_fun
Handler: index.handler
Runtime: nodejs8.10
CodeUri: build/authorizer.zip
Events:
GetApi:
Type: Api
Properties:
Path: /driver
Method: delete
swagger file content
{
"swagger": "2.0",
"info": {
"title": "demo"
},
"host": "rl0cg75uff.execute-api.ap-south-1.amazonaws.com",
"basePath": "/Prod",
"schemes": [
"https"
],
"paths": {
"/driver": {
"delete": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"security": [
{
"CognitoAuth": []
}
],
"x-amazon-apigateway-integration": {
"uri": "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-south-1:539977196287:function:driver_delete_fun/invocations",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavio r": "when_no_match",
"httpMethod": "POST",
"contentHandling": "CONVERT_TO_TEXT",
"type": "aws"
}
}
},
},
"securityDefinitions": {
"CognitoAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "cognito_user_pools",
"x-amazon-apigateway-authorizer": {
"providerARNs": [
"arn:aws:cognito-idp:ap-south-1:539977196287:userpool/ap-south-1_6j7axGXVm"
],
"type": "cognito_user_pools"
}
},
"lambdaAuth":{
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "custom",
"x-amazon-apigateway-authorizer": {
"authorizerUri": "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-south-1:539977196287:function:my-service-dev-hello/invocations",
"authorizerResultTtlInSeconds":1,
"identitySource": "method.request.header.Authorization",
"type": "request"
}
}
},
"definitions": {
"Empty": {
"type": "object",
"title": "Empty Schema"
}
}
}
Cloudformation does not compare the contents of your s3 object when you specify an S3 uri in DefinitionUri. You can instead specify a relative local path and have cloudformation cli upload it for you just as it usually does with function code. Cloudformation will then use your file hash in the s3 object key, hence having different DefinitionUri and CodeUri everytime they contain changes.
Therefore, instead of having these:
DefinitionUri: s3://devdeliforcemumbailambda/swagger-json-testapi.json
CodeUri: build/authorizer.zip
do this:
DefinitionUri: ./swagger-json-testapi.json
CodeUri: ./authorizer-code-directory
What I wrote is applicable to the following: (up to date docs here)
BodyS3Location property for the AWS::ApiGateway::RestApi resource
Code property for the AWS::Lambda::Function resource
CodeUri property for the AWS::Serverless::Function resource
DefinitionUri property for the AWS::Serverless::Api resource
SourceBundle property for the AWS::ElasticBeanstalk::ApplicationVersion resource
TemplateURL property for the AWS::CloudFormation::Stack resource
Deploy your stack as follows:
aws cloudformation package --template-file template.yaml --s3-bucket devdeliforcemumbailambda --output-template-file packaged-template.yaml
aws cloudformation deploy --capabilities CAPABILITY_NAMED_IAM --stack-name test-swagger --template-file packaged-template.yaml
Trying to send an email through Microsoft Graph where another email from user mailbox is to be used as an attachment.
What I am doing is something similar to:
POST https://graph.microsoft.com/v1.0/me/sendMail HTTP/1.1
authorization: bearer {access_token}
content-type: application/json
content-length: 96
{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [{
"emailAddress": {
"address": "garthf#a830edad9050849NDA1.onmicrosoft.com"
}
}],
"attachments": [
"#odata.type": "#Microsoft.OutlookServices.ItemAttachment",
"name": "menu.txt",
"item": {
"id": "some_id"
<!--This is the id of an existing email in User's inbox -->
}
]
},
"saveToSentItems": "false"
}
This returns with
Cannot process input of abstract type Microsoft.OutlookServices.Item
Can anyone help with suggestions?
Does anyone know how to define possible enum values in an OpenAPI 2.0 definition so that they will be displayed in the Model tab of Swagger UI? Example here has an enum option for the status property. How to do define such an enum in OpenAPI 2.0?
"enum" works like this in OpenAPI 2.0:
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"type": "string",
"enum": [ "1", "2"],
"required": true
}
and in OpenAPI 3.0:
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"schema": {
"type": "string",
"enum": [ "1", "2"]
},
"required": true
}
As you can see, there's a query parameter called sample of type string, and has an enum stating two possible values. In this case, the sample states the parameter is required, so the UI will not show an empty value as an option.
For a minimal working sample, try this:
{
"swagger": "2.0",
"info": {
"title": "title",
"description": "descriptor",
"version": "0.1"
},
"paths": {
"/sample": {
"post": {
"description": "sample",
"parameters": [
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"type": "string",
"enum": [
"1",
"2"
],
"required": true
}
],
"responses": {
"200": {
"description": "Successful request."
}
}
}
}
}
}
To test it locally, you can declare a variable (for example spec) in your javascript, and pass it into the SwaggerUi object.
var spec = { ... };
window.swaggerUi = new SwaggerUi({
url: url,
spec: spec,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
...
The url parameter will be ignored in this case.
Eventually, the output looks like this:
Updating this with YAML syntax.
OpenAPI 2.0:
parameters:
- in: query
name: sample
description: a sample parameter with an enum value
type: string
enum:
- 1
- 2
required: true
OpenAPI 3.0:
parameters:
- in: query
name: sample
description: a sample parameter with an enum value
schema:
type: string
enum:
- 1
- 2
required: true
This should work:
{
"name": "bookingType",
"in": "path",
"type": "array",
"items": {
"enum": [
"packages", "accommodations"
]
},
"description": "lorem ipsum"
}
Reference https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#itemsObject
This isn't the exact answer, but it might work for you until they add this functionality.
Simply declare the property like so
"MyObject":{
"properties":{
"MyEnum":{
"type":"Value1 or Value2 or Value3"
}
}
}
Your ModelSchema will show {}, but The Model will show MyEnum(Value1 or Value2 or Value3, optional)