Taking input as array in swagger not working - swagger

I have the following code for my swagger documentation. Here I am trying to create an API where I want to take input as an array of objects. so far I am doing like following.
{
method: 'POST',
path: '/api/Route',
handler: function(request, reply) {
//some operations
reply('Success');
},
config: {
description: 'Create a Route',
tags: ['api', 'user'],
auth: 'UserAuth',
validate: {
payload: {
"array": [{
userId: Joi.string().trim().required(),
status: Joi.number().required()
},
{
userId: Joi.string().trim().required(),
status: Joi.number().required()
},
{
userId: Joi.string().trim().required(),
status: Joi.number().required()
}
]
}
},
plugins: {
'hapi-swagger': {
responseMessages: swaggerDefaultResponseMessages
}
}
}
}
so what actually going on when I run the above code the swagger creates the documentation like this.
this is a link to the image. so please can anybody tell me why I am not getting the whole array in swagger documentation instead getting only one element of the array. and I also saw the Following question but unable to understand in which file they are making these changes. Could anybody help? Thanks in advance.

You could use the array validation of Joi. Then in the input, you could just pass the array into it. for that, you just need to write the following code in your payload instead of what you are writing at present.
payload: {
userData: Joi.array().items({
userName: Joi.string(),
status: Joi.string()
})
}
after that you swagger documentation looks something like this.
userData is your array of objects. and you could give input array of objects as following.
"userData": [{
"userName": "string", // objects
"status": "string"
},
{
"userName": "string", // objects this way you can add more
"status": "string"
}
]

Related

Jira API Set Pending Reason

Using the Jira API I'm trying to set a Pending Reason. I've dug out the following from the schema:
"customfield_15072":
{
"required":false,
"schema":
{
"type":"option",
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:select",
"customId":15072
},
"name":"Pending reason",
"key":"customfield_15072",
"operations":["set"],
"allowedValues":
[
{"self":"https://url/rest/api/2/customFieldOption/15100","value":"More info required","id":"15100"}
]
}
So I'm updating the following custom field using a PUT to the ticket (e.g. domain/issue/TICKET-234)
{"fields":{"customfield_15072":"15100"}}
But I keep getting told that this is is not valid for the customfield.
responseText:
`{"errorMessages":[],"errors":{"customfield_15072":"Specify a valid 'id' or 'name' for Pending reason"}}`,
Can anyone point me in the direction as to what I'm doing wrong?
Thanks
The data needed to be in this format:
{
"fields": {
"customfield_15072": {
"id": "15100"
}
}
}

Nested parameters on GET request

Question of the day: how to encode URL to be able to pass complex data on a GET request?
# data to pass
{
"main_key": {
"other_key": {
"main_array": [{
"name": "Bob",
"nickname": "bobby"
},
{
"name": "Tom",
"nickname": "Tommy"
}
]
}
}
}
Here is the current solution I got with Postman
Here is the current Rails interpretation of such a query, which is correct.
# Rails server side
Parameters: {"main_key"=>{"other_key"=>{"main_array"=>[{"name"=>"Bob", "nickname"=>"bobby"}, {"name"=>"Tom", "nickname"=>"tommy"}]}}, "default"=>{"format"=>:json}}
Can anybody have a better way to achieve a request with such a complex nested array object?
The other solution I got is to pass JSON directly as value of a query parameter and then parse it from the controller.
**Edit: ** I can pass this json on the body of the request but as it's a GET method, it does not respect XHR requirements.
If you have jquery, you can use .param() method of it.
let myParams = {
"main_key": {
"other_key": {
"main_array": [{
"name": "Bob",
"nickname": "bobby"
},
{
"name": "Tom",
"nickname": "Tommy"
}
]
}
}
}
console.log($.param(myParams));
This will give you your desired string.
"main_key%5Bother_key%5D%5Bmain_array%5D%5B0%5D%5Bname%5D=Bob&main_key%5Bother_key%5D%5Bmain_array%5D%5B0%5D%5Bnickname%5D=bobby&main_key%5Bother_key%5D%5Bmain_array%5D%5B1%5D%5Bname%5D=Tom&main_key%5Bother_key%5D%5Bmain_array%5D%5B1%5D%5Bnickname%5D=Tommy"

