我正在尝试为 OpenAPI 3.0 规范中的响应创建链接。更具体地说,我想提供我的响应之一与其他可用操作之间的已知关系(参见Link Object)。
在我的 Spring Boot 项目中,我使用 Springdoc(版本:1.3.9)来生成 API 文档。根据@ApiResponse#links文档,我尝试使用以下端点代码来实现我的目标:
@GetMapping(value = "/avatar", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Request avatar info", operationId = "requestAvatar")
@ResponseStatus(HttpStatus.OK)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", links = {
@Link(name = "Download Avatar", operationId = "downloadAvatar",
parameters = {
@LinkParameter(name = "userId"),
@LinkParameter(name = "uuid")
})
}),
...
@ResponseBody
ResponseEntity<Avatar> requestAvatar();
Run Code Online (Sandbox Code Playgroud)
不幸的是,除了“无链接”描述之外,我在 Swagger UI 中看不到任何结果。
检查生成的 JSON 规范后,我也没有找到APIlinks的任何密钥requestAvatar。
我在创建过程中是否遗漏了某些内容,@Link或者 …
我使用以下 OpenAPI 命令来使用openapi-generator生成客户端:
npx @openapitools/openapi-generator-cli generate -i api.yaml -g cpp-restsdk -o %CD%
Run Code Online (Sandbox Code Playgroud)
运行命令后为什么会显示以下警告:
[main] WARN o.o.codegen.DefaultCodegen - The value (generator's option) must be either boolean or string. Default to `false`.
Run Code Online (Sandbox Code Playgroud) 基本上我的所有 DTO 都是使用可空字段生成的,例如:id?: number;。如何为未标记为可为空的类型创建不可为空的类型。
我的 DTO 架构如下所示:
"UserDTO": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"nullable": true
},
"lastName": {
"type": "string",
"nullable": true
},
"id": {
"type": "integer",
"format": "int32"
}
}
Run Code Online (Sandbox Code Playgroud)
我生成这样的代码:
openapi-generator generate --additional-properties=prefixParameterInterfaces=true,typescriptThreePlus=true --remove-operation-id-prefix -i libs/services/defs/swagger.json -g typescript-fetch -o libs/services/src/api
我正在努力将路径参数从网关传递到实际端点。
这是我的 Open API yaml:
swagger: '2.0'
info:
description: |
Blah blah
version: 0.0.1
title: SSAuth
contact:
email: blah@gmail.com
schemes:
- https
produces:
- application/json
paths:
/v0/auth/users/echo:
get:
summary: check the health of api
operationId: healthCheck
consumes:
- application/json
produces:
- application/json
responses:
200:
description: OK
x-google-backend:
address: https://path-to-my-cloud-run-service/v0/auth/users/echo
security:
- api_key: []
/v0/auth/users/type/{type}:
post:
summary: Add a new user to the user
operationId: addUser
consumes:
- application/json
produces:
- application/json
parameters:
- name: type
in: path
description: provider type …Run Code Online (Sandbox Code Playgroud) 我已经在 Spring boot 应用程序中配置了 Swagger,spring boot 应用程序有 2 个控制器,casesController 和 AttachmentController,我希望附件控制器中的所有端点都出现在 case 控制器端点之后,但 swagger 正在做相反的事情。
我的代码如下所示:
配置文件:
@Configuration
class OpenApiConfig(
@Value("\${springdoc.info.title}") val title: String,
@Value("\${springdoc.info.description}") val infoDescription: String,
@Value("\${springdoc.info.version}") val version: String,
@Value("\${springdoc.info.license.name}") val licenseName: String,
@Value("\${springdoc.info.license.url}") val licenseUrl: String,
@Value("\${springdoc.info.contact.email}") val email: String,
@Value("\${springdoc.server.url}") val url: String,
@Value("\${springdoc.server.description}") val description: String
) {
@Bean
fun customOpenAPIConfig(): OpenAPI {
return OpenAPI()
.components(
Components()
)
.info(
Info().title(title).description(infoDescription).version(version)
.license(License().name(licenseName).url(licenseUrl))
.contact(Contact().email(email))
)
.servers(listOf(Server().url(url).description(description)))
.tags(listOf(Tag()))
}
}
Run Code Online (Sandbox Code Playgroud)
附件控制器:
@RestController
@RequestMapping("/cases/{id}/attachments")
lass AttachmentController(private …Run Code Online (Sandbox Code Playgroud) 我有一个 .NET 5.0 ASP.NET Core 项目,我正在使用它Nswag来生成 API 客户端。假设我有以下 API 模型:
public class GetFooListResponseModel
{
public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想要的是两件事。让我们从基本的开始。
Foo不是GetFooListResponseModel.非常感谢!
我想使用库中的类作为属性的类型。说是吧foo.lib.Foo。将其正确定义为属性类型的正确方法是什么?
SomeDto:
type: object
properties:
foo:
$ref: `foo.lib.Foo`???
Run Code Online (Sandbox Code Playgroud) 在设计 API 时,我定义了一个UserGeo包含两个字段的模型 - domain(字符串数组) 和country(字符串数组)。
[]如果没有为 提供值,则应在请求正文中使用空列表domain。但是在将default属性定义为[]生成Spring代码时,不会分配空 ArrayList<> 又名[]。
附上代码示例:
招摇定义:
definitions:
UserGeo:
type: "object"
properties:
country:
type: "array"
items:
type: "string"
domain:
type: "array"
items:
type: "string"
default: []
Run Code Online (Sandbox Code Playgroud)
这不会将域值默认为空列表,请检查生成的 Spring/Java 代码:
.
.
public class Classification {
@JsonProperty("country")
@Valid
private List<String> country = null;
@JsonProperty("domain")
@Valid
private List<String> domain = null;
.
.
Run Code Online (Sandbox Code Playgroud)
然而,当我将域字段定义为required甚至没有使用默认值定义它时,生成的代码会分配一个空列表作为domain.
definitions:
UserGeo:
type: …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个以枚举作为查询参数的服务,但它总是生成错误。这是 yaml 的部分:
name: language
in: query
description: language
schema:
type: string
enum:
- en
- de
Run Code Online (Sandbox Code Playgroud)
我在参数中使用它:
- $ref: '#/components/parameters/language'
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
[ERROR] .../api/TestApi.java:[110,65] illegal start of type
[ERROR] .../api/TestApi.java:[110,66] ')' expected
[ERROR] .../api/TestApi.java:[110,82] ';' expected
Run Code Online (Sandbox Code Playgroud)
代码如下所示:
public Response getByLanguage( @PathParam("id") Long id, , allowableValues="en, de" @QueryParam("language") String language
Run Code Online (Sandbox Code Playgroud)
这是我的插件:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.0.1</version>
<configuration>
<inputSpec>${basedir}/src/main/resources/openapi.yaml</inputSpec>
<output>${project.build.directory}/generated-sources/java</output>
<generatorName>jaxrs-resteasy-eap</generatorName>
<modelPackage>com.openapi.example.model</modelPackage>
<apiPackage>com.openapi.example.api</apiPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<sourceFolder>openapi</sourceFolder>
<dateLibrary>java8</dateLibrary>
<interfaceOnly>true</interfaceOnly>
<java8>true</java8>
<serializableModel>true</serializableModel>
<useTags>true</useTags>
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
<executions>
<execution>
<phase>generate-resources</phase>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution> …Run Code Online (Sandbox Code Playgroud) pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Hoxton.SR10</version>
</parent>
...
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当使用 springdoc-openapi-ui、springdoc-openapi-security 以及 spring-boot-starter-security 和 keycloak-spring-boot-starter 时,我们面临一个问题。
pom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>12.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.4.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
DummySecurityConfig(当前使用配置文件dev):
@Profile({"test", "dev"})
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
prePostEnabled = true,
securedEnabled = true,
jsr250Enabled = true)
@EnableWebMvc
public class DummySecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
protected static final String[] ACTUATOR_WHITELIST = {
"/actuator/**"
};
protected …Run Code Online (Sandbox Code Playgroud) openapi ×10
java ×4
swagger ×3
spring-boot ×2
springdoc ×2
api ×1
asp.net-core ×1
c# ×1
generator ×1
kotlin ×1
nswag ×1
nswagstudio ×1
swagger-ui ×1
typescript ×1
user-roles ×1
yaml ×1