Swagger UI not loading Throwing 500 error - swagger

I am using spring doc openui for swagger doc. Below is the dependency I am using
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
I tried 1.6.13 version too.
No other extra config I used even url for swagger ui also default one.
when I tried to open this URL : localhost:8080/swagger-ui/index.html or localhost:8080/swagger-ui.html
It is returning 500 error
"{"error":{"description":"Duplicate key class Parameter{\n name: x-nodeId \n in header \n description:null \n required:true \n .... }, "code": 500, "instructions":"Contact Field Support"}}"
You can see same in Screenshot.
Updated :
The error is throwing for Header Parameters it seems. I have X-nodeId as header param for most of api's once I removed those. The error pointing to new header param, i.e
"{"error":{"description":"Duplicate key class Parameter{\n name: x-countryCode \n in header \n description:null \n required:true \n .... }, "code": 500, "instructions":"Contact Field Support"}}"

Related

Swagger UI does not show file upload option (and not only that) in OpenAPI 3.0.3 Request Body info

I was trying to add a file upload option for a POST request in Swagger UI by following the documentation.
The problem is this:
As you can see, Swagger UI does not render anything, neither for textual inputs nor for file inputs.
I tried to search on SO other answers about this but nothing.
This is the request body that I defined for the requestBody parameter of the path:
CreateTicketMessagePayload:
description: |-
Request multipart body required to proceed in the ticket message creation processes.
**Object part**:
```
{
"message": "Textual content of the message",
"private": "Flag that indicates the message is visible just for technicians"
}
```
**File part**:
"attachment": Message attachment file.
content:
multipart/form-data:
schema:
type: object
properties:
message:
type: string
example: This is a message
private:
type: boolean
example: false
attachment:
type: string
format: binary
What am I doing wrong?
In case of multipart/form-data requests, Swagger UI shows the inputs after you click "Try it out".
There's an existing enhancement request to display the inputs by default:
Display static documentation information for multipart properties in OpenAPI 3.0 files

Prevent default response code in Swagger/OpenAPI definition with NestJS

What I'm trying
I'm implementing a DELETE route using NestJS.
Since the route is only ever supposed to return a HTTP 204 No Content on successful deletion, utilizing the #nestjs/swagger decorators, I've annotated it as follows:
#ApiParam({
name: 'id',
required: true,
type: 'string',
example: 'abc123',
})
#ApiNoContentResponse({
description: 'All items for the given id have been deleted',
})
#Delete('/accounts/:id/items')
async deleteItems(
#Param('id') id: string,
) {
// do stuff
}
Stack
#nestjs/cli: 9.1.6
#nestjs/common: 9.1.6
#nestjs/core: 9.1.6
#nestjs/platform-express: 9.1.6
#nestjs/swagger: 6.1.2
nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": [
"#nestjs/swagger/plugin"
]
}
}
Question
How can I prevent this (default?) response from being generated?
What happens
When running the server with npm run start:dev, the swagger description for my route is created. Although I've not explicitly defined it, the description now contains an entry for HTTP 200 in the Response section of the route. The entry contains no description and no indicator that it's not explicitly defined.
What I expected to happen
When running the server with npm run start:dev, the swagger description for my route is created. The description only contains and entry for HTTP 204 in the Response section of the route.
What I've tried
Explicitly defined an #ApiOkResponse:
// they all have no effect
#ApiOkResponse()
// OR
#ApiOkResponse({})
// OR
#ApiOkResponse({ status: 204 })
Defined an #ApiDefaultResponse:
// they all create a response 'default' with no description
#ApiDefaultResponse()
// OR
#ApiDefaultResponse({})
// OR
#ApiDefaultResponse({ status: 204 })
You can just add #HttpCode(204) as an annotation to your code. 200 is added by default, because this is the default response status code for DELETE requests.This default behavior would be overwritten with the annotation mentioned above.
More information about the status codes can be found here: https://docs.nestjs.com/controllers

GrapeSwagger::Errors::UnregisteredParser: No parser registered for String

I'm adding a Rails API using Grape where the success type should be String. When I do this the swagger_doc no longer generates as there is no parser registered for String. How do I register a String parser? Thanks to anyone that can help!
Below is the description that is being used to generate the swagger_doc
desc 'generate pdf',
detail: '...',
body_name: 'pdf',
success: String,
consumes: %w[application/x-www-form-urlencoded],
failure: BaseAPI.document_errors([401, 403, 404])

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

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"
}
}
```

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.

Resources