Is there a way to replace URL Link of Text in Google Docs API?

I started exploring Google Docs API in Python. It does pretty much everything I want it to do except for one thing.
I can replace the text of a document but I can't change the value of the hyperlinks.
Meaning if a link looks like this : a link, I can change the value of the text a link but not the target URL.
I've been going through the documentation but I can't find anything about it. Could it be a missing feature or am I missing the way to do that?
You can modify the hyperlink using UpdateTextStyleRequest of the batchupdate method in Google Docs API. At this time, please set the property of Link of TextStyle.
Endpoint
POST https://docs.googleapis.com/v1/documents/{file ID}:batchUpdate
Request body:
{
"requests": [
{
"updateTextStyle": {
"textStyle": {
"link": {
"url": "https://sampleUrl" # Please set the modified URL here.
}
},
"range": {
"startIndex": 1,
"endIndex": 2
},
"fields": "link"
}
}
]
}
Note:
From your question, I could understand that you have already used Google Docs API and you can modify the text of the link text. I think that you can modify the link using above request body and the script you have.
References:
UpdateTextStyleRequest
TextStyle
Link
If this was not useful for your situation, I apologize.
Edit:
You want to retrieve the text with the hyperlink.
From your reply comment, I could understand like above. When my understanding is correct, you can retrieve it using documents.get method. In this case, when fields is used, the response become to easily read.
Endpoint:
GET https://docs.googleapis.com/v1/documents/{file ID}?fields=body(content(paragraph(elements(endIndex%2CstartIndex%2CtextRun(content%2CtextStyle%2Flink%2Furl)))))
In this endpoint, body(content(paragraph(elements(endIndex,startIndex,textRun(content,textStyle/link/url))))) is used as fields.
Sample response:
For example, when the following texts are put in a Google Document and def has a hyperlink,
abc
def
The response is as follows. From the following result, you can retrieve the position of text with the hyperlink can be retrieved. Using this, you can modify the hyperlink.
{
"body": {
"content": [
{},
{
"paragraph": {
"elements": [
{
"startIndex": 1,
"endIndex": 5,
"textRun": {
"content": "abc\n",
"textStyle": {}
}
}
]
}
},
{
"paragraph": {
"elements": [
{
"startIndex": 5,
"endIndex": 8,
"textRun": {
"content": "def",
"textStyle": {
"link": {
"url": "https://sample/"
}
}
}
},
{
"startIndex": 8,
"endIndex": 9,
"textRun": {
"content": "\n",
"textStyle": {}
}
}
]
}
}
]
}
}
Reference:
documents.get
batchUpdate requires to know position of text, we can get document with all content and find positions of links
In my case I implement it as:
Copy template to new place with final name
Replace link texts and other parts of text
Get document
Find links positions in doc
Update link URLs
Here example in nodejs
const {google, docs_v1} = require('googleapis');
async function replaceInDoc(doc) {
let documentId = 'some doc id'
let auth = 'auth value for user'
let linkNewUrl = 'https://github.com/googleapis/google-api-nodejs-client'
google.options({auth: auth})
var docs = new docs_v1.Docs({}, google)
// document should have link with http://repo-url.com text, we will update it
var requests = [
{
replaceAllText: {
containsText: {
text: 'http://repo-url.com',
matchCase: true,
},
replaceText: linkNewUrl,
},
}
]
var updateRes = await docs.documents.batchUpdate({
documentId: documentId,
resource: {
requests: requests,
},
});
var docInfo = await docs.documents.get({documentId: documentId})
var linkPos = findLinksInDoc(docInfo)
// set new url to link by position of link in the document
var requests = [
{
updateTextStyle: {
textStyle: {
link: {
url: linkNewUrl
}
},
range: {
startIndex: linkPos[linkNewUrl][0],
endIndex: linkPos[linkNewUrl][1]
},
fields: "link"
}
}
]
var updateRes = await docs.documents.batchUpdate({
documentId: documentId,
resource: {
requests: requests,
},
});
}
// returns hash as { 'http://example.com': [startPosition, endPosition] }
function findLinksInDoc(doc) {
var links = {}
doc.data.body.content.forEach(section => {
if (section.paragraph) {
section.paragraph.elements.forEach(element => {
if (element.textRun && element.textRun.textStyle.link) {
links[element.textRun.content] = [element.startIndex, element.endIndex]
}
})
}
})
return links
}

