标签: openapi

如何为`map` 对象中的属性名称编写 OpenAPI 3 (Swagger) 规范?

我试图描述的 API 有一个结构,其中根对象可以包含任意数量的子对象(属性本身就是对象)。“键”或根对象中的属性是子对象的唯一标识符,值是子对象的其余数据。

{
  "child1": { ... bunch of stuff ... },
  "child2": { ... bunch of stuff ... },
  ...
}
Run Code Online (Sandbox Code Playgroud)

这可以类似地建模为数组,例如:

[
  { "id": "child1", ... bunch of stuff ... },
  { "id": "child2", ... bunch of stuff ... },
  ...
]
Run Code Online (Sandbox Code Playgroud)

但这既使识别属性在结构上变得不太清楚,并使子代 ID 之间的唯一性隐式而非显式,因此我们想要使用对象或映射。

我已经看过Model with Map/Dictionary Properties的 Swagger 文档,但这并不完全适合我的用例。写一些类似的东西:

"Parent": {
  "additionalProperties": {
    "$ref": "#/components/schemas/Child",
  }
Run Code Online (Sandbox Code Playgroud)

产生这样的东西:

API 的 Swagger 渲染

这充分传达了属性中值的描述性,但我如何记录对象中“键”的限制?理想情况下,我想说“这不仅仅是任意字符串,而是与孩子对应的 ID”。这是否以任何方式支持?

swagger swagger-editor openapi

11
推荐指数
1
解决办法
8277
查看次数

OpenApi 3 从外部文件导入模式

我正在为 Web 服务定义通用模式,并且我想将它们导入到规范的组件/模式部分。我想创建一个跨多个服务通用的规范数据模型,以避免在每个服务定义中重新定义相似的对象。

有没有办法做到这一点?是否有与 XSD 对其导入标签所做的类似的机制?

rest xsd web-services swagger openapi

11
推荐指数
2
解决办法
7028
查看次数

如何将 OpenAPI 规范 (Swagger 2.0) 转换为 proto3?

我已经进行了广泛的谷歌搜索,但找不到一个好的工具来做到这一点。我能找到的最接近的是https://github.com/googleapis/gnostic,它允许将 OpenAPI 描述 (swagger.yaml) 转换为 .pb 文件或 .json 文件。我想知道是否有任何工具可以将此 .pb 文件转换为 .proto proto3 文件?

我也试过https://github.com/NYTimes/openapi2proto但不幸的是有一些情况没有正确处理。

在此先感谢您的帮助!

code-generation protocol-buffers openapi

11
推荐指数
1
解决办法
1798
查看次数

如何从 protobuf (.proto) 文件在 (.json/.yaml) 中生成 swagger3 (OpenAPI3) 规范?

我的原始用例:

我正在gRPC使用服务器(使用protobuf)在 GO 中构建应用程序,并将其包装在 HTTPS 服务器中(使用gin)。只有 HTTPS 服务器被发布到客户端使用(我的意思是我的应用程序可以通过 REST API 访问,实际上然后在 gRPC 端点上拨号),我使用SwaggerOpenAPI3发布它(版本 3 是主要的此处要求)规范。gRPC 和 HTTPS 都是必需的,任何解决方案都应遵循此架构。

我不想在两个地方维护我的服务器规范,即我不想同时维护 proto 文件 ( .proto) 和 swagger 规范 ( .json/.yaml)。由于我绝对必须编写 proto 文件来生成 gRPC 服务器,因此我想自动生成 swagger 规范(OpenAPI3)。

我在哪里:

我能够使用grpc-gatewayswagger从 protobuf 文件 ( .proto)生成OpenAPI2 规范,如下所示:grpc-rest-go-example。但我的要求是 OpenAPI3;更具体地说,我想使用OpenAPI3 中的功能并从proto 的功能映射到它。这在 OpenAPI2 中是不可能的,因为它不允许 API 具有多个类型定义的请求/响应主体,这是 OpenAPI3 中通过启用 oneOf、anyOf 和 allOf 构造添加的一项功能。oneOfoneof

在尝试这样做时,我偶然发现了 GoogleAPIs googleapis/ gnostic 的这个库,其描述是:

该存储库包含一个 …

protocol-buffers proto openapi swagger-3.0 grpc-go

11
推荐指数
1
解决办法
3476
查看次数

NestJs Swagger 混合类型

我有一个类,其中一个属性可以是字符串或字符串数​​组,不知道如何在 swagger 中定义它

    @ApiProperty({
        description: `to email address`,
        type: ???, <- what should be here?
        required: true,
    })
    to: string | Array<string>;
Run Code Online (Sandbox Code Playgroud)

我试过

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: 'string' },
                { type: 'Array<string>' },
            ],
        },
        required: true,
    })
Run Code Online (Sandbox Code Playgroud)

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: 'string' },
                { type: 'string[]' },
            ],
        },
        required: true,
    })
Run Code Online (Sandbox Code Playgroud)

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: …
Run Code Online (Sandbox Code Playgroud)

swagger typescript openapi nestjs

11
推荐指数
1
解决办法
2万
查看次数

OpenAPI 能否以有用的方式集成 HATEOAS?

是否可以使用 OpenAPI 来描述 HATEOAS REST API?

当我以HAL格式描述 API 时,我需要为其定义三种模式(一种用于请求有效负载,一种用于收集资源,一种用于项目资源)。例如:

components:
  schemas:
    Link:
      type: object
      properties:
        href:
          type: string
        hreflang:
          type: string
        title:
          type: string
        type:
          type: string
        deprecation:
          type: string
        profile:
          type: string
        name:
          type: string
        templated:
          type: boolean
    Links:
      type: object
      discriminator:
        propertyName: _links
      properties:
        _links:
          type: object
          additionalProperties:
            type: string
            $ref: "#/components/schemas/Link"
    CollectionModel:
      type: object
      discriminator:
        propertyName: _embedded
      properties:
        _embedded:
          type: object
        _links:
          type: object
          properties:
            self:
              type: string
            profile:
              type: string
            search:
              type: string
    CollectionModel_Foo:
      type: …
Run Code Online (Sandbox Code Playgroud)

swagger openapi openapi-generator

11
推荐指数
0
解决办法
2398
查看次数

如何为OpenAPI 3.0.0运行swagger-codegen

貌似官方规范V3支持接近释放https://blog.readme.io/an-example-filled-guide-to-swagger-3-2/和招摇,代码生成了3.0.0支持开发并在分支上传递一定程度的测试https://github.com/swagger-api/swagger-codegen3.0.0

我有一个swagger规范(从我现有的2.0规范通过https://github.com/mermade/swagger2openapi生成,输出看起来不错)

是否有一种简单的方法来运行swagger-codegen而无需自己打包jar?

[main] INFO io.swagger.parser.Swagger20Parser - 从/input/myspec.openapi3.json读取[main] INFO io.swagger.codegen.ignore.CodegenIgnoreProcessor - 找不到.swagger-codegen-ignore文件.线程"main"中的异常java.lang.RuntimeException:缺少swagger输入或配置!at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:685)at io.swagger.codegen.cmd.Generate.run(Generate.java:285)at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java) :35)

看起来swagger-codegen repo 你构建之后运行一个docker容器有一些支持的方式; 我只是希望/猜测有一种支持的方法来做到这一点,而不需要在本地编译,因为我需要在几个地方设置它.

swagger openapi swagger-codegen

10
推荐指数
2
解决办法
1万
查看次数

如何在 Swagger UI 中使用 OpenAPI 3.0 响应“链接”?

我正在编写 Open API 3.0 规范并尝试获取响应链接以在 Swagger UI v 3.18.3 中呈现。

例子:

openapi: 3.0.0
info:
  title: Test
  version: '1.0'
tags: 
  - name: Artifacts
paths:
  /artifacts:
    post:
      tags: 
        - Artifacts
      operationId: createArtifact
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
      responses:
        201:
          description: create
          headers:
            Location:
              schema:
                type: string
                format: uri
                example: /artifacts/100
          content:
            application/json:
              schema:
                type: object
                properties:
                  artifactId:
                    type: integer
                    format: int64
          links:
            Read Artifact:
              operationId: getArtifact
              parameters:
                artifact-id: '$response.body#/artifactId'
  /artifacts/{artifact-id}:
    parameters:
      - name: artifact-id
        in: path
        required: …
Run Code Online (Sandbox Code Playgroud)

swagger swagger-ui openapi

10
推荐指数
1
解决办法
6860
查看次数

openApi 模式中不区分大小写的字符串参数

我有一个带有如下参数的开放 API 规范: 

- name: platform
  in: query
  description: "Platform of the application"
  required: true
  schema:
    type: string
    enum:
      - "desktop"
      - "online"
Run Code Online (Sandbox Code Playgroud)

当我从 URL 获取“平台”参数时,它可以是这样的:

platform=online or 
platform=ONLINE or 
platform=Online or 
platform=onLine  or ... any other format
Run Code Online (Sandbox Code Playgroud)

但是当我要使用它时,它仅在参数全部为小写时才有效,例如"platform=online",显然要匹配枚举值。

如何使模式不区分大小写并理解所有类型的传递参数?

enums openapi

10
推荐指数
1
解决办法
3175
查看次数

AWS CDK 如何根据 OpenApi 规范创建由 Lambda 支持的 API 网关?

我想使用 AWS CDK 来定义 API 网关和 APIG 将代理到的 lambda。

OpenAPI 规范支持x-amazon-apigateway-integration对 Swagger 规范的自定义扩展(在此处详细说明),为此需要 lambda 的调用 URL。如果 lambda 定义在与 API 相同的堆栈中,我在 OpenAPI 规范中看不到如何提供它。我能想到的最好的方法是使用 lambda 定义一个堆栈,然后从中获取输出并运行sed以在 OpenAPI 规范中执行查找和替换以插入 uri,然后使用此修改创建第二个堆栈OpenAPI 规范。

例子:

  /items:
    post:
      x-amazon-apigateway-integration:
        uri: "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:123456789012:function:MyStack-SingletonLambda4677ac3018fa48679f6-B1OYQ50UIVWJ/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        type: "aws_proxy"

Run Code Online (Sandbox Code Playgroud)

一季度。这似乎是一个鸡与蛋的问题,以上是唯一的方法吗?

我尝试使用SpecRestApi CDK 构造的defaultIntegration属性。该文件指出:

除非指定了集成,否则用作此 API 中创建的所有方法的默认值的集成。

这似乎应该能够使用 CDK 规范中定义的 lambda 定义默认集成,因此所有方法都使用此集成,而无需提前知道 lambda 的 uri。

因此我试过这个:

SingletonFunction myLambda = ...

SpecRestApi openapiRestApi = SpecRestApi.Builder.create(this, "MyApi")
                        .restApiName("MyApi")
                        .apiDefinition(ApiDefinition.fromAsset("openapi.yaml"))
                        .defaultIntegration(LambdaIntegration.Builder.create(myLambda)
                                    .proxy(false)
                                    .build())
                        .deploy(true) …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services swagger aws-lambda aws-api-gateway openapi

10
推荐指数
2
解决办法
4979
查看次数