make requestBody fields optional in swaggerUI - json

Using the following versions for my flask python project:
connexion==2.3.0
swagger-ui-bundle==0.0.5
OAS 3.0
I want to make all the requestBody fields optional, right now I have defined the json as shown below and I have assumed if you do not provide required field in the requestBody, then it should be taken as false and if I do not provide any values in swagger UI for these fields it should NOT generate curl request with those fields with -d option.
"openapi": "3.0.0",
"info": {
"description": "Dev",
"version": "1.0.0",
"title": "DEV-API",
"contact": {
"email": "dev#email.com"
},
"license": {
"name": "Dev",
"url": "https://opensource.org/licenses/MIT"
}
},
"servers": [
{
"url": "http://xx.yy.zz.a:8080"
}
],
"tags": [
{
"name": "Custom Event Post Request",
"description": "Example API for posting custom events"
}
],
"paths": {
"/api/v1/calls/{id}/{event-name}": {
"post": {
"tags": [
"Post Event"
],
"summary": "Post Custom Event to a call",
"operationId": "post_call_custom_event_data",
"parameters": [
{
"name": "id",
"in": "path",
"schema": {
"type": "string"
},
"required": true
},
{
"name": "event-name",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded":{
"schema": {
"type": "object",
"properties": {
"event1": {
"type":"boolean"
},
"event2":{
"type": "string"
},
"event3": {
"type":"string"
}
}
},
"encoding": {
"event2": {
"allowReserved": true
},
"event3": {
"allowReserved": true
}
}
}
}
},
"responses": {
"200": {
"description": "ok"
},
"400": {
"description": "Failed. Bad Post Data"
}
}
}
}
}
But it does generate with no Values as follows:
curl -X POST "http://xx.yy.zz.a:8080/api/v1/calls/5454/custom-event" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "event1=&event2=&document="
I am not sure how to make the requestBody fields/elements to appear as optional in swagger UI

Your API definition is correct. It's a limitation of Swagger UI that it always sends all form fields, including optional fields with empty values. This issue is tracked here:
https://github.com/swagger-api/swagger-ui/issues/5303

Related

Integromat - Dynamically render spec of a collection from an rpc

I am trying to dynamically render a spec (Specification) of a collection from an RPC. Can't get it to work. Here I have attached the code of both 'module->mappable parameters' and the 'remote procedure->communication' here.
module -> mappable parameters
[
{
"name": "birdId",
"type": "select",
"label": "Bird Name",
"required": true,
"options": {
"store": "rpc://selectbird",
"nested": [
{
"name": "variables",
"type": "collection",
"label": "Bird Variables",
"spec": [
"rpc://birdVariables"
]
}
]
}
}
]
remote procedure -> communication
{
"url": "/bird/get-variables",
"method": "POST",
"body": {
"birdId": "{{parameters.birdId}}"
},
"headers": {
"Authorization": "Apikey {{connection.apikey}}"
},
"response": {
"iterate":{
"container": "{{body.data}}"
},
"output": {
"name": "{{item.name}}",
"label": "{{item.label}}",
"type": "{{item.type}}"
}
}
}
Thanks in advance.
Just tried the following and it worked. According to Integromat's Docs you can use the wrapper directive for the rpc like so:
{
"url": "/bird/get-variables",
"method": "POST",
"body": {
"birdId": "{{parameters.birdId}}"
},
"headers": {
"Authorization": "Apikey {{connection.apikey}}"
},
"response": {
"iterate":"{{body.data}}",
"output": {
"name": "{{item.name}}",
"label": "{{item.label}}",
"type": "{{item.type}}"
},
"wrapper": [{
"name": "variables",
"type": "collection",
"label": "Bird Variables",
"spec": "{{output}}"
}]
}
}
Your mappable parameters would then look like:
[
{
"name": "birdId",
"type": "select",
"label": "Bird Name",
"required": true,
"options": {
"store": "rpc://selectbird",
"nested": "rpc://birdVariables"
}
}
]
Needing this myself. Pulling in custom fields that have different types but would like them all to show for the user to update customs fields or when creating a contact be able to update them. Not sure if best to have them all show or have a select drop down then let the user use the map for more than one.
Here is my response from a Get for custom fields. Could you show how my code should look. Got little confused as usualy look for add a value in the output and do you need two separate RPC's in integromat? Noticed your store and nested were different.
{
"customFields": [
{
"id": "5sCdYXDx5QBau2m2BxXC",
"name": "Your Experience",
"fieldKey": "contact.your_experience",
"dataType": "LARGE_TEXT",
"position": 0
},
{
"id": "RdrFtK2hIzJLmuwgBtAr",
"name": "Assisted by",
"fieldKey": "contact.assisted_by",
"dataType": "MULTIPLE_OPTIONS",
"position": 0,
"picklistOptions": [
"Tom",
"Jill",
"Rick"
]
},
{
"id": "uyjmfZwo0PCDJKg2uqrt",
"name": "Is contacted",
"fieldKey": "contact.is_contacted",
"dataType": "CHECKBOX",
"position": 0,
"picklistOptions": [
"I would like to be contacted"
]
}
]
}

