标签: openapi

Swagger Edtior Post 请求示例:无法渲染 n,请参阅控制台

我在 openapi 中呈现示例请求正文时遇到问题。我已经把它提炼成这个简单的例子:

openapi: 3.0.2
info:
  title: Test
  version: "1"
paths:
  /Users:
    post:
      requestBody:
        content:
          application/json:
            example: 
              name: "John"
              
      responses:
        "200":
          description: Fetches them
          content:
            application/json:
              example:
                - name: John Doe
                
Run Code Online (Sandbox Code Playgroud)

https://editor.swagger.io/#和其他一些工具似乎无法呈现请求正文。我得到的是:

Could not render n, see the console.

虽然,响应正文按预期呈现得很好。

我在这里做错了什么

swagger swagger-editor openapi

6
推荐指数
1
解决办法
2196
查看次数

OpenAPI / Swagger 3.0:默认鉴别器值

您如何为每个子类设置默认鉴别器?

例如,采用以下模式:

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
Run Code Online (Sandbox Code Playgroud)

上述模式的代码生成器将创建一个客户端,其中的petType值必须由程序员显式设置。为什么不能默认设置Cat对象?petTypeCat

我尝试使用defaultvalue 让它工作。但是,生成的代码包含隐藏的属性(子级和父级上的相同属性)。

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string …
Run Code Online (Sandbox Code Playgroud)

polymorphism swagger openapi swagger-codegen openapi-generator

6
推荐指数
1
解决办法
572
查看次数

OpenAPI 与 JSON:API

我找不到有关JSON:API和OpenAPI之间用例差异的任何资源

根据我的理解,JSON:API 更侧重于业务数据,而 OpenAPI 更侧重于 REST 本身?

任何指针都会很棒,谢谢!

api rest json-api openapi

6
推荐指数
2
解决办法
1593
查看次数

在 Swagger 中记录 @RequestBody 映射

我正在为各种 API 的文档创建 swagger 文件。在控制器中,我请求一个 RequestParam 和一个 RequestBody:

