Swagger UI seems to not handle optional keys with POST json body - node.js

Packages :
hapi-swagger: 9.0.1
joi: 13.0.2
Context :
I use the swagger-ui with a swagger.json file generated by hapi-swagger.
I use for hapi-swagger the jsonEditor option set to true.
Which means that i don't enter myself the body in a single text area but i use directly the generated inputs of the UI.
Issue :
Only the key "name" is required in the payload, the other ones are optional by default if i refer to the Joi doc.
Actually the Swagger-UI sends :
curl -X POST
--header 'Content-Type: application/json'
--header 'Accept: application/json'
-d '{
"name":"fzef",
"idsUser": [],
"idsUsergroup":[]
}'
Instead i want that the Swagger-UI to send a request like :
curl -X POST
--header 'Content-Type: application/json'
--header 'Accept: application/json'
-d '{
"name":"fzef",
}'
Swagger UI
Joi Schema
Joi.object().keys({
request: Joi.object().keys({
name: Joi.string().required(),
idsUser: Joi.array().items(Joi.string()),
idsUsergroup: Joi.array().items(Joi.string()),
}),
}),
});
Swagger.json
...
"paths": {
"/addCompany": {
"post": {
"operationId": "postAddcompany",
"parameters": [{
"type": "string",
"default": "1",
"pattern": "/^v[0-9]+$/",
"name": "apiVersion",
"in": "path",
"required": true
},
{
"in": "body",
"name": "body",
"schema": {
"$ref": "#/definitions/Model 208"
}
}
],
"tags": ["api", "CompanyCommandsAPIPart"],
"responses": {
"default": {
"schema": {
"type": "string"
},
"description": "Successful"
},
}
}
}
}
"definitions": {
...
"Model 208": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"idsUser": {
"$ref": "#/definitions/Model 13",
"type": "array",
"x-alternatives": [{
"$ref": "#/x-alt-definitions/idsFunctionality",
"type": "array"
}, {
"type": "string"
}]
},
"idsUsergroup": {
"$ref": "#/definitions/Model 13",
"type": "array",
"x-alternatives": [{
"$ref": "#/x-alt-definitions/idsFunctionality",
"type": "array"
}, {
"type": "string"
}]
},
},
"required": ["name"]
},
...
}
What can i do to get this request body ?
Do i need to precise a joi method in order that the hapi-swagger parser add a parameter like 'optional' to the swagger.json ?
I found the same issue for the query of a GET method but found no solution:
https://github.com/swagger-api/swagger-editor/issues/684

The JSON Editor component provides the "Properties" and "Edit JSON" buttons to customize the JSON payload, as you can in the component demo here: http://jeremydorn.com/json-editor/. However, Swagger UI 2.x (the version used by hapi-swagger at the time of writing) initializes the JSON Editor with disable_properties: true and disable_edit_json: true so that these buttons are hidden, and the UI does not expose any configuration options to change the JSON Editor options. There is an open issue in the hapi-editor repository on GitHub: https://github.com/glennjones/hapi-swagger/issues/332.
A possible workaround is to tweak the Swagger UI code. Assuming your Swagger UI's index.html uses unminified swagger-ui.js, find the following lines in <hapi-swagger>/public/swaggerui/swagger-ui.js:
disable_properties:true,
disable_edit_json:true,
and replace them with:
disable_properties:false,
disable_edit_json:false,
Now the JSON Editor will have the "Object Properties" button. Click this button to select the properties to be displayed in the form editor and included in the request body. Unselected properties will not be sent in the request body.

Related

Multiple authorization using swagger OpenAPI 3.0 with Express and JSON format

