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.91k stars 6.58k forks source link

[BUG] [Java JAXRS-Spec] Additional annotations not being added to models #7128

Open connor-butch opened 4 years ago

connor-butch commented 4 years ago
Description

It appears that the gradle open api plugin jax-rs-spec generator (java server-side api without an implementation) version 4.3.1 is not adding additional annotations on top of model classes. I would like to add the @Introspected annotation for use with a micronaut implementation of the jaxrs specification.

NOTE: I've added the field to other generation tasks (such as java client), and the annotations are added correctly, so maybe there could be some code reuse.... not sure.

Please find the code here: https://gitlab.com/connorbutch/micronaut

openapi-generator version

4.3.1 (latest at the time of submitting this)

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
Expected Output

I would expect the introspected annotation to be present at the top of the generated models (I generated this with pure java client for reference of what I would expect it to look like)

@io.micronaut.core.annotation.Introspected //<--------------------this is the line that is missing from the generated server spec @javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-08-04T17:01:02.684815-04:00[America/New_York]") public class Pet {

Actual Output

The output is the same, except that it is missing the annotations specified in the additional annotations section

//<------------------------------other annotation missing here @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2020-08-04T17:01:02.612149-04:00[America/New_York]") public class Pet {

Generation Details

run a gradle build in the root directory (gradlew clean build) https://gitlab.com/connorbutch/micronaut

here is the part of the build.gradle used to reproduce this:

task buildJaxRsServerApi(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) { //this has to go above compileJava.dependsOn generatorName = "jaxrs-spec" inputSpec = "open-api.yml" outputDir = "$projectDir/reading-comprehension-server-api" //NOTE: if there is a file you don't want overwritten, then add to the open-api ignore file (similar to gitignore or dockerignore) apiPackage = "com.connor.reading.api" modelPackage = "com.connor.reading.dto" configOptions = [ additionalModelTypeAnnotations: "@io.micronaut.core.annotation.Introspected", //TODO figure out why this isn't working.... this works for pure java client version, but not here //this is required for micronaut (as it allows for jackson to marshall to/from json without using reflection, since reflection doesn't work in graalvm) dateLibrary: "java8", interfaceOnly: "true", //we want interface only so we can implement with micronaut //generateBuilders: "true", //there is a bug in open api that prevents us using this returnResponse: "true", //this is preferred so we can return headers as well (when desired, such as for 201 for post to return location uri header) generatePom: "false" //prefer our own gradle build file ] }

Steps to reproduce

run a gradle build (gradlew clean build)

Related issues/PRs

Couldn't find any similar issues already open.

Suggest a fix

I am not sure -- it looks like the mustache template was merged on this pull request: https://github.com/OpenAPITools/openapi-generator/pull/4026/files#diff-55cbe2d778426ff755518ec18c0f8875

borsch commented 4 years ago

Hi @connor-butch I'll try to fix this

borsch commented 4 years ago

@connor-butch I have fixed issue and create PR here - https://github.com/OpenAPITools/openapi-generator/pull/7180 . Problem is that I have accidentally put import of model annotations in wrong place.

There is a workaround. I have created pull request to you repository - https://gitlab.com/connorbutch/micronaut/-/merge_requests/1/diffs

connor-butch commented 4 years ago

Thanks Borsch. You are awesome! I'm not sure how to upvote your contribution, but it looks to have worked.

borsch commented 4 years ago

@connor-butch no problem. could you please close the issue

Philzen commented 5 months ago

@connor-butch @borsch This issue can be closed, can't it?