question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to represent empty response body (HTTP 204)

See original GitHub issue

Description

In many of the APIs that I’m developing, we have cases where we reporting success does not require a response body (ex: deleting an entity, there is nothing to say if it works since the entity is gone.)

Unfortunately, it appears that swagger annotations cannot (currently) handle this.

Example:

Both this:

	@ApiResponses({
		@ApiResponse(code = 204, message = "identity deleted sucessfully",
					response = void.class),
		@ApiResponse(code = 404, message = "no such user or identity")
	})

and this:

	@ApiResponses({
		@ApiResponse(code = 204, message = "identity deleted sucessfully"),
		@ApiResponse(code = 404, message = "no such user or identity")
	})

produce the following spec (irrelevant portions omitted):

        "responses" : {
          "204" : {
            "description" : "identity deleted sucessfully",
            "schema" : {
              "type" : "object"
            }
          },
          "404" : {
            "description" : "no such user or identity"
          }
        }

It seems clear to me that this behavior is unarguably wrong, as void has a very well-understood meaning of, well… nothing. As it stands, ReDoc, swagger-ui, et al. will display the 204 response as though it will produce JSON consisting of {} (an empty object). This is incorrect.

Suggested resolution

When @ApiResponse#response is Void.class or void.class, omit the schema property in the resulting spec.

Additionally, consider adding special case handling to 204, since its meaning according to the HTTP spec strongly implies that there should be no response. (IIRC 404 already works like this unless you specify a response property for it.)

Workaround

None. Document incorrect behavior and warn your API documentation’s users.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
vipingupta5352commented, Jun 25, 2019

Found the workaround, As the default behavior of @ApiOperation is to return the return type of method, so for every status, the return type of method will be returned. If you want to send empty response then write @ApiResponse(code = 204, message = "No User found for that Id",response = Object.class)

And in the SwaggerConfig write

 @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.useDefaultResponseMessages(false)
        		.directModelSubstitute(Object.class, java.lang.Void.class);          
    }

So whenever an Object.class is returned swagger will automatically convert it into Void.class

2reactions
Orahpajocommented, Apr 8, 2019

I may be a bit late to the party, but the following workaround does the trick for me, in case anyone with the same issue is stumbling across this issue: @ApiResponse(code = 204, message = "identity deleted sucessfully", response = Object.class)

For me the problem was, that the @ApiResponse inherited the response type from @ApiOperation. So I had to override it with something empty. “Void.class” would seem like the right choice, but this is the default value, thus leading to the inheritance from @ApiOperation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is that possible to have 204 status code with non-empty ...
A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body. Go HTTP...
Read more >
204 No Content - HTTP - MDN Web Docs - Mozilla
The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate ......
Read more >
Rest API statusCode 204 with empty response - OutSystems
As 204 means empty response the consumer does not want to receive any ... I am not super familiar with swagger so can't...
Read more >
HTTP Status 204 (No Content) - REST
The 204 response MUST NOT include a message-body and thus is always terminated by the first empty line after the header fields.
Read more >
Failure to Publish Offers with Empty Response 204
Unfortunately, the publish call returns HTTP code 204 ("No Content") and the offer is not published. The body of the response is empty...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found