那里肯定有人已经遇到过这种情况......
我创建了一个 WebApi 解决方案,实现了 swagger,完整的文档,整个 9 码!
现在,我想在我的 Web 应用程序上将此 Api 作为连接服务使用。因此,请遵循在线的每个教程:
这会在我的解决方案中生成一个部分类(MyTestApiClient)
public parial class MyTestApiClient
{
// auto generated code
}
Run Code Online (Sandbox Code Playgroud)
下一步,在 Startup.cs 中注入服务
services.AddTransient(x =>
{
var client = new MyTestApiClient("https://localhost:5001", new HttpClient());
return client;
});
Run Code Online (Sandbox Code Playgroud)
然后,将类注入到使用它的某个类中,这一切都有效
public class TestService
{
private readonly MyTestApiClient _client; // this is class, not an interface -> my problem
public TestService(MyTestApiClient client)
{ …Run Code Online (Sandbox Code Playgroud) dependency-injection swagger .net-core openapi openapi-generator
我在 Azure 函数应用程序 (v3) 中添加了 OpenApi 支持,.ConfigureOpenApi()使用Program.Main(). 我对特定功能使用函数装饰,但如何控制 ~/api/swagger/ui 上显示的通用 API 名称、版本等?
这是我的Program.Main()代码:
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.ConfigureOpenApi()
.ConfigureServices(services =>
{
services.AddLogging();
}
)
.Build();
host.Run();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个 Open API 3 规范的 yaml 文件,它有一些 x- 前缀的属性。我正在尝试使用 openapi-generator-cli 生成一个 Angular Typescript SDK。但是,当我在模板胡子中引用该属性时,以 x 为前缀的属性始终为空。
来自 yaml 的示例端点(省略了不相关的内容):
/about:
get:
summary: Information about this API
x-foo: getAbout
Run Code Online (Sandbox Code Playgroud)
我如何在 Mustache 模板中使用它(省略了不相关的内容):
{{#operation}}
public {{x-foo}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
Run Code Online (Sandbox Code Playgroud)
(基本上,我正在尝试使用 x-foo 参数作为正在生成的 SDK 中的方法名称。)
但是,生成的 SDK 替换{{x-foo}}为空白:
public ()
Run Code Online (Sandbox Code Playgroud)
这就是我调用生成器的方式(没有换行符):
openapi-generator generate
--generator-name typescript-angular
--template-dir ./path/to/templates
--input-spec ./path/to/api.yml
--output ./output
--config ./path/to/config.json
Run Code Online (Sandbox Code Playgroud)
如何从 openapi-generator 模板中引用供应商扩展/以 x 为前缀的 Open API 3 属性?
我试图从 OpenAPI v3 YAML 文件生成 Spring REST 接口。构建 说:
Successfully generated code to property(class java.lang.String, property(class java.lang.String, fixed(class java.lang.String, /home/zolv/workspaces/main/generation-test/src/main/java)))
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings
Run Code Online (Sandbox Code Playgroud)
但是输出目录中没有生成代码。
我遵循了OpenAPI 生成器 gradle 插件文档文档。
我的 build.gradle:
plugins {
id 'java-library'
id "org.openapi.generator" version "4.1.1"
}
repositories {
jcenter()
}
dependencies {
implementation "org.openapitools:openapi-generator-gradle-plugin:3.3.4"
}
openApiGenerate {
generatorName = "spring"
inputSpec = "$rootDir/docs/openapi/api.yml".toString() …Run Code Online (Sandbox Code Playgroud) 对于我们的 REST API,我们有某些 HTTP 请求标头,这些标头将由基础设施组件设置,并且不应在 OpenAPI 规范中发布给使用者。
在我们构建的基于 Spring Web 的实现代码(实现 openapi-generator 生成的接口)中,从 HTTP 请求中检索类似内容的正确方法是什么?例如,接口上未指定的标头。
我在 openapi-generator 本身中没有看到任何选项来将 HttpServletRequest 作为参数添加到生成的方法中。我希望有一种更通用的方法可以在 Spring / Spring Web 中处理它。
我正在使用如下所示的 OpenAPI 生成器 maven 插件来为模型生成 Java 客户端代码。
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
当 ,我生成模型类时,它们使用通常的 POJO 字段声明以及 getter 和 setter 生成。但我想做的是,而不是生成getter和setter方法,我想我的类与龙目岛标注为Java的POJO以获得更多信息自动生成@Getter,@Setter,@Data,等有没有一种方法来定制模型生成器,以适应上述用例的要求?
我试图找出是否有办法。我找到了这个讨论,最后一条评论谈到了PR,其中使用 Lombok 注释生成模型的问题已得到解决。但是我在 OpenAPI 生成器开源项目中没有看到任何明确的使用说明或任何关于此功能的文档,说明它尚未实现。那么,今天有没有办法用 Lombok 注释而不是常规的 getter 和 setter 生成模型?
在 OpenAPI/Swagger 文件中声明“char”的正确方法是什么?我尝试过这些..但没有用
myTestProperty:
type: char
example: C or B
Run Code Online (Sandbox Code Playgroud)
我也这样累了,但没有运气
myTestProperty:
type: string
format: char
example: C or B
Run Code Online (Sandbox Code Playgroud)
该文档确实特别提到了 char 数据类型,但在其他地方也找不到。
请建议。
我在 Swagger V2.0 中有一个 API 规范的请求正文,如下所示。
"/uri/path":
...
parameters:
- in: body
...
schema:
$ref: '#/definitions/StatusObject'
definitions:
StatusObject:
status:
$ref: '#/definitions/StatusEnum'
StatusEnum:
type: string
enum: ['ALPHA', 'BRAVO', 'UNKNOWN']
Run Code Online (Sandbox Code Playgroud)
现在,如果客户端未设置该值,我希望StatusObject.status获得默认值。UNKNOWN我试图按如下方式实现这一目标,但没有运气。
"/uri/path":
...
parameters:
- in: body
...
schema:
$ref: '#/definitions/StatusObject'
definitions:
StatusObject:
status:
$ref: '#/definitions/StatusEnum'
default: 'UNKNOWN'
StatusEnum:
type: string
enum: ['ALPHA', 'BRAVO', 'UNKNOWN']
Run Code Online (Sandbox Code Playgroud)
我也尝试过,'#/definitions/StatusEnum.UNKNOWN'但还是不行。也梳理了文档,但找不到进一步的内容。我缺少什么?
我想要实现的是为此属性设置默认值status。当枚举按如下方式定义时,此方法有效。
"/uri/path":
...
parameters:
- in: body
...
schema:
$ref: '#/definitions/StatusObject'
definitions:
StatusObject:
status:
type: string
enum: …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Spring Boot(最新版本)并创建一个具有 RESTful api 的后端的应用程序。传统上,我创建了如下控制器:
@RestController
@RequestMapping("/contacts")
public class ContactController {
@Autowired
private ContactService service;
@RequestMapping(value = "/contactId/{contactId}",
method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody ContactEntity getContactById(@PathVariable("contactId") long contactId) {
ContactEntity contactEntity = service.getContactById(contactId);
return contactEntity;
}
Run Code Online (Sandbox Code Playgroud)
集成测试一直是这样的:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ServiceContextConfiguration.class)
@ComponentScan("com.tomholmes.springboot.phonebook.server")
@Transactional
@WebAppConfiguration
public class ContactControllerTest {
@Test
public void testGetContactById() throws Exception {
MockHttpServletRequestBuilder requestBuilder =
MockMvcRequestBuilders.get(BASE_URL + "/contactId/6");
this.mockMvc.perform(requestBuilder)
.andDo(print())
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
多年来,它一直作为“代码优先”API 正常工作。现在,我正在使用 OpenAPI 3 和 YAML 文件处理合约优先的 API。API 是在与以前相同的位置生成的,我希望测试能够像以前一样工作,但事实并非如此。
因此,一项资源:
[https://www.hascode.com/2018/08/testing-openapi-swagger-schema-compliance-with-java-junit-and-assertj-swagger/#API_Test]建议我使用assertj …
junit spring-boot junit-jupiter openapi-generator springdoc-openapi-ui
我试图将一个大的 yml 分成一堆较小的 yml 文档。我按照David Garcia 提供的示例进行操作,然后使用 OpenAPI CodeGenerator 生成我的模型。OpenAPI Generator 抱怨[BUG] attribute components.schemas.Schema name doesn't adhere to regular expression ^[a-zA-Z0-9.-_]+$. 因此,我尝试通过克隆 David Garcia 的存储库并在本地部署来使用 David Garcia 的示例,但我遇到了相同的错误。我决定在swagger editor中检查它,我遇到了同样的问题,但错误消息显示
Semantic error at components.schemas.$ref
Component names can only contain the characters A-Z a-z 0-9 - . _
Jump to line 25
Run Code Online (Sandbox Code Playgroud)
我正在使用 David Garcia 示例中的 yaml:
Semantic error at components.schemas.$ref
Component names can only contain the characters A-Z a-z 0-9 - . _
Jump to …Run Code Online (Sandbox Code Playgroud)