如何只在内部Openapi YAML模型中包含x-implements标志?

huangapple go评论56阅读模式
英文:

How to include the x-implements flag in internal Openapi YAML models only?

问题

以下是您要翻译的内容:

我正在使用 x-implements 标志来定义我的 openapi 模式,如下所示:

paths:
  /pets:
    get:
      summary: 列出所有宠物
      operationId: 列出宠物
      tags:
        - 宠物
      responses:
        '200':
          description: 一组宠物。
          content:
            application/json:
              schema:
                type: 数组
                items:
                  $ref: '#/components/schemas/宠物'

components:
  schemas:
    宠物:
      x-implements: ['com.petstore.PetInterface']
      type: 对象
      required:
        - 名称
      properties:
        id:
          type: 整数
          format: int64
        名称:
          type: 字符串
        标签:
          $ref: '#/components/schemas/标签'

我已经在我的代码中定义了 com.petstore.PetInterface。我正在使用 maven-openapi-generator 生成代码,这对于我的内部使用很有效。然而,我将要将 openapi 文件交给客户,他们没有这个接口。如何处理这个问题?我考虑了以下解决方案:

  1. 在交付文件给客户之前(手动)删除 x-implements 标志。
  2. 创建内部和外部模式,但我认为这会导致大量的代码重复。

是否有更好的解决方案?谢谢。

英文:

I am using use the x-implements flag for my openapi schema as follows:

paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      responses:
        '200':
          description: A list of pets.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'

components:
  schemas:
    Pet:
      x-implements: ['com.petstore.PetInterface']
      type: object
      required:
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          $ref: '#/components/schemas/Tag'

I have defined the com.petstore.PetInterface in my code. I am generating the code with the maven-openapi-generator and this all works fine for my internal usage. However, I am going to deliver the openapi file to the customer and they don't have this interface. How to deal with this? I was thinking about the following solutions:

  1. delete the x-implements flag (manually) before delivering the file to the customer
  2. create an internal and external schema, but I guess this results in a lot of code duplications

Are there any better solutions? Thanks.

答案1

得分: 0

我正在考虑以下解决方案:

  1. 在将文件交付给客户之前(手动)删除x-implements标志

这听起来不错,似乎是常见情况,因为存在诸如openapi-filteropenapi-format之类的工具。

在你的情况下,你可以使用yq自动删除x-implements键。下面是针对yq v4 的示例;-i标志会原地更新输入文件:

# Bash
yq -i 'del(.components.schemas.*["x-implements"])' openapi.yaml

# Windows CMD
yq -i "del(.components.schemas.*[\"x-implements\"])" openapi.yaml
英文:

> I was thinking about the following solutions:
> 1. delete the x-implements flag (manually) before delivering the file to the customer

This sounds fine and seems to be a common use case, given that tools like openapi-filter and openapi-format exist.

In your case, you can automate the deletion of the x-implements key by using yq. The example below is for yq v4; the -i flag updates the input file in-place:

# Bash
yq -i 'del(.components.schemas.*["x-implements"])' openapi.yaml

# Windows CMD
yq -i "del(.components.schemas.*[\"x-implements\"])" openapi.yaml

huangapple
  • 本文由 发表于 2023年5月23日 00:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定