OpenAPITools / OpenAPITools/openapi-generator

Generating code from yaml file does not produce empty @Content

Open
#11,817 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

There is an responses for errors like 404 or similar without any body. But there is as well 200 response with body.
I know, that it's better to have body even for errors with clear messages, but here we do have legacy application with tens openapi yaml files.
Once generating interface with SpringCodegen the empty "@Content" is not generated within response annotation. This leads, that Springs swagger-ui shows for every error code default response content.

openapi-generator version 5.4.0, SpringCodegen
OpenAPI declaration
openapi: 3.0.1
info:
  title: Some API
  description: |-
    REST API for ...
  version: "1.0"
servers:
  - url: /some-api
tags:
  - name: SomeRoot
    description: Some Root services
paths:
  /:
    get:
      tags:
        - SomeRoot
      summary: Get some services
      description: |
        Some description
      operationId: listSomething
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SomeResource'
        401:
          description: Unauthorized
          content: {}
        403:
          description: Forbidden
          content: {}
        404:
          description: Not Found
          content: {}
      deprecated: false

components:
  schemas:
    SomeResource:
      type: object
      title: SomeResource

Generation Details

Simple maven generation with spring

Steps to reproduce

Run maven command to generate code.
Generated code looks like this:

@Operation(
        operationId = "listSomething",
        summary = "Get some services",
        tags = { "SomeRoot" },
        responses = {
            @ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  Object.class))),
            @ApiResponse(responseCode = "401", description = "Unauthorized"),
            @ApiResponse(responseCode = "403", description = "Forbidden"),
            @ApiResponse(responseCode = "404", description = "Not Found")
        }
    )
    @Parameters({
    })
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/",
        produces = { "application/json" }
    )
    ResponseEntity<Object> listSomething(
        
    );

See that ApiResponce for 401,403,404 does not have "content = @Content"

Suggest a fix

Fix https://github.com/OpenAPITools/openapi-generator/blob/v5.4.0/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache
Line 140. If {{#baseType}} is null, then use "content = @Content"

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

Start by reading modules/openapi-generator/src/main/resources/JavaSpring/api.mustache around line 140 and reproduce the issue with the provided YAML using Maven and SpringCodegen. Confirm that responses with content: {} generate content = @Content for 401, 403, and 404 while the 200 response remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.