Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #11511
Closed
Open
Issue created Feb 03, 2022 by Administrator@rootContributor

[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 {};
Assignee
Assign to
Time tracking