The project has two authorization systems, basic auth and bearer. I need for each request after clicking on the "try it out" and "execute" buttons to attach to the request the Authorization headers, in which there will be a Basic line and a jwt header, in which there will be a bearer token. The problem is that I can attach these headers individually, but not together. There is a feeling that both authorizations want to write to the Authorization header and one of them overwrites the other, even though I explicitly indicated the header names in the schema.
My schemas:
{
"securitySchemes": {
"Bearer": {
"in": "header",
"name": "jwt",
"type": "http",
"scheme": "bearer"
},
"basicAuth": {
"type": "http",
"scheme": "basic"
}
}
}
and how I use it:
{
"/channel/base-list": {
"get": {
"tags": [
"CMS Channel"
],
"security": [
{
"Bearer": [],
"basicAuth": []
}
],
"summary": "Get _id and title of all channels",
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "count",
"required": false,
"schema": {
"type": "Integer"
},
"default": 25,
"example": 10
},
{
"in": "query",
"name": "search",
"required": false,
"schema": {
"type": "String"
},
"description": "Channel name"
}
],
"responses": {
"200": {
"description": "A list of channels",
"content": {
"application/json": {
"schema": {
"$ref": "#/definitions/get-channel-base-list"
}
}
}
}
}
}
}
}
I use swagger-ui-express for node.JS and OpenAPI 3.0
A request can contain only one Authorization header, and the Authorization header can only contain a single set of credentials (i.e. either Basic or Bearer, but not both). Your use case is not supported by the HTTP protocol.

How to send email with attachment using cURL with Azure Logic App

I am trying to build a simple Logic App in Azure, that sends an email with a zip attachment using cURL from a Linux VM.
In the Logic App, without the attachment it is working fine.
But it breaks once Attachment is configured.
JSON schema, When a HTTP request is received:
{
"properties": {
"Attachments": {
"type": "array"
},
"cc": {
"type": "string"
},
"from": {
"type": "string"
},
"html": {
"type": "string"
},
"subject": {
"type": "string"
},
"to": {
"type": "string"
}
},
"type": "object"
}
Config for Outlook Send an email v2, Logic App, Azure
Commands to send email from Linux VM:
# create attachment
echo "abcd" > test.txt
zip test.zip test.txt
# curl to send json to azure logic app, with attachment
curl -vvv --request POST \
--header 'Content-Type: application/json' \
--url 'https://prod-21.southeastasia.logic.azure.com:443/workflows/xxx/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=xxx' \
--data '{"from": "xxx#outlook.com", "cc": "xxx#outlook.com, "Attachments": [{ "Content-Type": "BASE64_ENCODED_CONTENT", "Name": "test.zip" }], "to": "xxx#outlook.com", "subject": "Hello, World!", "html": "Hey, test email." }'
Error message from the Run History, Logic App, Azure
Full error from the log:
{
"status": 400,
"message": "Parameter 'Attachment Content' cannot be null or empty.\r\nclientRequestId: 40d2dc9c-549b-4608-xxxxxxxxxxxxxxx",
"error": {
"message": "Parameter 'Attachment Content' cannot be null or empty."
},
"source": "office365-sea.azconn-sea.p.azurewebsites.net"
}
Would like to know how to handle the attachment on this scenario. Any documentation would be helpful as well.
Thank you.
Would be also interested in this topic!
Did you try to use --form instead of --data? Or do you already have a solution for this?
I also tried to specify the "Attachments" property in logic apps as:
"properties": {
"Attachment": {
"contentEncoding": "base64",
"contentMediaType": "image/png",
"type": "string"
},
}
...

How do I create a group tab on Kentico Kontent Management Api v2