@PostMapping("/message-now/save-with-params")
    @Timed(value = MetricsTimerConstants.storeMessageWithParamsTimer)
    public ResponseEntity<Object> saveMessageWithParams(@RequestBody Map<String, Object> request,
            @RequestParam List<String> params) { .....
Run Code Online (Sandbox Code Playgroud)

有没有办法在 OpenApi 中定义列表和地图?搜索我在这个主题上一无所获

api rest swagger spring-boot openapi

6
推荐指数
1
解决办法
1468
查看次数

Quarkus Openapi 扩展:在构建时获取生成的 openapi yml 文件

为了生成客户端库,我将在构建时获取 Quarkus 生成的 openapi yml 文件。

目前,我发现获取它的唯一方法是运行服务器并从 /q/openapi 端点获取它,但在这个过程中必须运行服务器才能获得 spec yml 文件,这有点过分了。

该文件将用作输入,以使用 CI 中的生成器生成客户端库。

openapi quarkus

6
推荐指数
1
解决办法
2062
查看次数

NSwag CSharpClientGenerator 的客户端无法反序列化字符串

我们使用 AddOpenApiDocument 得到了 Swagger.json 输出(见下文)。

下面的 swagger 片段显示它返回 application/json 的 200 响应类型。模式部分(我不太熟悉)显示“type”:“string”。

当我们从 NSwag.CodeGeneration.CSharp.CSharpClientGenerator 生成并使用客户端时,我们会收到以下错误:

SwaggerException: Could not deserialize the response body stream as System.String.
Status: 200
Response: 

 ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: P. 
Path '', line 1, position 1.
Run Code Online (Sandbox Code Playgroud)

招摇的片段

"/api/infrastructure": {
  "get": {
    "tags": [
      "Infrastructure"
    ],
    "operationId": "Infrastructure_Ping",
    "responses": {
      "200": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

生成客户端代码如下所示:

                    var status_ = (int)response_.StatusCode; …
Run Code Online (Sandbox Code Playgroud)

swagger asp.net-core openapi nswag openapi-generator

6
推荐指数
1
解决办法
8401
查看次数

drf-spectaulous:使用@extend_schema指定空负载

考虑到我有一个简单的观点,

# serializers.py
class EmptyPayloadResponseSerializer(serializers.Serializer):
    detail = serializers.CharField()


# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema
from .serializers import EmptyPayloadResponseSerializer


class EmptyPayloadAPI(APIView):
    @extend_schema(responses=EmptyPayloadResponseSerializer)
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})
Run Code Online (Sandbox Code Playgroud)

当我渲染架构时,我收到以下错误响应,

错误#0:EmptyPayloadAPI:无法猜测序列化程序。这是 APIView 的优雅后备处理。如果视图在您的控制之下,请考虑使用 GenericAPIView 作为视图基类。暂时忽略视图。

那么,我如何告诉@extend_schema装饰器我不打算传递任何内容作为有效负载?

python django django-rest-framework openapi drf-spectacular

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

如何在Azure Function中配置OpenApiRequestBody?

我想用 OpenApi 注释来装饰我的 azure c# 函数。该函数接受 JSON 模式作为参数。如何在注释中提及这一点。

想知道如何配置下面的注释

[OpenApiRequestBody(contentType: "json", bodyType: typeof(System.Text.Json.JsonDocument), Description = "Parameters",Example =typeof(Parameters))]

public class ModifyOrder
    {
        [FunctionName("ModifyOrder")]
        [OpenApiOperation(operationId: "run", tags: new[] { "Modify Order" })]
        [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
        [OpenApiRequestBody(contentType: "json", bodyType: typeof(System.Text.Json.JsonDocument), Description = "Parameters",Example =typeof(Parameters))]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
        public static async Task<IActionResult> run(
            [HttpTrigger(AuthorizationLevel.Function, "put", Route = null )] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"C# HTTP trigger function processed a request."); …
Run Code Online (Sandbox Code Playgroud)

c# azure openapi

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

无法从 Gitlab 内的 Swagger UI 调用外部 API。内容安全策略错误

I\xe2\x80\x99m 尝试在 GitLab 中使用 OpenAPI 文件(为我的文件运行 UI),但是当我使用从 Swagger UI 尝试/执行功能(向外部 API 发送请求)时,我得到一个内容安全隐私错误。

\n

我的 OpenAPI 可在 Swagger 在线编辑器和 Gitlab don\xe2\x80\x99t 中运行。我认为我需要设置这个外部 URL,但我不知道在哪里设置。也许我的 OpenAPI 代码中有什么东西?或者 GitLab 中的设置?

\n

错误:

\n

在此输入图像描述

\n

gitlab swagger swagger-ui openapi

6
推荐指数
1
解决办法
772
查看次数

安全性未从 Open API 生成器添加到 Swagger

我正在团队中开发一个新项目,我们正在遵循 API 优先方法来实现 API。我们使用openapi-generator-maven-pluginOpenAPI 3.0.3 格式的 yml 文件生成 API。为了生成 swagger 文件,我们使用 springfox 2.9.2。我面临的问题是当我尝试为请求增加安全性时。

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
security:
  - bearerAuth: [ ]
Run Code Online (Sandbox Code Playgroud)

该Authorize按钮不会出现在 swagger 页面中,仅出现请求附近的锁,但它不会执行任何操作(见下图)。

我观察到,如果我打开/v2/api-docsswagger json 不包含安全定义部分。

我设法添加安全性的唯一方法是通过 Docket 对象中的代码添加安全部分,如下所示:

new Docket(DocumentationType.SWAGGER_2)
    .securityContexts(Collections.singletonList(securityContext()))
    .securitySchemes(Collections.singletonList(bearerJwtKey()))
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.example"))
    .paths(PathSelectors.any())
    .build();
Run Code Online (Sandbox Code Playgroud)

这是为 Swagger UI 添加安全性的唯一方法还是我遗漏了什么?

在此输入图像描述

swagger spring-boot openapi openapi-generator

6
推荐指数
1
解决办法
4539
查看次数