OpenAPITools / OpenAPITools/openapi-generator

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

Open
#11,511 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The affected template is modules/openapi-generator/src/main/resources/JavaSpring/api.mustache at the cited response annotation line; start by inspecting how produces and mediaType are populated. Regenerate the supplied OAS example and verify that @ApiResponse content reflects text/plain and handles multiple response media types without changing @RequestMapping(produces=...).

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.