Skip to content

[BUG][Java] Content mediatype is hardcoded in api.mustache #11511

@p-daniil

Description

@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:

@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = @Content(mediaType = "application/json", schema = @Schema(implementation = {{{baseType}}}.class)){{/baseType}}){{^-last}},{{/-last}}

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 {};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions