我正在尝试使用 AWS API Gateway 实现在 Swagger 或 OpenAPI 3.0 中定义的 API。
这个 API 中的一个端点采用一个抽象的基本模型(让我们称它为Pet与陈旧的 Swagger 示例一致),但实际上需要一个从Pet……Dog例如派生的具体模型。
具体模型可以由 上的type属性确定Pet。
具体模型可以添加其他字段。
当然,这是一份工作discriminator:
definitions:
Pet:
discriminator: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/definitions/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy …Run Code Online (Sandbox Code Playgroud) 我正在关注关于在我的 Cloud Function 前面设置Google Cloud 端点(而不是 AWS API Gateway)的Google教程。我正在触发我的 Cloud Function 以触发 AWS lambda 函数,并且我正在尝试从 OpenAPI规范定义的端点传递 a 。path parameter
路径参数是 URL 路径的可变部分。它们通常用于指向集合中的特定资源,例如由 ID 标识的用户。一个 URL 可以有多个路径参数,每个参数都用花括号 { } 表示。
Run Code Online (Sandbox Code Playgroud)paths: /users/{id}: get: parameters: - in: path name: id # Note the name is the same as in the path required: true schema: type: integer
GET /users/{id}
我的 openapi.yaml
swagger: '2.0'
info:
title: Cloud Endpoints + GCF
description: Sample API on Cloud Endpoints with …Run Code Online (Sandbox Code Playgroud) google-cloud-endpoints aws-lambda google-cloud-functions openapi
我想设置OpenApiSchema.Example. 这是一个IOpenApiAny.
我可以将一个简单的 c# 对象转换/转换为现有的IOpenApiAny,例如。Microsoft.OpenApi.Any.OpenApiObject? 或者我需要在我的所有对象上实现接口
例子
var schma = new Microsoft.OpenApi.Models.OpenApiSchema
{
Example = new MyObject();//doesn't work, how can i cast it?
};
Run Code Online (Sandbox Code Playgroud)
背景:我正在尝试创建一个 Swashbuckle 过滤器,它将示例添加到请求和响应参数和架构中。我正在使用即将推出的 Swashbuckle 5.0(测试版),它正在使用 OpenAPI.NET,所以我正在尝试使用它来创建我的过滤器。
我正在尝试在 Spring Boot 中使用 Spring实现Open API Doc和Open API UI:
我指的是以下指南:https : //www.baeldung.com/spring-rest-openapi-documentation
maven中的依赖如下:
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-core -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-core</artifactId>
<version>1.1.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.20</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我使用的 Spring Boot 版本是 2.2
我在通过以下方式运行应用程序时遇到与元数据相关的错误 ./mvnw spring-boot:run
java.lang.IllegalStateException: Unable to read meta-data for class org.springdoc.core.MultipleOpenApiSupportConfiguration
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:233) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getOrder(AutoConfigurationSorter.java:204) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.access$000(AutoConfigurationSorter.java:150) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.autoconfigure.AutoConfigurationSorter.lambda$getInPriorityOrder$0(AutoConfigurationSorter.java:63) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:360) ~[na:na]
at java.base/java.util.TimSort.sort(TimSort.java:234) ~[na:na]
at java.base/java.util.Arrays.sort(Arrays.java:1516) ~[na:na]
Run Code Online (Sandbox Code Playgroud)
任何线索为什么会发生?
使用OAS3定义创建 API 网关时,路径和查询参数类型会更改为字符串。我找不到任何说明预期行为或原因的 AWS 文档。我知道手动配置 API Gateway 时它不会公开type路径/查询变量,但是有没有办法让 API Gateway 保留 OAS3路径/查询参数类型以供文档使用?
我们正在尝试使用 Serverless Developer Portal 来托管我们的 API 文档,我们需要公开正确的参数类型;例如,对于limit查询参数,我们需要将其显示为integer.
这是一个基本示例的 SAM。路径参数param是 aninteger并且 swagger 生成的文档正确显示它,但是 API Gateway 将其更改为 astring并且生成的文档将其显示为string:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ComplexityScoreProxy:
Type: AWS::Serverless::Api
Properties:
StageName: dev
DefinitionBody:
openapi: 3.0.1
info:
version: "1"
title: Aws API Gateway Boolean Test
paths:
/{param}:
get:
x-amazon-apigateway-integration:
type: http_proxy
uri: https://example.com
httpMethod: GET
responses:
"200": …Run Code Online (Sandbox Code Playgroud) 我是OpenAPI3.0.0 的新手。
我已经使用openapi-generator成功创建了一个 java 客户端库。
但是OpenAPIGenerator 允许生成 API 客户端库(SDK 生成)、服务器存根、文档。所以我想知道是否有任何命令或步骤可用于生成 HTML 文档以及自定义文档模板。
我在控制器中使用泛型。例如,从某些端点我返回Response<News>和Response<Tag>。
嗯,Swagger自动生成这部分yaml
responses:
200:
description: default response
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseNews'
Run Code Online (Sandbox Code Playgroud)
和
responses:
200:
description: default response
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseTags'
Run Code Online (Sandbox Code Playgroud)
这是我在 Java 中的 Response 实体。
public class Response<T> {
private List<T> data;
private Boolean moreDataExists;
}
Run Code Online (Sandbox Code Playgroud)
这就是 Swagger 生成组件的方式。
ResponseNews:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/News'
moreDataExists:
type: boolean
ResponseTags:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/Tags'
moreDataExists:
type: boolean
Run Code Online (Sandbox Code Playgroud)
嗯,这几乎是重复的代码。我想避免它,只在我的端点的描述中使用Response,并明确地向我的用户展示我使用泛型。
类似的东西:
responses:
200:
description: default …Run Code Online (Sandbox Code Playgroud) 我们需要使用 Java 注释生成 API 的多个定义。我们对任何和所有建议持开放态度,但如果我们能够弄清楚如何使其发挥作用,那么提出的建议似乎是一个可行的选择。
我们基本上需要从单个代码库生成 2 个 API 文档。第一个应该包括所有扫描的资源。这个没问题,我们可以做到这一点。
这是示例注释方法的删节版本:
@POST
@Path("/some/path")
@Consumes("application/json")
@Operation(
summary = "Do something...",
description = "Do something..."
)
@Tags({@Tag(name = "example-tag")})
@RequestBody(
description = "Do something...",
required = true,
content = @Content(
schema = @Schema(
implementation = Example.class
)
)
)
@ApiResponses({
@ApiResponse(
responseCode = "201",
description = "Do something...",
content = @Content(
schema = @Schema(
implementation = Example.class
)
)
)
})
public ApiResponse doSomething() throws Exception {
...
}
Run Code Online (Sandbox Code Playgroud)
这是我们构建文档的方式:
OpenAPI oas …Run Code Online (Sandbox Code Playgroud) 我正在从 swagger 2 迁移到 OpenApi 3。
Swagger 2 示例代码
@ApiOperation(value = "", nickname = "")
@GetMapping
public List<Employee> findEmployees(@Valid Dto dto) {
return employeeService.findEmployees(dto);
}
Run Code Online (Sandbox Code Playgroud)
OpenApi 3 代码
@Operation(summary = "")
@GetMapping
public List<Employee> findEmployees(@Valid Dto dto) {
return employeeService.findEmployees(dto);
}
Run Code Online (Sandbox Code Playgroud)
DTO类
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Dto {
private String status;
private String name;
private String destination;
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,swagger-ui 的生成存在显着差异。
Swagger 2 将 DTO 对象显示为扁平化为单个查询参数:
Swagger 2 ui 中发生单个查询参数时对象的图像扁平化
而 OpenApi 3 创建了一个 JSON 对象 …
我有一个请求对象,它可以是 2 个字符串类型“A”或“B”。
注意:这是我真正想要的一个更简单的例子。枚举在这里对我不起作用。
public class SampleRequest
{
//Can only be "A" or "B"
public string Property1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个可以作为 OpenAPI“OneOf”属性输出的模式过滤器。
https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.5
https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#schema-filters
public class CustomSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema {Type = "string", Description = "A"},
new OpenApiSchema {Type = "string", Description = "B"}
};
}
}
Run Code Online (Sandbox Code Playgroud)
运行 swagger 时,swagger-ui 正确呈现“oneOf”描述:
oneOf: List [ OrderedMap { "type": "string", "description": "A" }, OrderedMap { …Run Code Online (Sandbox Code Playgroud) openapi ×10
swagger ×6
java ×3
api-doc ×2
spring-boot ×2
springdoc ×2
swagger-2.0 ×2
.net-core ×1
aws-lambda ×1
aws-sam ×1
c# ×1
spring ×1
swagger-ui ×1
swashbuckle ×1
yaml ×1