Swagger ui - Query param - java

I am using Swagger ui and Swagger core (1.3) for a jersey application. I have certain query parameters which I must send with every request like post, get, delete...
How can I default this ?

You can use the annotation #ApiParam from the Swagger annotations in order to configure the Query param to be used from the Swagger-UI.
For example
#Path("/{username}")
#ApiOperation(value = "Updated user")
public Response updateUser(
#ApiParam(value = "description for query-parameter") #QueryParam("username") String username
) {
...
}
Please, read more about this annotation in the following official documentation:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apiparam

You can't, but since swagger 2.0 (I don't know if this is supported by swagger-code/swagger-ui), you can defines parameters to be reuse across operations.
For example :
{
"parameters": {
"pageParam": {
"name": "page",
"in": "query",
"description": "page number to get",
"required": false,
"type": "integer",
"format": "int32"
}
},
"paths": {
"/customers": {
"get": {
"description": "Retrive list of customers",
"parameters": {
"$ref": "#/parameters/pageParam"
},
...
}
}
},
...
}

For swagger version 3, you can use the annotation #Parameter
import io.swagger.v3.oas.annotations.Parameter
https://docs.swagger.io/swagger-core/v2.0.0-RC3/apidocs/io/swagger/v3/oas/annotations/Parameter.html

Related

Add response headers using openapi-generator

I am trying to add "Content-Disposition" header to my api response. This api downloads a file. Here is how I configured the json specification file for openapi-generator (for java spring):
"/download": {
"get": {
"description": "Returns the file",
"operationId": "getFile",
"responses": {
"200": {
"description":"A file",
"headers": {
"content-disposition": {
"description": "Content disposition header",
"schema": {
"type": "string",
"example": "attachment; filename=keyFile.pem"
}
}
},
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
"400": {
"description": "Delete operation not allowed"
},
"404": {
"description": "File not found"
}
}
}
}
But the code generated by openapi-generator does not add the header to the response. The swagger UI also does not show any header in the response.
The answer here says that adding headers is not supported in spring generator for openapi-generator. Is this true? If yes then, is there an alternative to this?
Can somebody help me with this? I am pretty new to spring-boot and open-api.

Openapi generator angular not generating multipart formdata endpoint correctly. (useForm is false by default)

This is the endpoint I have:
#PostMapping(value = "/file-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<FileReference> handleFileUpload(
#RequestPart(value = "file", name = "file") MultipartFile[] file, #ApiIgnore HttpSession session) {
return service.handleFileUpload(
Arrays.stream(file).map(MultipartFileWithUUID::new).collect(Collectors.toList()),
session);
}
This is the generated endpoint in the swagger.json (swagger 2.0):
...
"post": {
"tags": [
"damage-report-controller"
],
"summary": "handleFileUpload",
"operationId": "handleFileUploadUsingPOST",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "file",
"in": "formData",
"required": false,
"type": "array",
"items": {
"type": "file"
},
"collectionFormat": "multi"
}
],
...
And here is the generated function:
public handleFileUploadUsingPOST(file?: Array<Blob> ...) {
let headers = this.defaultHeaders;
header settings etc...
// to determine the Content-Type header
const consumes: string[] = [
'multipart/form-data'
];
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): any; };
let useForm = false;
...
if (useForm) {
formParams = new FormData();
} else {
formParams = new HttpParams({encoder: this.encoder});
}
...
}
The error I have is 415: Unsupported media type.
I don't know how it should be generated correctly, but I changed let useForm; to true and it works,
so my guess that let useForm = canConsumeForm(consumes) because canConsumeForm returns a boolean.
What should I change so it gets generated correctly?
In case anyone reads this, I haven't found the proper solution using swagger 2.0, so I updated to openapi 3.0 and that fixed the problem.
Apparently swagger 2.0 doesn't support uploading an array of files, even though the only problem was that the generated service didn't use existing functions properly.

OpenAPI Generator : generate correct method profile for downloading binary file

I have a Swagger file (OAS 2) with an endpoint used to download a file. I'm using openapi-generator (through the Maven plugin) to generate Java / OpenFeign client code.
The problem is that this particular endpoint translates to:
#RequestLine("GET /files/{id}/content")
#Headers({
"Accept: application/octet-stream",
})
File downloadByIdUsingGET(#Param("id") String id);
which returns null when invoked.
According to this, the correct method profile should be:
Response downloadByIdUsingGET(#Param("id") String id);
And indeed if I write the interface myself, with this method I can use:
Response downloadResponse = api.downloadByIdUsingGET(id);
InputStream downloadInputStream = downloadResponse.body().asInputStream();
So the question is: how can I configure the generator to use the Response type for this endpoint?
I have tried:
<typeMappings>file=feign.Response</typeMappings>
but this substitutes File type with Response everywhere, including in other endpoints where I don't want it, particularly (guess what) the upload endpoint, which is generated as:
FileProperties uploadUsingPOST(#Param("file") File file);
and works fine like this.
Here's the relevant part of the Swagger file:
"/files/{id}/content": {
"get": {
"produces": [
"application/octet-stream"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string",
}
],
"responses": {
"200": {
"description": "Successful",
"schema": {
"type": "file"
}
}
}
}
}

How to pass formData for POST request in swagger.json?

In my play framework application, I have registered APIs in route file as:
POST /api/rmt-create-request controllers.Api.CreateRMTRequestForm
On action of controller, I am using following code to access formData submitted with form submit as :
public Result CreateRMTRequestForm()
{
Map<String, String[]> params = request().body().asMultipartFormData().asFormUrlEncoded();
Its working fine as API when I submit the form with forntend application.
I am trying to create APIs documentation with swagger.ui in which within swagger.json file I have written following JSON data.
"paths": {"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description" : "Return newly created request data",
"operationId": "create-new-rmt-request",
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"name": "rootNodeName",
"in": "formData",
"description": "Root node class name for item",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/rmt-request-data"
}
}
}
},
"default": {
"$ref": "#/components/responses/default"
}
}
}
},
While inspecting RequestHeader data, its not showing content-Type property with value 'multipart/form-data' as well as formData are not attached, which makes controller to throw null exception.
Can anyone help whats missing in swagger.json file ?
You are mixing OpenAPI 2.0 and 3.0 syntax.
In OpenAPI 3.0, request body (including form data) is defined using the requestBody keyword instead of in: formData parameters.
Also, OAS3 does not use consumes. The media types consumed by the operation are specified inside the requestBody.
"paths": {
"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description": "Return newly created request data",
"operationId": "create-new-rmt-request",
"requestBody": {
"content": {
"multipart/form-data": { // or "application/x-www-form-urlencoded" - depending on what you need
"schema": {
"type": "object",
"properties": {
"rootNodeName": {
"type": "string",
"description": "Root node class name for item"
}
}
}
}
}
}
}
}
}
More information: Describing Request Body

Modifying katharsis json response

I am using katharsis with spring boot.
I want to change the 'id' key in json response with my table PK column name, following is the katharsis response
{
"type": "table-name",
"id": "A",
"attributes": {
"description": "AAA"
},
"relationships": {
},
"links": {
"self": "http://localhost/table-name/A"
}
}
I want to change the above "id": "A" with "coulmnName":"A".
Answer on katharsis github repository
https://github.com/katharsis-project/katharsis-core/issues/203

Categories

Resources