JIRA API after POST returns { errorMessages: [ 'Internal server error' ], errors: {} }

I am trying to create a new issue utilizing the JIRA REST API and whenever I try, I get back the following generic error:
{ errorMessages: [ 'Internal server error' ], errors: {} }
I can successfully GET from the API, and the credentials I'm connecting with have full Admin access to JIRA (so it's not an Auth issue), but I get this error every time with POST. Below is a snippet of the JSON data I'm sending. Am I missing anything obvious?
Below is my JavaScript code. Note I'm using jira-connector from npm. (Real domain replaced with mydomain for this sample code)
const JiraClient = require('jira-connector');
const dotenv = require('dotenv').config();
function createNewIssue(fields) {
const encoded = process.env.JIRA_ENCODED_PW;
const jira = new JiraClient({
host: 'mydomain.atlassian.net',
basic_auth: {
base64: encoded
}
});
return new Promise((resolve, reject) => {
jira.issue.createIssue(fields, (error, issue) => {
if (error) {
console.log(error);
reject(error);
} else {
console.log(issue);
resolve(encoded);
}
});
})
}
Below is the JSON that's being passed into fields in the JS above. Note customfield_17300 is a radio button, and customfield_17300 is a multi-select box. For both cases, I've tried using the "id" and also the actual string "name" value. All IDs below were taken straight from a API GET of the same issue in question:
{
"fields": {
"project": {
"id": "13400"
},
"summary": "TEST API TICKET - 01",
"issuetype": {
"id": "11701"
},
"customfield_14804": { "id": "13716" },
"customfield_14607": "Hardware",
"customfield_17300": [
{
"id": "18322"
}
] ,
"customfield_16301": "Customer PO",
"customfield_14800": "LA, California",
"customfield_16302": "FEDEX 234982347g"
}
}
sigh I figured it out... other posts that said this cryptic error was due to a malformed JSON were correct.
In my route, I passed fields as coming from req.body.fields which actually dove into the fields values instead of passing it straight through. This made it so that when the JSON was sent to JIRA the fields outer wrapper was missing. I changed my route to pass along req.body instead of req.body.fields and all was well.
...that was a fun 4 hours...

Custom envelope fields using Docusign REST API

I'm using the docusign_rest gem to integrate with the Docusign REST API in my rails application. I have created a custom envelope field in the Docusign admin called SFID. I need to pass an ID into SFID inside of the envelope. I'm getting the following error with my JSON code:
{"errorCode"=>"INVALID_REQUEST_BODY", "message"=>"The request body is missing or improperly formatted. Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'API_REST.Models.v2.customFields' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'customFields', line 1, position 1073."}
My controller code:
#envelope_response = client.create_envelope_from_template(
status: 'sent',
email: {
subject: "The test email subject envelope",
body: ""
},
template_id: '90B58E8F-xxxxx',
custom_fields: [
{
textCustomFields: [
{
name: 'SFID',
value:'12345',
required: 'false',
show: 'true'
}
]
}
],
signers: [
...
The Docusign API explorer says the following is the correct way to push an envelope custom field:
{
"customFields": {
"textCustomFields": [
{
"value": "0101010101",
"required": "true",
"show": "true",
"name": "SFID"
},
{
"required": "true",
"show": "true"
}
]
}
}
The Docusign_rest gem says the following on custom envelope fields:
customFields - (Optional) A hash of listCustomFields and textCustomFields.
# Each contains an array of corresponding customField hashes.
# For details, please see: http://bit.ly/1FnmRJx
What formatting changes to I need to make to my controller code to get it to successfully push a custom envelope field?
You have an extra array in your customFields node.
Remove the [] array from your custom_fields:
#envelope_response = client.create_envelope_from_template(
status: 'sent',
email: {
subject: "The test email subject envelope",
body: ""
},
template_id: '90B58E8F-xxxxx',
custom_fields:
{
textCustomFields: [
{
name: 'SFID',
value:'12345',
required: 'false',
show: 'true'
}
]
},
signers: [
...
Also I'm assuming that your client.create_envelope_from_template is converting your _'s into a camelCased string. if that is not happening, then that also needs to change.

Resources