OpenAPITools / OpenAPITools/openapi-generator
[BUG] jaxrs-spec generator missing import in api class for subclass used in path parameter
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
- Run the above command on the given yaml.
- Observe the generated TestApi.java and see that it contains an import for
Basebut notDerived.Derivedis correctly generated as a path param for thegeneratorBugoperation, but without the proper import, will not compile.
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied OpenAPI declaration and reproduce the issue using the jaxrs-spec generator command for v6.0.0. Compare the generated TestApi.java with the v5.4.0 output, focusing on imports for the Derived path parameter and the generated Base and Derived models. Done means the generated API imports Derived rather than only Base and compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100