So I have tried all sorts now.
I have tried this json
{
name: "Bla bla",
type: "group-tab",
external_id: "some_key_that_is_unique_gUID"
}
That did not work. It complains that group-tab is not a valid type. If I leave it off then it complains that the value is mandatory. The SDK allows me to omit the type.
How do I create a group and what is the valid type for a group tab.
I assume you want to create a content type with content groups, am I right?
If so, you need to send POST request to https://manage.kontent.ai/v2/projects/{project_id}/types with the body:
{
"external_id": "article",
"name": "Article",
"codename": "my_article",
"content_groups": [
{
"name": "Article copy",
"external_id": "article-copy"
},
{
"name": "Author",
"codename": "author"
}
],
"elements": [
{
"name": "Article title",
"codename": "title",
"type": "text",
"content_group": {
"external_id": "article-copy"
}
},
{
"name": "Article body",
"codename": "body",
"type": "rich_text",
"content_group": {
"external_id": "article-copy"
}
},
{
"name": "Author bio",
"codename": "bio",
"allowed_blocks": [
"images",
"text"
],
"type": "rich_text",
"content_group": {
"codename": "author"
}
}
]
}
Please note "content_groups":[...] field in the root of JSON as well as a specific content group for each element.
Also, do not forget to add auth header. You can find more info in the documentation.
Note: I assume (from your disclosed payload), the API is complaining correctly that group_tab is not a valid type, since you probably use the wrong endpoint - not sure here since you didn't post your whole request.
Creating a content group should occur while adding a new content type via the Management API v2 (documentation here: https://docs.kontent.ai/reference/management-api-v2#operation/add-a-content-type )
From the documentation example, "Article Copy" and "Author" content groups are added to the newly created "Article" content type, and the groups are referenced in each element to indicate which group the respective element should fall under.
In the example from the documentation, focus on:
"content_groups": [
{
"name": "Article copy",
"external_id": "article-copy"
},
{
"name": "Author",
"codename": "author"
}
],
in data and:
"content_group": {
"external_id": "article-copy"
}
for each element in the elements array.
curl --request POST \
--url https://manage.kontent.ai/v2/projects/<YOUR_PROJECT_ID>/types
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'Content-type: application/json' \
--data '
{
"external_id": "article",
"name": "Article",
"codename": "my_article",
"content_groups": [
{
"name": "Article copy",
"external_id": "article-copy"
},
{
"name": "Author",
"codename": "author"
}
],
"elements": [
{
"name": "Article title",
"codename": "title",
"type": "text",
"content_group": {
"external_id": "article-copy"
}
},
{
"name": "Article body",
"codename": "body",
"type": "rich_text",
"content_group": {
"external_id": "article-copy"
}
},
{
"name": "Author bio",
"codename": "bio",
"allowed_blocks": [
"images",
"text"
],
"type": "rich_text",
"content_group": {
"codename": "author"
}
}
]
}'

Swagger-ui 2.0 path do not get Bearer Authorization from top

when I check on swagger , my path "/customers" says "no headers authorization" whereas I have entered a Bearer token into the top right "Authorize" input. It is like the customers "security" prop is not linked to general securityDefinitions. In web Swagger, the padlock near GET /Customers is grey and open, when I click on it it does not show any "available authorization".(Postman version is working), I can't find why, could you help me please ?
In my swagger-ui (2.0)json file I got :
"securityDefinitions": {
"Bearer": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"description": "Enter your bearer token in the format **Bearer <token>**"
}
},
then below on the second path I entered "security" :
"/customers": {
"get": {
"tags": ["Customer Search"],
"summary": "find a customer by email, name, tel, ...",
"security": [{ "bearerAuth": [] }],
"parameters": [
{
"name": "email",
"in": "query",
"description": "email",
"required": true,
"type": "string"
},
{
"name": "realmCode",
"in": "query",
"description": "realmCode",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "send customer json file"
}
}
Here is the beginning of my node Express JS code for my route :
router.get("/customers", async (req: any, res: any) => {
if (!req.headers.authorization)
return res.status(401).json({ error: "no headers authorization" });
else {
```
OK I found it :
"security": [{ "Bearer": [] }],
instead of "bearerAuth" :)

swagger node ValidationError: "body" is required

I asked another question in another thread that gave me start on Swagger POST, but here I am stuck with another problem.
Here is my parameter definitions in my config json file
"/people/postTest": {
"post": {
"summary": "post test.",
"description": "Test.",
"operationId": "postTest",
"consumes": [
"application/json"
],
"tags": [
"test post"
],
"parameters": [
{
"in":"body",
"name":"body",
"description": "body for the POST request",
"required":true,
"schema": {"$ref":"#/definitions/inBody"}
}
] }}
"definitions": {
"inBody": {
"properties": {
"RequestSystem": {
"type": "string"
}}}}
(copied it from a big file so only took a small part but there is no issues with syntax)
and I am using CURL to post my request
curl -H "Content-Type: application/json" -X POST -d '{"RequestSystem":"IVR"}' http://localhost:8016/people/postTest
and this is the validation error I am getting when I try to start my node server
**ValidationError: "body" is required**
I know I am passing "RequestSystem" parameter but I am not sure where I am making mistakes in setting up my params in my config json file's "parameters". Any help would be appreciated.
I think you need to make required=false
[
{
"in":"body",
"name":"body",
"description": "body for the POST request",
"required":false,
"schema": {"$ref":"#/definitions/inBody"}
}
]
}
}
Try this It should work.

Resources