I am trying to document an api error response with a example of the json body. I couldn't find an example or a fitting annotation. Playing around with the swagger editor I could at least get something that looks like the result I want to achieve.
responses:
'200' :
description: Request completed with no errors
examples:
application/json: {"result" : { "id": "blue" }}
libraries being is swagger-core 1.6.0
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<scope>compile</scope>
<version>1.6.0</version>
</dependency>
and the endpoints are created using jax-rs.
I did this to the endpoint
#ApiResponses(value = {
#ApiResponse(code = 200, message = "OK",
examples = #Example(value = #ExampleProperty(mediaType = "application/json", value = "{\"result\" : { \"id\": \"blue\" }}"))
)
})
public Response getResult(){}
The generated swagger.json doesn't have the desired
examples:
application/json: {"result" : { "id": "blue" }}
I tried also passing response = ApiResponse.class, Examples.class and Example.class but it doesn't change.
How can I do this with annotations?
Related
What I tried was
#ApiResponse(responseCode = "200", description = "Success", content = {#Content(mediaType = "application/json", schema = #Schema(implementation = ResponseEntity.class))})
but this is what I get under the response section in my apidoc. It seems like it's not reading the ResponseEntity class and not generating any schema.
Error-ModelName{namespace='org.springframework.http', name='ResponseEntity'}
and I am wondering if I can use openapi3 annotation to fix this.
What is the right format of the Response for Kinesis Firehose with http_endpoint as destination. Have already gone through the aws link:
https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html#responseformat
I have used the below lambda code in python(integrated in api) as well as with many other options, but keep getting the below error message. The test is performed using the "Test with Demo Data" option
sample code:
def lambda_handler(event, context):
data ={}
headersD = {}
headersD['content-length'] = 0
headersD['content-type'] = 'application/json'
data['requestId'] = 'ed4acda5-034f-9f42-bba1-f29aea6d7d8f'
data['timestamp'] = '1578090903599'
bodyDetail= {}
data['body'] = ''
data['headers'] =headersD
data['statusCode']=200
resp = json.dumps(data)
return resp
error response as seen in the logs:
The response received from the endpoint is invalid. See Troubleshooting HTTP Endpoints in the Firehose documentation for more information. Reason:. Response for request 'request-Id' is not recognized as valid JSON or has unexpected fields. Raw response received: 200 "HttpEndpoint.InvalidResponseFromDestination"
Here is the sample output that worked(in python):
responseBody = {
"requestId": "requestId",
"timestamp": 123456
}
resp = {
"headers": {"Content-Type": "application/json", "Content-Length": 100},
"body": json.dumps(responseBody),
"statusCode": 200
}
return resp
The swagger version I am using is as below and My question is related to the #ApiResponse annotation.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
When a user accesses my api and if the user provides invalid details/input, my application is returning a list of errors in the users input.
Each error is captured in a an error object
MyErrorObject
{
Date date;
String errorCode;
String errorMessage;
//getters //setters
}
-so my application response incase of invalid user input will look something like below -
[
{
"date": "2021-09-22",
"errorCode": "6548",
"errorMessage": "there are no booking available in selected city"
},
{
"date": "2021-09-22",
"errorCode": "2649",
"errorMessage": "your age does not allow you to travel to this location"
}
]
---Now back to my question - using swagger #ApiResponse annotation, I want the api users to know the response format. So, I tried below
#ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
message = ErrorConstants.NotAllowed, response=List<MyErrorObject>.class)
But above did not work, I am seeing below compile time error -
Syntax error, insert "}" to complete MemberValueArrayInitializer
I tried - other combinations too like below but no luck -
#ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST,
message = ErrorConstants.NotAllowed, response=List<MyErrorObject.class>)
So, what is the right way to represent a list of errors using #ApiResponse annotation??
So the error response on swagger doc looks something like below -
[
{
"date": "Date",
"errorCode": "string",
"errorMessage": "string"
}
]
From the definition of the ApiResponse.class, it's evident they have a responseContianer attribute that you can set to achieve the desired result.
#ApiResponse(
code = HttpURLConnection.HTTP_BAD_REQUEST,
message = ErrorConstants.NotAllowed,
response = MyErrorObject.class,
responseContainer = "List"
)
Unable to get example field values in swagger UI.
#ApiOperation(value = "Sample API to test swagger doc", response = SampleResponse.class)
#ApiImplicitParams({
#ApiImplicitParam(
name = "sampleResponse",
dataType = "SampleResponse",
examples = #io.swagger.annotations.Example(
value = {
#ExampleProperty(value = "{'code': '123', 'msg':'message'}", mediaType = "application/json")
}))
})
#ApiResponses(value = {
#ApiResponse(code = 200, response = SampleResponse.class, message = "Success", examples = #io.swagger.annotations.Example(
value = {
#ExampleProperty(value = "{'code': '123', 'message':'success'}", mediaType = "*/*")
})),
#ApiResponse(code = 401, message = "UnAuthorized"),
#ApiResponse(code = 400, message = "Invalid ID supplied", response = ApiResponse.class,
examples = #Example(value =
{
#ExampleProperty(mediaType = "application/json", value = "{\"code\" : \"42\", \"message\" : \"Invalid ID supplied\"}")
}
))
})
#PosMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public SampleResponse get(#ApiParam(required = true) #RequestBody SampleResponse sampleResponse) {
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Example defined with annotation #ExampleProperty are missing in UI.
In below image swagger UI example fields are shown with value as its default data type.
I couldn't find much in swagger2, but upgraded to openapi, where this can be achieved by using #io.swagger.v3.oas.annotations.parameters.RequestBody for defining request body and its examples and #io.swagger.v3.oas.annotations.responses.ApiResponse for response.
Whenever I attempt to make a PUT Request to Reddit API in order to add a friend, it fails and claims a JSON Parse Error 'JSON_PARSE_ERROR'. Nothing I do is working. Here is how I form the request.
Endpoint: /api/v1/me/friends/username
>>> Endpoint URL: PUT https://oauth.reddit.com/api/v1/me/friends/micheal
Authorization: Bearer <Access_Token>
// The response given:
{"fields": ["json"], "explanation": "unable to parse JSON data", "reason": "JSON_PARSE_ERROR"}
I have also tried the /api/friend/username endpoint and nothing works.
I had exactly the same problem, and your question led me to the solution.
The endpoint is expecting a json payload ACTUALLY NAMED "json." I'm not sure what language you're using, this is what it looks like in Node:
var options = {
url: 'https://oauth.reddit.com/api/v1/me/friends/mynewfriend',
headers: {
'User-Agent': 'Appname/1.0 by username',
'Authorization': "bearer " + <Access_Token>
},
json: {
'name': 'mynewfriend',
'notes': 'whatever notes you want to put',
}
};
request.put(options, function(error, response, body) {
blah blah blah
}
the json itself is described in https://www.reddit.com/dev/api/#PUT_api_v1_me_friends_{username}