在 OpenAPI 3.0 中使用 OpenAPI Generator 时,“Unrecognized token openapi”错误是什么意思?

vpa*_*vpa 6 openapi openapi-generator

我以 OpenAPI 3.0 格式 ( https://swagger.io/docs/specification/basic-structure/ )编写了一个 API 定义。现在我正在尝试生成 Java Spring 对象,就像我之前使用 Swagger 2.0 定义及其关联的 Maven 插件所做的那样。

到目前为止,我有一个基本的 API 定义,开头是:

openapi: 3.0.0 info: title: Demo API description: This is a basic REST API implementing the [Open API Specification](https://en.wikipedia.org/wiki/OpenAPI_Specification). version: 0.0.1

在我的pom.xml文件中,我添加了:

<dependency>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-cli</artifactId>
  <version>3.3.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但是在执行时mvn install,我收到此错误:

com.fasterxml.jackson.core.JsonParseException: Unrecognized
     token 'openapi': was expecting ('true', 'false' or 'null')
     at [Source: definition\DEFINITION.yml; line: 1, column: 9]
Run Code Online (Sandbox Code Playgroud)

有谁知道问题出在哪里?

Gau*_*ani 7

经过多次尝试后,我发现问题出在我的 Swagger 文件上。那url不是一个正确的 URL。就像/api(没有主机域的 url)。

OpenAPI 生成器可以给出更好的错误消息。无论如何,放置一些有效的网址,让我可以生成代码。


小智 1

我目前正在研究 openapi-generator-maven-plugin 从 OpenAPI JSON 模式生成 Java 类。

这些错误看起来像是语法问题。因此,首先确保您的架构在语法上是正确的并且如下所示:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Demo API",
    "description": "This is a basic REST API implementing the [Open API Specification](https://en.wikipedia.org/wiki/OpenAPI_Specification).",
    "version": "0.0.1"
  },
  # Schema definition goes here
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么 @vpa 要把他完全有效的 YAML 简化为 JSON? (12认同)