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.98k stars 6.6k forks source link

[BUG] Generated Scala Play Server has several import issues #7371

Closed linde9821 closed 4 years ago

linde9821 commented 4 years ago

Bug Report Checklist

Description

I want to create a simple "news api" in scala-play where the server stub is generated with the openapi scala-play-server generator. The problem is that several issues occure when trying to compile the server. Here is a part of the stack trace:

[info] Loading global plugins from C:\Users\lindn\.sbt\1.0\plugins
[info] Loading settings for project api-build from plugins.sbt ...
[info] Loading project definition from D:\Programmieren\Effort\newsapi\api\project
[info] Updating ProjectRef(uri("file:/D:/Programmieren/Effort/newsapi/api/project/"), "api-build")...
[info] Done updating.
[warn] There may be incompatibilities among your library dependencies.
[warn] Run 'evicted' to see detailed eviction warnings
[info] Loading settings for project api from build.sbt ...
[info] Set current project to newsapi (in build file:/D:/Programmieren/Effort/newsapi/api/)
[info] sbt server started at local:sbt-server-e4a2a2df9369e122ec0c
[newsapi] $ run

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Enter to stop and go back to the console...)

[info] Compiling 14 Scala sources and 2 Java sources to D:\Programmieren\Effort\newsapi\api\target\scala-2.12\classes ...
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:3:28: not found: type DefaultHttpErrorHandler
[error] class ErrorHandler extends DefaultHttpErrorHandler {
[error]                            ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:5:47: not found: type Future
[error]                                e: Throwable): Future[Result] = e match {
[error]                                               ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:4:41: not found: type RequestHeader
[error]     override def onServerError(request: RequestHeader,
[error]                                         ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:7:13: not found: value Future
[error]             Future.successful(BadRequest(e.getMessage))
[error]             ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:7:31: not found: value BadRequest
[error]             Future.successful(BadRequest(e.getMessage))
[error]                               ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\ErrorHandler.scala:8:17: not found: type JsResultException
[error]         case _: JsResultException =>

...

[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\generated\model\News.scala:21:39: not found: type Format
[error]     implicit lazy val newsJsonFormat: Format[News] = Json.format[News]
[error]                                       ^
[error] D:\Programmieren\Effort\newsapi\api\app\de\hwrBerlin\lehre\effort\newsapi\generated\model\News.scala:21:54: not found: value Json
[error]     implicit lazy val newsJsonFormat: Format[News] = Json.format[News]
[error]                                                      ^
[error] 49 errors found
[error] (Compile / compileIncremental) Compilation failed
[error] application -

! @7h3p4mn3k - Internal server error, for (GET) [/] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[not found: type DefaultHttpErrorHandler]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
        at scala.Option.map(Option.scala:146)
        at play.sbt.run.PlayReload$.$anonfun$taskFailureHandler$1(PlayReload.scala:33)
        at scala.Option.map(Option.scala:146)
        at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:28)
        at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:24)
        at play.sbt.run.PlayReload$.$anonfun$compile$3(PlayReload.scala:51)
        at scala.util.Either$LeftProjection.map(Either.scala:569)
        at play.sbt.run.PlayReload$.compile(PlayReload.scala:51)

The full stack trace can be found here

It seems like many imports are missing or not correct.

Iam running windows 10 with the AdoptOpenJDK version "11.0.8" and sbt with version 1.3.13

openapi-generator version

1.0.18-4.3.1

OpenAPI declaration file content or url
openapi: "3.0.1"
info:
  version: "0.0.2"
  title: "api which provides CRUD functionalities to handle news/event for the EFFORT webpage"
servers:
  - url: "http://news.effort.lehre.hwr-berlin.de"
    description: production server
  - url: "http://localhost:9000"
    description: local server
paths:
  /news:
    get:
      summary: returns a sorted (by creation date) list of news for the given interval
      tags:
        - news
      operationId: getNews
      parameters:
        - in: query
          name: startIndex
          description: the start index of the interval (*inclusiv*) 
          required: true
          schema:
            type: integer
            format: int64
            minimum: 0
        - in: query
          name: endIndex
          description: the end index of the interval (*exclusiv*) 
          required: true
          schema:
            type: integer
            format: int64
            minimum: 0
      responses:
        '200':
          description: A list of news sorted by creation date
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NewsList'

    post:
      summary: add a new news object to the database
      tags:
        - news
      security:
        - bearerAuth: []
      operationId: postNews
      requestBody:
        required: true
        description: The News to create
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/News'
      responses:
        '200':
          description: The created news
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/News'
        '401':
          $ref: '#/components/responses/UnauthorizedError'

    patch:
      summary: Patch a News Article
      tags:
        - news
      security:
        - bearerAuth: []
      operationId: patchNews
      requestBody:
        required: true
        description: The News to patch
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/News'
      responses:
        '200':
          description: The updated News
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/News'
        '401':
          $ref: '#/components/responses/UnauthorizedError'

    delete:
      summary: delete a News
      tags:
        - news
      security:
        - bearerAuth: []
      operationId: deleteNews
      parameters:
        - in: query
          name: newsId
          description: The id of the news to delete
          required: true
          schema:
            type: string
      responses:
        '200':
          description: deleted the news sucesfull
        '401':
          $ref: '#/components/responses/UnauthorizedError'

components:
  securitySchemes:
    bearerAuth:            # arbitrary name for the security scheme
      type: http
      scheme: bearer
      bearerFormat: JWT    # optional, arbitrary value for documentation purposes

  responses:
    UnauthorizedError:
      description: Access token is missing or invalid

  # TODO: make sure the datamodel is appropriated 
  schemas:
    NewsList:
      type: array
      items:
        $ref: '#/components/schemas/News'
    News:
      required:
        - title
        - description
        - author
      properties:
        newsId:
          type: string
          format: uuid
        name:
          type: string
        description:
          type: string
        author:
          type: string
        creationDate:
          type: string
          format: date
Generation Details
openapi-generator generate -g scala-play-server --additional-properties=basePackage=de.hwrBerlin.lehre.effort.newsapi,apiPackage=de.hwrBerlin.lehre.effort.newsapi.generated.api,modelPackage=de.hwrBerlin.lehre.effort.newsapi.generated.model -o api -i openapi.yaml
Steps to reproduce

Should occure when executing the given command in Generation Details for the provided OpenAPI file.

wing328 commented 4 years ago

@linde9821 thanks for reporting the issue. Can you please test with 5.0.0-beta2 or the latest master to see if the issue still occurs?

linde9821 commented 4 years ago

hi thx for the reply. It seems to work 👍 e.g. the imports are generated

import de.hwrBerlin.lehre.effort.newsapi.generated.api._
import play.api.inject.{Binding, Module => PlayModule}
import play.api.{Configuration, Environment}

and the dependencies are there

"javax.annotation" % "javax.annotation-api" % "1.3.2" % "compile",

I guess the issue can be closed. Thx for the support But i suggest updating some dependencies. For instance "org.webjars" % "swagger-ui" % "3.1.5", is used but "org.webjars" % "swagger-ui" % "3.32.5" is available. (There are more :D)

wing328 commented 4 years ago

But i suggest updating some dependencies. For instance "org.webjars" % "swagger-ui" % "3.1.5", is used but "org.webjars" % "swagger-ui" % "3.32.5" is available. (There are more :D)

Nice suggestion. I wonder if you've time to file a PR updating those dependencies to the newer versions. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/scala-play-server/build.sbt.mustache is a good starting point.

linde9821 commented 4 years ago

Would be glad to do so. But i dont have Permission to create a new branch and therefore a PR.