How to format code blocks in descriptions in Swagger UI 3.x? - swagger

I would like to put a Markdown code block in the description of my API but the Swagger UI seems to be reading as though it was a single line code snippet. I currently have:
description: |
This API was created to allow interaction with the Image Status Database (ISD)
## Requests
## Responses
In the case of a successful response, you will always receive a `data` key
that contains your data.
```
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}
```
This gets displayed as:
The Swagger Editor, however, displays the proper code block:
Is this not supported by the Swagger UI?

The code block formatting issue was fixed in Swagger UI 3.22.0 and Swagger Editor 3.6.26. Code blocks are displayed properly in these versions:
Note the line break between "a data key" and "that contains" in the text - it is caused by the | literal block style, which preserves the line breaks in YAML multi-line strings. To avoid that line break you can either 1) remove it in your YAML, or 2) use the > folded style and also indent the code block (to prevent it from being folded), as shown below:
description: >
This API was created to allow interaction with the Image Status Database (ISD)
## Requests
## Responses
In the case of a successful response, you will always receive a `data` key
that contains your data.
```
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}
```

Related

Graph API Unable to read JSON request payload

In power automate using Invoke an HTTP request, I am unable to make a graph API call for email sent(Post: https://graph.microsoft.com/v1.0/me/sendMail) with a customized email body (HTML elements and tags). It does not allow the below custom HTML code and errors as "Unable to read JSON request payload. Please ensure the Content-Type header is set and payload is of valid JSON format."  Please find the code below. In addition, HTML code is
Please find the code below.
And HTML Code is
This error occurs because your payload is not in a valid json format. You have to escape the quotation marks. Use \" intead of ".
{
"data": "<html><head>The title attribute example</head><body><h3 title=\"Hello HTML!\">Titled head tag example</h3></body></html>"
}
Like #Optimal already mentioned. It is good to escape the quotation marks of the HTML Body value.
However, I see the request body also has a saveToSentItems boolean property. Can you try it with that boolean as well?
Try something like below:
{
"message": {
"subject": "#{triggerBody()['text']}",
"body": {
"contentType": "HTML",
"content": "#{triggerBody()['text_1']}"
},
"toRecipients": [
{
"emailAddress": {
"address": "#{triggerBody()['text_2']}"
}
}
]
},
"saveToSentItems": "true"
}

Why is the exact difference between "violation" and "deny" in OPA/Rego?

In Open Policy Agent (https://www.openpolicyagent.org/)
regarding to Kubernetes, depending which engine is used:
Gatekeeper: https://github.com/open-policy-agent/gatekeeper
OR
Plain OPA with kube-mgmt: https://www.openpolicyagent.org/docs/latest/kubernetes-introduction/#how-does-it-work-with-plain-opa-and-kube-mgmt
There are different ways to define validation rules:
In Gatekeeper the violation is used. See sample rules here: https://github.com/open-policy-agent/gatekeeper-library/tree/master/library/general
In plain OPA samples, the deny rule, see sample here:
https://www.openpolicyagent.org/docs/latest/kubernetes-introduction/#how-does-it-work-with-plain-opa-and-kube-mgmt
It seems to be the OPA constraint framework defines it as violation:
https://github.com/open-policy-agent/frameworks/tree/master/constraint#rule-schema
So what is the exact "story" behind this, why it is not consistent between the different engines?
Notes:
This doc reflects on this: https://www.openshift.com/blog/better-kubernetes-security-with-open-policy-agent-opa-part-2
Here is mentioned how to support interoperability in the script: https://github.com/open-policy-agent/gatekeeper/issues/1168#issuecomment-794759747
https://github.com/open-policy-agent/gatekeeper/issues/168 In this issue is the migration mentioned, is just because of "dry run" support?.
Plain OPA has no opinion on how you choose to name your rules. Using deny is just a convention in the tutorial. The real Kubernetes admission review response is going to look something like this:
{
"kind": "AdmissionReview",
"apiVersion": "admission.k8s.io/v1beta1",
"response": {
"allowed": false,
"status": {
"reason": "container image refers to illegal registry (must be hooli.com)"
}
}
}
So whatever you choose to name your rules the response will need to be transformed into a response like the above before it's sent back to the Kubernetes API server. If you scroll down a bit in the Detailed Admission Control Flow section of the Kubernetes primer docs, you'll see how this transformation is accomplished in the system.main rule:
package system
import data.kubernetes.admission
main = {
"apiVersion": "admission.k8s.io/v1beta1",
"kind": "AdmissionReview",
"response": response,
}
default response = {"allowed": true}
response = {
"allowed": false,
"status": {
"reason": reason,
},
} {
reason = concat(", ", admission.deny)
reason != ""
}
Note in particular how the "reason" attribute is just built by concatenating all the strings found in admission.deny:
reason = concat(", ", admission.deny)
If you'd rather use violation or some other rule name using plain OPA, this is where you would change it.

Swagger UI not showing responses examples

While using swagger-editor I create the below YAML that shows an example of the response object. It's properly displaying in the swagger-editor. When I then download the JSON and display it in swagger-ui, the example is completely missing.
/person/{email}/create:
post:
summary: Create a new account
tags:
- Person
parameters:
...
responses:
201:
description: The new SQL ident and sport details
examples:
application/json: |
[
12,
[
{
"sql_idnet" : 12,
"name" : "Basketball"
},
{
"sql_ident" : 13,
"name" : "Ice Hockey"
}
]
]
This might be because the response does not have a schema - in Swagger this means the response does not have a body.
That said, Swagger UI 3.0 displays this example correctly.
Swagger UI and Swagger Editor currently do not support multiple examples. You can follow this issue for updates.

Rails: Pretty print JSON without overkill

I'm using the below code to display an unauthorized message in JSON:
def render_unauthorized
# Displays the Unauthorized message since the user did
# not pass proper authentication parameters.
self.headers['WWW-Authenticate'] = 'Token realm="API"'
render json: {
error: {
type: "unauthorized",
message: "This page cannot be accessed without a valid API key."
}
}, status: 401
end
Which outputs:
{"error":{"type":"unauthorized","message":"This page cannot be accessed without a valid API key."}}
So my question is this: Is there a way to pretty print this message (WITHOUT putting it in a separate view and using some 3rd party gem).
Edit: What is pretty print?
Properly spaced, and well .. pretty. Here's the output I'd like to see:
{
"error": {
"type": "unauthorized",
"message": "This page cannot be accessed without a valid API key."
}
}
Solution
Using #emaillenin's answer below worked. For the record, here's what the final code looks like (since he hadn't included the whole thing):
def render_unauthorized
# Displays the Unauthorized message since the user did
# not pass proper authentication parameters.
self.headers['WWW-Authenticate'] = 'Token realm="API"'
render json: JSON.pretty_generate({ # <-- Here it is :')
error: {
type: "unauthorized",
message: "This page cannot be accessed without a valid API key."
}
}), status: 401
end
Use this helper method built into JSON.
JSON.pretty_generate
I just tried and it works:
> str = '{"error":{"type":"unauthorized","message":"This page cannot be accessed without a valid API key."}}'
> JSON.pretty_generate(JSON.parse(str))
=> "{\n \"error\": {\n \"type\": \"unauthorized\",\n \"message\": \"This page cannot be accessed without a valid API key.\"\n }\n}"
If you (like I) find that the pretty_generate option built into Ruby's JSON library is not "pretty" enough, I recommend my own NeatJSON gem for your formatting.
To use it gem install neatjson and then use JSON.neat_generate instead of JSON.pretty_generate.
Like Ruby's pp it will keep objects and arrays on one line when they fit, but wrap to multiple as needed. For example:
{
"navigation.createroute.poi":[
{"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
{"text":"Take me to the airport","params":{"poi":"airport"}},
{"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
{"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
{"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
{
"text":"Go to the Hilton by the Airport",
"params":{"poi":"Hilton","location":"Airport"}
},
{
"text":"Take me to the Fry's in Fresno",
"params":{"poi":"Fry's","location":"Fresno"}
}
],
"navigation.eta":[
{"text":"When will we get there?"},
{"text":"When will I arrive?"},
{"text":"What time will I get to the destination?"},
{"text":"What time will I reach the destination?"},
{"text":"What time will it be when I arrive?"}
]
}
It also supports a variety of formatting options to further customize your output. For example, how many spaces before/after colons? Before/after commas? Inside the brackets of arrays and objects? Do you want to sort the keys of your object? Do you want the colons to all be lined up?
Using the aligned option would allow your output to look like this:
{
"error": {
"type" : "unauthorized",
"message" : "This page cannot be accessed without a valid API key."
}
}

"additional properties" error in Survey Monkey API call

I'm trying to use the create_flow endpoint to the Survey Monkey API. It is sending back a status 3 message with the following error:
additional properties not defined by 'properties' are not allowed in field '_data'
I'm able to do successfully use all other API endpoints and have a valid API key and durable OAuth token.
Here's an example JSON body that I'm sending to: https://api.surveymonkey.net/v2/batch/create_flow?api_key=apikeyhere
{
"survey": {
"template_id": "566",
"survey_title": "test1",
"collector": {
"type": "email",
"name": "collector1",
"recipients": [
{
"email": "email#example.com"
}
]
},
"email_message": {
"reply_email": "myemail#example.com",
"subject": "this is a test"
}
}
Note: JSON formatting here is being generated automatically using RJSONIO
Any ideas what might be causing the error? It seems like all fields are correctly named and where they're supposed to be, so I'm not sure what the problem is.
It's a bad error message unfortunately - it's a known issue. It means you are providing extra keys that are not part of the create_flow schema.
The issue here is that the "email_message" and "collector" keys have been nested inside of "survey", instead of being in the main JSON body like the "survey" key. Move them out a level and it should work.

Resources