Why does the "schema" disappear from the "responses" object when importing an OpenAPI definition?

I can successfully import the following OpenAPI definition into Azure API Management.
However, when I export the OpenAPI definition, the "schema" name has been removed from the "responses" object. As a result, developers in my portal are not shown a schema or example for this operation.
My API definition is valid and functions correctly if added to the official editor.
How can I prevent the schema from being stripped out?
{
"swagger": "2.0",
"info": {
"title": "Foo",
"description": "Foo",
"version": "1"
},
"host": "example.org",
"schemes": [
"https"
],
"paths": {
"/foo": {
"get": {
"summary": "List all foo.",
"responses": {
"200": {
"description": "Success.",
"schema": {
"$ref": "#/definitions/Foo"
}
}
}
}
}
},
"definitions": {
"Foo": {
"type": "object",
"properties": {
"example": {
"type": "string",
"description": "An example."
}
}
}
}
}
This problem seems to occur when the definition lacks a "produces" name, in either the root object, or in the operation object.
For example, the following definition should import successfully, and then export without the "schema" being stripped away. Note that the "produces" name has been added to the root object, in between "schemes" and "paths"
{
"swagger": "2.0",
"info": {
"title": "Foo",
"description": "Foo",
"version": "1"
},
"host": "example.org",
"schemes": [
"https"
],
"produces": ["application/json"]
"paths": {
"/foo": {
"get": {
"summary": "List all foo.",
"responses": {
"200": {
"description": "Success.",
"schema": {
"$ref": "#/definitions/Foo"
}
}
}
}
}
},
"definitions": {
"Foo": {
"type": "object",
"properties": {
"example": {
"type": "string",
"description": "An example."
}
}
}
}
}

Nested json as a request body Input using swagger-ui in node js

