如何记录OpenAPI中的JSON合并补丁端点?

发布于 2025-02-04 03:37:08 字数 971 浏览 4 评论 0原文

我已经实施了通过OpenAPI记录的REST API。详细说明,该规范是使用SpringDoc-Openapi从Java源代码生成的。

但是,我需要接受补丁。因此,我

  /customers/{id}:
    patch:
      tags:
        - Customers
      summary: Updates an existing user
      description: Updates an existing user
      operationId: partialUpdateMergePatchCustomer
      parameters:
        - name: id
          in: path
          description: The numeric ID of the customer
          required: true
          schema:
            type: integer
            format: int64
      requestBody:
        content:
          application/merge-patch+json:
            schema:
              type: object
        required: true

已经阅读了OpenAPI规范/文档,但没有找到有关JSON MERGE补丁,JSON补丁等的任何信息。

我正在遇到以下我想克服的问题:

  1. 在这里要修补哪种类型的资源。其余的API,特别是。在Swaggerui中,有充分的文献记载,并且到处都有模式参考。如何指定实际要修补的资源模式?
  2. 除了纯模式参考,在我的API中,一些资源具有仅阅读字段,或者可能还适用于我想记录的某些字段的其他限制。作为原始架构的一部分,用自然语言做它有更好的方法吗?

I have implemented a REST API which is documented via OpenAPI. In detail, the specification is generated from Java source code using springdoc-openapi.

However, I have the need for accepting patches. So, I

  /customers/{id}:
    patch:
      tags:
        - Customers
      summary: Updates an existing user
      description: Updates an existing user
      operationId: partialUpdateMergePatchCustomer
      parameters:
        - name: id
          in: path
          description: The numeric ID of the customer
          required: true
          schema:
            type: integer
            format: int64
      requestBody:
        content:
          application/merge-patch+json:
            schema:
              type: object
        required: true

I have read through the OpenAPI specification/documentation but did not find any information regarding JSON Merge Patch, JSON Patch or the like.

I am experiencing the following issues I'd like to overcome:

  1. There is no clue information of which type of resource is to be patched here. The rest of the API, esp. in SwaggerUI, is well documented and has schema references everywhere. How can I specify the resources schema that actually is to be patched?
  2. Apart from the pure schema reference, in my API, some resources have read-only fields or there may be other restrictions that apply to certain fields that I'd like to document. Is there a better way as doing it in natural language as part of the original schema?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2025-02-11 03:37:08

我的问题与您的第1期相同。也就是说,与SpringDoc-Openapi生成的默认值相比,提供更多的信息来宣传JSONPATCH请求Boby参数。我将在此处发布可能会遇到同一问题的其他人的解决方案。

我的解决方案是将以下注释添加到RestController补丁方法:

@PatchMapping(value = "/{id}", consumes = "application/json-patch+json")
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(array = @ArraySchema(schema = @Schema(implementation = JsonPatchSchema.class))))
    public void patch(@PathVariable @Min(0) long id, @RequestBody JsonPatch patch) {

其中JSONPATCHSCHEMA包含请求的格式:

public class JsonPatchSchema {
    @NotBlank
    public Op op;

    public enum Op {
        replace, add, remove, copy, move, test
    }

    @NotBlank
    @Schema(example = "/name")
    public String path;

    @NotBlank
    public String value;
}

使用@schema注释您可以提供有关允许的内容的更多详细信息,请参见。例如,模式参数允许您定义正则表达式。不幸的是,我还没有找到一种生成允许内容的方法。

I had the same issue as your issue #1. That is, providing more info to swagger on the JsonPatch RequestBody parameter than the default generated by springdoc-openapi. I'm posting my solution here for others who may run into the same issue.

My solution was to add the following annotation to the RestController patch method:

@PatchMapping(value = "/{id}", consumes = "application/json-patch+json")
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(array = @ArraySchema(schema = @Schema(implementation = JsonPatchSchema.class))))
    public void patch(@PathVariable @Min(0) long id, @RequestBody JsonPatch patch) {

Where JsonPatchSchema contains the requested format:

public class JsonPatchSchema {
    @NotBlank
    public Op op;

    public enum Op {
        replace, add, remove, copy, move, test
    }

    @NotBlank
    @Schema(example = "/name")
    public String path;

    @NotBlank
    public String value;
}

Using the @Schema annotation you could provide more detail on what is allowed, see the Schema javadoc. For example the pattern parameter allows you to define a regular expression. I haven't found a way to generate the allowed content unfortunately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文