OpenAPITools / OpenAPITools/openapi-generator

[BUG] [java] [maven] Swagger page does not contain api from java superclass

Open
#16,225 0 comments 0 reactions 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

I have a BaseController implementing general purpose api and concrete controllers extending it ( e.g.: AnswersController ).

public abstract class BaseController<V extends ViewModel>  {

  ...

  public ResponseEntity<List<V>> getAll() {
    return ...
  }
}
@RestController
public class AnswerController extends BaseController<AnswerDto> implements AnswersApi {

  // @Override
  // public ResponseEntity<List<AnswerDto>> getAll() {
  // return super.getAll();
  // }
}

AnswerController is implementing a interface generated by plugin. The interface contains all the general methods, as in example:

   @Operation(
        operationId = "getAll",
        summary = "GET -> retrieve all answers",
        responses = {
            @ApiResponse(responseCode = "200", description = "OK", content = {
                @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = AnswerDto.class)))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/answers",
        produces = { "application/json" }
    )
    default ResponseEntity<List<AnswerDto>> getAll(
        
    ) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "[ { ... } ]";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

In order to have in swagger page AnswerController.getAll, I have to uncomment the method in the controller. The inherited method from BaseController is not used to create documentation of the swagger page. Am I missing something?

openapi-generator version

<openapi.generator.version>6.3.0</openapi.generator.version>

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: project
  description: description
  version: 1.0.0

paths:

  /answers:
    get:
      summary: "GET -> retrieve all answers"
      operationId: "getAll"
      responses:
        "200":
          description: "OK"
          content:
            'application/json':
              schema:
                type: "array"
                items:
                  $ref: "#/components/schemas/AnswerDto"

components:
  schemas:

    BaseDto:
      properties:
        id:
          type: "integer"
          format: "int64"


    AnswerDto:
      allOf:
        - $ref: "#/components/schemas/BaseDto"
      x-implements: ['package.ViewModel']
      properties:
        note:
          type: "string"
        fkSupplierSurvey:
          type: "integer"
          format: "int64"
        fkPossibleAnswer:
          type: "integer"
          format: "int64"

Generation Details
<plugin>
   <groupId>org.openapitools</groupId>
   <artifactId>openapi-generator-maven-plugin</artifactId>
   <version>${openapi.generator.version}</version>
   <executions>
      <execution>
         <goals>
            <goal>generate</goal>
         </goals>
         <configuration>
            <skipValidateSpec>true</skipValidateSpec> <!-- allow  multiple openations with same id -->
            <inputSpec>${openapi.spec}</inputSpec>
            <generatorName>spring</generatorName>
            <library>spring-boot</library>
            <output>${generated.source.directory}</output>
            <apiPackage>${openapi.api.package}</apiPackage>
            <modelPackage>${openapi.model.package}</modelPackage>
            <generateSupportingFiles>true</generateSupportingFiles>
            <configOptions>
               <sourceFolder>/</sourceFolder>
               <oas3>true</oas3>
               <dateLibrary>java8</dateLibrary>
               <useSpringfox>false</useSpringfox>
               <escapeQuotationMark>true</escapeQuotationMark>
               <escapeUnsafeCharacters>true</escapeUnsafeCharacters>
               <useSpringController>true</useSpringController>
               <useSpringBoot3>true</useSpringBoot3>
               <interfaceOnly>true</interfaceOnly>
               <requestMappingMode>api_interface</requestMappingMode>
               <useBeanValidation>false</useBeanValidation>
            </configOptions>
            <typeMappings>
               <!-- convert Double to BigDecimal -->
               <typeMapping>Double=java.math.BigDecimal</typeMapping>
               <typeMapping>Boolean=boolean</typeMapping>
               <typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
            </typeMappings>
            <importMappings>
               <importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
            </importMappings>
         </configuration>
      </execution>
   </executions>
</plugin>
Steps to reproduce
Related issues/PRs
Suggest a fix

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 reproducing the issue with the Spring generator and the provided OpenAPI declaration and Maven configuration. Compare the generated AnswerController, AnswersApi, and BaseController behavior when the inherited method is overridden versus left inherited. Done means the Swagger page includes the inherited getAll operation without requiring an override.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.