I'm new to swagger documentation. We have an existing project developed in progress language for RESTFul Web Services. The different resource based urls consumes and produces in application/json format. The input and output json formats for one of our resource url is given below:
Request:
{
"request": {
"companyNumber": 5000,
"operatorInit": "sys",
"operatorPassword": "",
"customerNumber": 101,
"shipTo": "",
"warehouse": "01",
"productCode": "2-001",
"crossReferenceFlag": false,
"retrieveFlag": false,
"tInbinlocation": {
"t-inbinlocation": [
{
"binloc": "",
"icswbinloc1fl": false,
"icswbinloc2fl": false,
"addrecordfl": false,
"deleterecordfl": false,
"charuser": "",
"user1": "",
"user2": "",
"user3": "",
"user4": "",
"user5": "",
"user6": 0,
"user7": 0,
"user8": null,
"user9": null
}
]
},
"tInfieldvalue": {
"t-infieldvalue": [
{
"level": "",
"lineno": 0,
"seqno": 0,
"fieldname": "",
"fieldvalue": ""
}
]
}
}
}
Response:
{
"response": {
"cErrorMessage": "",
"crossReferenceProduct": "2-001",
"crossReferenceType": "",
"tOutbinlocation": {
"t-outbinlocation": []
},
"tOutfieldvalue": {
"t-outfieldvalue": []
}
}
}
How to convert above request and response json formats into swagger 2.0 json format?
Thanks!
Try using api-spec-converter.
this tool supports converting API descriptions between popular formats.
Supported formats:
* swagger_1
* swagger_2
* api_blueprint
* io_docs
* google
* raml
* wadl
Related
In Groovy I have to update values in JSON payload and make an API call. I am running into challenges while updating payload as the fields are embedded in backslash. Is there a simpler way to directly update the servers in below payload i.e update 1. JSON payload to 2. Updated JSON payload (updating name and host values).
1. JSON payload:
{
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
2. Updated JSON payload:
{
"environment": "dev",
"config": "Update",
"configType": "Server",
"ServerName": "",
"Servers": "[\\{\"name\":\"Server-test_2\",\"host\":\"test123.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
Tried below (losing backslash in conversion process):
Code:
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
jsonResp.Servers.name = "Server-test_2"
jsonResp.Servers.host = "test123.com"
Servers is a string in your initial json - you have to parse it
import groovy.json.*
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
def servers = parser.parseText(jsonResp.Servers)
servers[0].name="Server-test_2"
servers[0].host="test123.com"
jsonResp.Servers = JsonOutput.toJson(servers)
json = JsonOutput.prettyPrint(JsonOutput.toJson(jsonResp))
I am using graph API to add message rule which is forward a mail from user's inbox. Rule is getting added but mails are not forwarding to specified id.
Here are some details:
var data = {
"displayName": "From partner",
"sequence": 1,
"isEnabled": true,
"conditions": {
"isAutomaticForward": true
},
"actions": {
"forwardTo": [
{
"emailAddress": {
"name": "recipient name ",
"address": "email address"
}
}
],
"stopProcessingRules": true
}
}
axios.post("https://graph.microsoft.com/v1.0/users/{userId}/mailFolders/inbox/messageRules", data,
{
headers: {
"Authorization": "Bearer " + access_token
}
}
)
.then(response => {
console.log(response.data)
})
.catch(err => {
console.log(err.response)
})
Response is as expected but mails are not forwarding.
I tried the above payload, steps and it works for me!!
(1) Create a new rule using Graph API
POST https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules
Content-type: application/json
{
"displayName": "From partner",
"sequence": 2,
"isAutomaticForward": true,
"actions": {
"forwardTo": [
{
"emailAddress": {
"name": "Alex Wilbur",
"address": "AlexW#contoso.onmicrosoft.com"
}
}
],
"stopProcessingRules": true
}
}
(2) Test whether the rule is working or not.
Result: It's working as expected
(3) Check that the above rule shows or not (as part of Outlook.office.com or Outlook UI's rule section)
Adding a snapshot that i captured from Outlook.office.com, mailbox settings!!
hello everyone as i am using payment gateway charge credit api but i am getting error while passing json object to api.and here my original JSON object which pass to charge credit api.
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "XXXXXX",
"transactionKey": "XXXXXXXX"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "5",
"payment": {
"creditCard": {
"cardNumber": "5424000000000015",
"expirationDate": "2020-12",
"cardCode": "999"
}
}
}
}
}
this is original json request
but in iOS while making JSON we get below JSON object and sequence of JSON object are change that's why we getting error from api.
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "XXXXXX",
"transactionKey": "XXXXXXX"
},
"refId": "123456",
"transactionRequest": {
"amount": "5",
"payment": {
"creditCard": {
"cardCode": "999",
"cardNumber": "5424000000000015",
"expirationDate": "2020-12"
}
},
"transactionType": "authCaptureTransaction"
}
}
}
after passing this JSON object to API, we will get bellow error
{
"messages": {
"resultCode": "Error",
"message": [
{
"code": "E00003",
"text": "The element 'transactionRequest' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element 'amount' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. List of possible elements expected: 'transactionType' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'."
}
]
}
}
here is my code for creating JSON object in app
var dict = Dictionary<String, Any>()
dict=[
"merchantAuthentication": [
"name": "xxxxxxx",
"transactionKey": "xxxxxxx"
],
"refId": "5656",
"transactionRequest": [
"transactionType": "authCaptureTransaction",
"amount": "55",
"payment": [
"creditCard": [
"cardNumber": "4111111111111111",
"expirationDate": "2020-12",
"cardCode": "999"
]
]
]
]
after print this JSON sequence are changed
Have you read the documentation? It's very clear from the linked page that what you're running into is a side effect of their translation of JSON elements to XML elements in the backend, specifically around the ordering of parameters in your JSON request:
A Note Regarding JSON Support
The Authorize.Net API, which is not
based on REST, offers JSON support through a translation of JSON
elements to XML elements. While JSON does not typically require a set
order to the elements in an object, XML requires strict ordering.
Developers using the Authorize.Net API should force the ordering of
elements to match this API Reference.
Their examples below this block also show that their transactionType parameters appear as the first attribute in the transactionRequest object. Tl;dr - move your transactionType parameter up in your transactionRequest object:
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "XXXXXX",
"transactionKey": "XXXXXXX"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "5",
"payment": {
"creditCard": {
"cardCode": "999",
"cardNumber": "5424000000000015",
"expirationDate": "2020-12"
}
}
}
}
}
please try with following code.
let merchantSub = ["name": "XXXXXX",
"transactionKey": "XXXXXXX"]
let childCreditCard = ["cardCode": "999",
"cardNumber": "5424000000000015",
"expirationDate": "2020-12"]
let creditCard = ["creditCard":childCreditCard]
let transactionRequest = ["amount": "",
"payment":creditCard,
"transactionType":""] as [String : Any]
let merchantAuthentication = ["merchantAuthentication" :merchantSub,
"refId" : "123456",
"transactionRequest":transactionRequest] as [String : Any]
let param = ["createTransactionRequest" : merchantAuthentication]
if let data = try? JSONSerialization.data(withJSONObject: param, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str)
}
However I recommend to use class but for initial level do like this.
I upload an image to slack using https://slack.com/api/files.upload
not specifying channels field in the request.
I can see the image in the web interface. What the api call should be to share the uploaded image in some channel at some point in the future?
I tried to upload an image and response from slack was:
{
"ok": true,
"file": {
"id": "FHJ9QTX1V",
"created": 1554115093,
"timestamp": 1554115093,
"name": "scaled_IMG-20190324-WA0002.jpg",
"title": "scaled IMG-20190324-WA0002",
"mimetype": "image/jpeg",
"filetype": "jpg",
"pretty_type": "JPEG",
"user": "UGRR6FCF7",
"editable": false,
"size": 217356,
"mode": "hosted",
"is_external": false,
"external_type": "",
"is_public": false,
"public_url_shared": false,
"display_as_bot": false,
"username": "",
"url_private": "https://files.slack.com/files-pri/TGQU3SCHF-FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
"url_private_download": "https://files.slack.com/files-pri/TGQU3SCHF-FHJ9QTX1V/download/scaled_img-20190324-wa0002.jpg",
"thumb_64": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_64.jpg",
"thumb_80": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_80.jpg",
"thumb_360": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_360.jpg",
"thumb_360_w": 360,
"thumb_360_h": 270,
"thumb_480": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_480.jpg",
"thumb_480_w": 480,
"thumb_480_h": 360,
"thumb_160": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_160.jpg",
"thumb_720": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_720.jpg",
"thumb_720_w": 720,
"thumb_720_h": 540,
"thumb_800": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_800.jpg",
"thumb_800_w": 800,
"thumb_800_h": 600,
"thumb_960": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_960.jpg",
"thumb_960_w": 960,
"thumb_960_h": 720,
"thumb_1024": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_1024.jpg",
"thumb_1024_w": 1024,
"thumb_1024_h": 768,
"image_exif_rotation": 1,
"original_w": 1040,
"original_h": 780,
"permalink": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
"permalink_public": "https://slack-files.com/TGQU3SCHF-FHJ9QTX1V-3366c52c9c",
"comments_count": 0,
"is_starred": false,
"shares": {},
"channels": [],
"groups": [],
"ims": [],
"has_rich_preview": false
}
}
And then tried to share the image using /api/chat.postMessage:
{
"channel": "CH68ZSHFA",
"text": "test",
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "test"
}
},
{
"type": "image",
"image_url": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
"alt_text": "attachment"
}
],
"as_user": false,
"username": "Client name"
}
I used url from url_private, url_private_download, permalink, permalink_public fields of the file description but got the same response:
{
"ok": false,
"error": "invalid_blocks",
"response_metadata": {
"messages": [
"[ERROR] downloading image failed [json-pointer:/1/image_url]"
]
}
}
Unfortunately there is no official API method to share a file on your workspace after it has been uppladed. So if you don't include the channel(s) in your initial files.upload request there is no official way to share that file later on.
But there is an unofficial API method called files.share, which has that very functionality. It works perfectly, but you will need a legacy token to use it, so this might not be a solution for you.
Another way to use an image on Slack is to include it in a message (as you are trying in your code example). Technically speaking that is not the same as sharing a file on Slack, since it only works for images and provide limited functionality for users.
It will work though, but only if your image URL is public, because chat.postMessage only works with public URLs to images and files on Slack are private by default (which means you need to provide authorization in any request to access that file from outside of Slack).
To get a public URL for your file you can call the API method files.sharedPublicURL after you uploaded it. You will get a public URL as response, which you can then use to include that image in your message.
I'm using Swashbuckle to generate documentation for my ASP MVC Web API. Everything works great, except documentation to JsonResult. Here is an example. I have a test class:
public class Test
{
public string Testing { get; set; }
}
and method in controller:
[HttpGet]
public JsonResult<Test> GetTest()
{
Test test = new Test { Testing = "testing json" };
return Json(test);
}
Based on this configuration, Swagger generates example value:
{
"Content": {
"Testing": "string"
},
"SerializerSettings": {
"ReferenceLoopHandling": 0,
"MissingMemberHandling": 0,
"ObjectCreationHandling": 0,
"NullValueHandling": 0,
"DefaultValueHandling": 0,
"Converters": [{
"CanRead": true,
"CanWrite": true
}
],
"PreserveReferencesHandling": 0,
"TypeNameHandling": 0,
"MetadataPropertyHandling": 0,
"TypeNameAssemblyFormat": 0,
"TypeNameAssemblyFormatHandling": 0,
"ConstructorHandling": 0,
"ContractResolver": {},
"EqualityComparer": {},
"ReferenceResolver": {},
"ReferenceResolverProvider": {
"Method": {},
"Target": {}
},
"TraceWriter": {
"LevelFilter": 0
},
"Binder": {},
"SerializationBinder": {},
"Error": {},
"Context": {
"m_additionalContext": {},
"m_state": 1
},
"DateFormatString": "string",
"MaxDepth": 0,
"Formatting": 0,
"DateFormatHandling": 0,
"DateTimeZoneHandling": 0,
"DateParseHandling": 0,
"FloatFormatHandling": 0,
"FloatParseHandling": 0,
"StringEscapeHandling": 0,
"Culture": "string",
"CheckAdditionalContent": true
},
"Encoding": {
"m_codePage": 0,
"dataItem": {
"m_dataIndex": 0,
"m_uiFamilyCodePage": 0,
"m_webName": "string",
"m_headerName": "string",
"m_bodyName": "string",
"m_flags": 0
},
"m_isReadOnly": true,
"encoderFallback": {
"bIsMicrosoftBestFitFallback": true
},
"decoderFallback": {
"bIsMicrosoftBestFitFallback": true
}
},
"Request": {}
}
Which is incorrect. I don't want a documentation for JsonResult, but only for my Test class. How can I change that? How can I tell Swagger to prepare documentation only for Test class?
An option is be to use SwaggerResponse take a look at this sample:
[SwaggerResponse(400, "Bad request")]
public class SwaggerAnnotatedController : ApiController
{
[SwaggerResponseRemoveDefaults]
[SwaggerResponse(HttpStatusCode.Created, Type = typeof(int))]
[SwaggerResponse(HttpStatusCode.BadRequest, "Invalid message", typeof(HttpError))]
public int Create(Message message)
{
throw new NotImplementedException();
}
Here is a link to the project:
https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/SwaggerAnnotatedController.cs#L15