I am new to node js and swagger-ui.
I want to pass below input body to my web service using swagger-ui,
I am able to pass normal json like:
{
"username":"abc",
"password":"******"
}
but I want to give following input to my web service using swagger-ui.
{
"data":
{"username":"abc",
"password":"******"
}
}
my api-docs.json file is below:
{
"info": {
"title": "Node Swagger API",
"version": "1.0.0",
"description": "Demonstrating how to describe a RESTful API with Swagger"
},
"host": "localhost:5002",
"basePath": "/",
"swagger": "2.0",
"paths": {
"/login": {
"post": {
"description": "user login",
"produces": [
"application/json"
],
"parameters": [
{
"name": "userName",
"description": "username to get userType",
"required": true,
"type": "string",
"in": "formData"
},
{
"name": "password",
"description": "password to get userType",
"required": true,
"type": "string",
"in": "formData"
}
],
"responses": {
"0": {
"description": "Access token has been expired. Please login again."
},
"200": {
"description": "Successfully returns the response"
},
"400": {
"description": "Invalid input Parameters"
},
"401": {
"description": "Invalid Access Token"
},
"404": {
"description": "No data found"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {},
"responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}
So How can pass below input using swagger-ui to my web service:
{
"data":
{"username":"abc",
"password":"******"
}
}
Any Help will be appreciable.
Thanks In Advance.
I always use the yml format. Try this. should work. Now you will get the details in the req.body.
{
"info": {
"title": "Node Swagger API",
"version": "1.0.0",
"description": "Demonstrating how to describe a RESTful API with Swagger"
},
"host": "localhost:5002",
"basePath": "/",
"swagger": "2.0",
"paths": {
"/login": {
"post": {
"description": "user login",
"produces": [
"application/json"
],
"parameters": [
{
"name": "userDetails",
"description": "username to get userType",
"required": true,
"in": "body",
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"0": {
"description": "Access token has been expired. Please login again."
},
"200": {
"description": "Successfully returns the response"
},
"400": {
"description": "Invalid input Parameters"
},
"401": {
"description": "Invalid Access Token"
},
"404": {
"description": "No data found"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {
"User" : {
"type":"object",
"properties": {
"data": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
}
}
},
"responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}

Swagger UI returns "no content" in the Response Body, and Response Code 0

I'm newer to Swagger UI. I use swagger with Json. When the response is also Json. When clicking Try it Out, I see a correct Request URL, but the Swagger UI returns "no content" in the Response Body, and Response Code 0.
Mentioned that API's URL is not publicly accessible, but the site that hosts the Swagger UI is in the same network as the site that hosts the API.
I see this stack overflow question, but not found the solution.
My Swagger.Json file:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger for Rest API",
"description": "A sample API that uses a application as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "Swagger API team",
"email": "abc#gmail.com",
"url": "http://xxxx.com"
},
"license": {
"name": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
},
"host": "localhost:85xx",
"basePath": "/v1",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/test/{username}/{albumname}/{imagename}": {
"get": {
"description": "Returns all images from the system that the user has access to",
"operationId": "findface",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "tags to filter by",
"required": true,
"type": "string"
},
{
"name": "albumname",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
},
{
"name": "imagename",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/test1"
}
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
}
},
"definitions": {
"test1": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"errorModel": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
Please can any one help.
Thank in Advance.
It should be a CORS issue. You should add Access-Control-Allow-Origin: * into response headers.
Please refer: https://github.com/swagger-api/swagger-ui/blob/master/README.md#cors-support
It should be a CORS issue. You should add Access-Control-Allow-Origin: * into response headers.
Use this code in the startup class
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());

Post a json body with swagger

I would like to POST a json body with Swagger, like this :
curl -H "Content-Type: application/json" -X POST -d {"username":"foobar","password":"xxxxxxxxxxxxxxxxx", "email": "foo#bar.com"}' http://localhost/user/register
Currently, I have this definition :
"/auth/register": {
"post": {
"tags": [
"auth"
],
"summary": "Create a new user account",
"parameters": [
{
"name": "username",
"in": "query",
"description": "The username of the user",
"required": true,
"type": "string"
},
{
"name": "password",
"in": "query",
"description": "The password of the user",
"required": true,
"type": "string",
"format": "password"
},
{
"name": "email",
"in": "query",
"description": "The email of the user",
"required": true,
"type": "string",
"format": "email"
}
],
"responses": {
"201": {
"description": "The user account has been created",
"schema": {
"$ref": "#/definitions/User"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Errors"
}
}
}
}
}
But the data are sent in the URL. Here the generated curl provided by Swagger :
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://localhost/user/register?username=foobar&password=password&email=foo%40bar.com'
I understand that the query keywork is not good, but I didn't find the way to POST a JSON body. I tried formData but it didn't work.
You need to use the body parameter:
"parameters": [
{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": false,
"schema": {
"$ref": "#/definitions/Pet"
}
}
],
and #/definitions/Pet is defined as a model:
"Pet": {
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
},
"xml": {
"name": "Pet"
}
},
Ref: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore.json#L35-L43
OpenAPI/Swagger v2 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object
For OpenAPI v3 spec, body parameter has been deprecated. To define the HTTP payload, one needs to use the requestBody instead, e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.json#L39-L41
OpenAPI v3 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject