springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.27k stars 495 forks source link

Swagger UI with GET Request, not setting content-type on try it out #657

Closed thedude459 closed 4 years ago

thedude459 commented 4 years ago

Describe the bug Swagger UI populates accept header but not the content-type: curl -X GET "http://localhost:8080/tasks?jobId=A4990A6DE8D52489E05313108A0A7678&tradeDate=2020-05-06" -H "accept: application/json"

Content-type is populated on the response header with the error message: connection: keep-alive content-type: application/json date: Fri, 08 May 2020 12:32:29 GMT keep-alive: timeout=60 transfer-encoding: chunked

"timestamp": "2020-05-08T12:32:29.081+0000", "status": 415, "error": "Unsupported Media Type", "message": "Content type '' not supported",

Controller: @GetMapping(value = "/tasks", produces = "application/json", consumes = "application/json")

POM:

org.springdoc springdoc-openapi-ui 1.3.8
bnasslahsen commented 4 years ago

You can have a look at the answer here:

thedude459 commented 4 years ago

@bnasslahsen it is not populating the content type on post either.

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]

@PostMapping (value = "/tasks", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)

bnasslahsen commented 4 years ago

If you are passing no content and no parameters, there is no swagger content that will be generated.

You should add the swagger-annotation to tell explicitely that you want to have a RequestBody generated.

Here is a sample code:

@PostMapping(value = "/tasks", produces = "application/json", consumes = "application/json")
@RequestBody(description = "dummy description", content = @Content)
public Foo postMyRequestBody() {
    return null;
}
thedude459 commented 4 years ago

@bnasslahsen it is a post request with headers and query params but no request body. It is a post because I am change the state of the database but I don't need a request body.

@PostMapping (value = "/tasks", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> postTasks(@Parameter(required = true) @RequestHeader(value = "INDENTITY", required = true) Identity identity , @Parameter(required = true) @RequestHeader(value = "PRODUCT", required = true) Product product , @NotNull @Parameter(required = true) @Valid @RequestParam(value = "jobId", required = true) String jobId , @NotNull @Parameter(required = true) @Valid @RequestParam(value = "tradeDate", required = true) String tradeDate

)
bnasslahsen commented 4 years ago

@thedude459,

The same answer above applies. You need to add @RequestBody swagger annotation... If you are compliant OpenAPI 3, there is no way to pass the content-type without @RequestBody.

If you want enhancements on that, please feel free to submit your request to the OpenAPI github.

laxmanJun01 commented 3 years ago

It should accept and take request header key & value as mentioned in code , should not do remapping /renaming request / response headers at all., I don't know why its not taking properly even you give custom "Content-Type" (key) in GET request, its taking as "accept".. so weird ! Btw, custom Content-Type is very powerful security header for api endpoints to protect from un-authorised access from open world. This is so disappointing in using swagger, seems post-man & collection is much flexible & better options if you can do youself manually., No point using Swagger UI if failed to serve this proper secure rest api usage.

Swagger UI tech team better to focus on this to get Swagger UI useful for custom & secure apis !