OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.56k stars 6.52k forks source link

[BUG] jaxrs-spec generator missing import in api class for subclass used in path parameter #12724

Open pbarnes-tibco opened 2 years ago

pbarnes-tibco commented 2 years ago
Description

I have a derived/subclass model generated using allOf, which is used in a path param in an API. The jaxrs-spec generator in v6.0.0 is generating the API class without an import for the derived class. It contains an import for the superclass instead. It will not compile.

In this case the subclass contains no members of its own, but the problem also occurs if it does have additional members.

openapi-generator version

6.0.0. Works in 5.4.0.

OpenAPI declaration file content
openapi: "3.0.3"
info:
  version: 1.0.0
  title: Bug demo
servers:
  - url: http://localhost:8080/
paths:
  /test/{derived}:
    post:
      operationId: generatorBug
      parameters:
        - name: derived
          in: path
          style: simple
          explode: true
          required: true
          schema:
            $ref: "#/components/schemas/Derived"
      responses:
        '200':
          description: Success
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

components:
  schemas:
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

    Base:
      type: object
      discriminator:
        propertyName: discriminator
      properties:
        name:
          type: string
        discriminator:
          type: string
      required:
        - name
        - discriminator
      additionalProperties: false

    Derived:
      allOf:
        - $ref: "#/components/schemas/Base"
Generation Details

Command line:

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v6.0.0 generate -i local/openapi.yaml -g jaxrs-spec -o /local/out/java
Steps to reproduce

The generated api class:

package org.openapitools.api;

import org.openapitools.model.Base;
import org.openapitools.model.Error;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import io.swagger.annotations.*;

import java.io.InputStream;
import java.util.Map;
import java.util.List;
import javax.validation.constraints.*;
import javax.validation.Valid;

@Path("/test/{derived}")
@Api(description = "the test API")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2022-06-28T13:56:54.632290Z[Etc/UTC]")
public class TestApi {

    @POST
    @Produces({ "application/json" })
    @ApiOperation(value = "", notes = "", response = Void.class, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Success", response = Void.class),
        @ApiResponse(code = 200, message = "unexpected error", response = Error.class)
    })
    public Response generatorBug(@PathParam("derived") Derived derived) {
        return Response.ok().entity("magic!").build();
    }
}
Related issues/PRs

Found none.

Suggest a fix

The generator should generate the class with the proper import, as in v 5.4.0.

oujesky commented 2 years ago

Seems same behaviour as in #12690

PirvuCatalin commented 2 years ago

Hi @pbarnes-tibco , I'm not sure if you saw in the generated java class, but you also have the following issue:

 @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Success", response = Void.class),
        @ApiResponse(code = 200, message = "unexpected error", response = Error.class)
    })

Basically, because you added "default" response type, you get this duplication for response "200". I'm facing the same issue and this leads to incorrect showing on the swagger ui. Do you guys know how to fix this?

pbarnes-tibco commented 2 years ago

Hi @pbarnes-tibco , I'm not sure if you saw in the generated java class, but you also have the following issue:

 @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Success", response = Void.class),
        @ApiResponse(code = 200, message = "unexpected error", response = Error.class)
    })

Basically, because you added "default" response type, you get this duplication for response "200". I'm facing the same issue and this leads to incorrect showing on the swagger ui. Do you guys know how to fix this?

I had not noticed that, and have not looked into it; not sure how to fix.

PirvuCatalin commented 2 years ago

I had not noticed that, and have not looked into it; not sure how to fix.

I actually checked everywhere and it turns out it's actually a bug and I corrected it in a PR at https://github.com/OpenAPITools/openapi-generator/pull/13021. For my use case, it was enough to generate (mvn clean install) the jar's on my machine and use this patched version.

Maybe you can also look into your issue.