Swagger codegen - api 和模型

Posted

技术标签:

【中文标题】Swagger codegen - api 和模型【英文标题】:Swagger codegen - api and model 【发布时间】:2021-10-10 03:26:00 【问题描述】:

所以这对我来说是 codegen101,我一直在尝试使用 swagger 插件并不断出错。这是一个已知问题吗,我已经尝试了插件的多个版本,认为这可能是其中一个已知问题,但根本没有成功。这应该是非常直接的练习。我不确定我错过了什么。谁能指出来

 <build>
    <plugins>
      <plugin>
        <groupId>io.swagger.codegen.v3</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.18</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>$project.basedir/src/main/resources/service-api.yaml</inputSpec>
              <language>java</language>
              <output>$project.build.directory/generated-sources/</output>
              <generateSupportingFiles>false</generateSupportingFiles>
              <apiPackage>com.openapi101.api</apiPackage>
              <modelPackage>com.openapi101.models</modelPackage>
              <configOptions>
                <interfaceOnly>true</interfaceOnly>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>

错误

[ERROR]
java.lang.RuntimeException: Could not generate model 'Book'
    at io.swagger.codegen.v3.DefaultGenerator.generateModels (DefaultGenerator.java:451)
    at io.swagger.codegen.v3.DefaultGenerator.generate (DefaultGenerator.java:779)
    at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute (CodeGenMojo.java:547)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:78)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:567)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: com.github.jknack.handlebars.HandlebarsException: /handlebars/JavaSpring/pojo.mustache:2:6: java.lang.reflect.InaccessibleObjectException: Unable to make public boolean java.util.Collections$EmptyMap.isEmpty() accessible: module java.base does not "opens java.util" to unnamed module @4bc33720
    /handlebars/JavaSpring/pojo.mustache:2:6
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:357)

编辑:使用新版本 - 3.0.27。还是不行

这是yaml文件

openapi: 3.0.0
 
info:
  version: 0.0.1
  title: Library Service
  description: The library service
 
tags:
  - name: "Library"
 
 
paths:
 
  /library/books:
    get:
      summary: This is summary
      tags:
        - "Library"
      description: This is a description
      operationId: getAllBooksInLibrary
      responses:
        "200":
          description: This means its ok
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Book'
 
components:
 
  schemas:
 
    Book:
      description: This is the book model
      type: object
      properties:
        name:
          description: The name of book
          type: string
        bookAuthor:
          description: name of author
          type: string

【问题讨论】:

试用最新版本,3.0.27。如果它仍然不起作用,您能否从您的 OpenAPI YAML 文件中发布 Book 架构? 上面添加了图书架构 【参考方案1】:

我有同样的问题。解决方案是简单地使用 Java 8 来运行它。这让我可以毫无问题地运行生成

【讨论】:

以上是关于Swagger codegen - api 和模型的主要内容,如果未能解决你的问题,请参考以下文章

swagger-codegen 或 openapi-generator PHP 版本

如何在类控制器中实现api swagger的spring注释?

Swagger-Codegen 不使用供应商扩展 x-discriminator-value

将生成的 Flask 应用程序代码(Swagger-Codegen)粘合到后端实现的最简洁方法

Swagger-codegen 开始使用

如何在 Gradle 中为 OpenAPI 3.0 使用 Swagger Codegen?