[BUG][Java] Content mediatype is hardcoded in api.mustache
Created by: p-daniil
Hi! I'm trying to return String value. My OAS looks like this:
openapi: 3.0.0
info:
title: API
description: API
version: LATEST
tags:
- name: helloWorld
description: Hello World Api
paths:
/helloWorld:
get:
tags:
- helloWorld
responses:
'200':
description: OK
content:
'text/plain':
schema:
type: string
parameters:
- name: name
in: query
schema:
type: string
But it results in this method:
/**
* GET /helloWorld
*
* @param name (optional)
* @return OK (status code 200)
*/
@Operation(summary = "", tags={ "helloWorld", }, responses = { @ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))) })
@RequestMapping(
method = RequestMethod.GET,
value = "/helloWorld",
produces = { "text/plain" }
)
default ResponseEntity<String> helloWorldGet(@Parameter(name = "name", description = "") @Valid @RequestParam(value = "name", required = false) String name
) {
return getDelegate().helloWorldGet(name);
}
As you can see, @Content(mediaType = "application/json"...
Root of the problem is in this line, where mediatype is hardcoded as application/json:
https://github.com/OpenAPITools/openapi-generator/blob/dc1df25f29791724a0f187e6a6d7886de315652c/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache#L140
As a temporary solution, it can be replaced:
mediaType = "application/json"
on
mediaType = {{#produces}}"{{{mediaType}}}"{{/produces}}{{^produces}}"application/json"{{/produces}}
But mediaType can contain multiple values, separated with comma, because it used in @RequestMapping(produces={...})
.
True way to support multiple content types will be generate @Content
annotation in array, I suppose, because in @ApiResponse
annotation content is type of array:
/**
* An array containing descriptions of potential response payloads, for different media types.
*
* @return array of content
**/
Content[] content() default {};