我有以下openapi文件。我预计要生成的 API 类名将是SampleApi因为操作 "/hello" 被标记为 "sample"
tags。但是它使用operation名称生成 API 类名称,它是HelloApi. 我在这里缺少什么?我正在使用openapi-generator-maven-plugin版本3.3.1
openapi: "3.0.0"
info:
version: 1.0.0
title: Sample Service
tags:
- name: sample
paths:
/hello:
get:
summary: Says hello world
operationId: greet
tags:
- sample
responses:
200:
description: ok
content:
plain/text:
schema:
type: string
example: Hello World
我正在尝试使用 openapi-generator-cli 从 v2 swagger 文件生成 API 客户端。为此,我使用 openapi-generator-cli 的 docker 容器,它将其版本报告为“4.1.0-SNAPSHOT”。
代码生成使用以下选项:
{
"npmName": "...",
"npmVersion": "0.0.3",
"snapshot": true,
"ngVersion": "8.1.1"
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将该providedInRoot选项设置为 true。
但是,生成的服务类没有使用@Injectable装饰器进行注释。因此,将它们导入我的组件并在组件的构造函数中添加服务后,我无法使用它们。这就是我的组件的样子:
import { Component, OnInit } from '@angular/core';
import { UsersService, User } from '...'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(userService: UsersService) {}
title = 'user-frontend';
ngOnInit() {
this.userService.listUsers();
}
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为 userService 不存在于 AppComponent 的范围内。
这就是我导入生成的模块的方式:
import { BrowserModule } from '@angular/platform-browser';
import …Run Code Online (Sandbox Code Playgroud) 我正在typescript-angular使用 openapi-generator-maven-plugin生成代码,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.2.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/../my_server/openapi.json</inputSpec>
<generatorName>typescript-angular</generatorName>
<output>${project.basedir}</output>
<npmName>myClientRest</npmName>
<npmRepository>http://localhost:8444/repository/npm-releases/</npmRepository>
<providedInRoot>true</providedInRoot>
<apiModulePrefix>my</apiModulePrefix>
<stringEnums>true</stringEnums>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我现在缺少的是一个package.json文件,以便我可以执行npm install. 从一些较旧的 swagger 示例中,看起来使用 swagger 插件package.json生成了一个文件。
所以我的问题是为什么package.json没有生成文件,我该怎么做才能得到一个?
生成器运行的输出对我来说看起来不错:
[信息] --- openapi-generator-maven-plugin:4.2.0:generate (default) @ client ---
[信息] OpenAPI 生成器:typescript-angular(客户端)
[信息] 生成器“typescript-angular”被认为是稳定的。
[信息] 提示:未定义环境变量“TS_POST_PROCESS_FILE”(可选)。例如要格式化源代码,请尝试'export TS_POST_PROCESS_FILE="/usr/local/bin/prettier --write"' (Linux/Mac)
[INFO] 注意:要启用文件后处理,'enablePostProcessFile' 必须设置为
true(--enable-post-process-file for CLI)。 …
我有 GET API,它将 Map 作为请求参数。如何在 Open API 3.0 中以 yaml 格式定义它
@GetMapping
public void getDevicesInfo(@RequestParam(required = false) Map parameters)
{
}
Run Code Online (Sandbox Code Playgroud)
开放API不支持Map类型。
我有一个 .net-core 3.1 解决方案,其简单端点仅接受帖子。我想使用 openAPI-generator 生成 SDK(因为Visual Studio for mac中不提供 New REST Client SDK 选项)。
我第一次这样做时,我使用以下命令:
openapi-generator generate -i ../swagger.v0.0.21.json -g csharp -o ./generator
Run Code Online (Sandbox Code Playgroud)
这生成了一个 .net 框架解决方案,因此我必须对其进行一些修改并安装一些 nuget 测试库才能使其运行。毕竟我的连接被拒绝了——我希望这只是因为它是为错误的框架构建的。
我注意到还有另一个生成目标csharp-netcore。但我不确定如何使用它。我的命令应该是什么样的?我刚在想:
openapi-generator generate -i ../swagger.v0.0.21.json -g csharp-netcore -targetFramework netcoreapp3.1 -o ./generator
Run Code Online (Sandbox Code Playgroud)
但这会产生:
[error] Found unexpected parameters: [netcoreapp3.1]
Run Code Online (Sandbox Code Playgroud)
(明确地说,我有一个解决方案结构:
我正是想进行测试。但我的来源直接来自Assignment_1 目录。
我正在使用 openapi-generator Gradle 插件从开放 API 模式生成模型文件。通过 build.gradle 脚本中的这些设置,一切似乎都正常:
openApiGenerate {
globalProperties = [
apis: "false",
modelDocs: "false",
models: "Pet"
]
generatorName = "java"
generateModelTests = false
inputSpec = "$rootDir/src/main/resources/schema/my_schema.json".toString()
outputDir = "$rootDir".toString()
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8",
serializationLibrary: "jackson",
library: "jersey1"
]
Run Code Online (Sandbox Code Playgroud)
}
结果类在正确的包中生成:
问题就在这里 - 我的源代码中不需要它们,我只在编译阶段需要它们。我希望它们在构建目录中生成,以将它们与其他逻辑分开。但是当我将输出目录更改为“$buildDir/ generated”.toString() 时,会发生这种情况:
有没有办法摆脱错误的包“src.main.java”?
我在 spring 项目中使用 OpenAPI java 生成器 [1] 和 library:resttemplate, dateLibrary:java8 从规范生成客户端。
对于规范中的属性:
targetDate:
type: string
format: date
Run Code Online (Sandbox Code Playgroud)
生成以下代码:
public static final String JSON_PROPERTY_TARGET_DATE = "targetDate";
private LocalDate targetDate;
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public LocalDate getTargetDate() {
return targetDate;
}
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
Run Code Online (Sandbox Code Playgroud)
我希望该字段能够序列化为完整日期,例如规范所承诺的“2023-01-01”:https: //spec.openapis.org/oas/v3.0.0#data-types。然而它实际上被序列化为一个数组:[2023,1,1]。
同样的另一个属性
otherDate:
type: string
format: date-time
Run Code Online (Sandbox Code Playgroud)
被序列化为自纪元以来的秒数,而不是全时。(我认为这是生成器中的错误)
由于生成了代码,我无法添加任何注释。我怎样才能确保日期正确序列化?
[1] openapi-generator-maven-plugin 6.3.0
java openapi jackson-databind openapi-generator openapi-generator-maven-plugin