代码之家  ›  专栏  ›  技术社区  ›  A.L

如何在API平台中使用“$ref”引用和声明Swagger中的组件?

  •  1
  • A.L  · 技术社区  · 6 年前

    我们可以 define components in Swagger :

    components:
      schemas:
        User:
          properties:
            id:
              type: integer
            name:
              type: string
    

    稍后使用此组件:

    responses:
      '200':
        description: The response
        schema: 
          $ref: '#/components/schemas/User'
    

    我想用这个来避免重复内容。


    components:
        schemas:
            Part:
                description: Array of Part
                type: array
                items:
                    type: object
                    properties:
                        name:
                            type: string
    
    App\Entity\Item:
        collectionOperations:
            post:
                method: 'POST'
                swagger_context:
                    parameters:
                        - name: body
                          description: Item data
                          in: body
                          schema:
                              type: object
                              properties:
                                  name:
                                      description: Part
                                      type: string
                                      required: true
                                  part:
                                      $ref: '#/components/schemas/Part'
    

    处理异常时引发异常(Symfony\Component\Config\Exception\FileLoaderLoadException:在中找不到资源“components”)(正在从“/app/config/routes/api\u platform.yaml”导入。确保有一个加载程序支持“api\u platform”类型。)

    components 项目。


    如何在API平台中定义和使用引用?如何定义引用并在多个YAML文件中使用它?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kévin Dunglas esboych    6 年前

    你不能这样做。

    这个 components 密钥属于Swagger/OpenAPI格式,而不是API平台配置(映射)格式。API平台配置文件和Swagger定义都可以用YAML编写,但它们并不相关。

    不能直接注入API平台的配置文件中。

    API平台的配置允许使用 swagger_context 键,但不能编写随机的招摇定义(例如 component 键)在该结构外部。

    做你想做的事 键将是不够的(组件必须在Swagger文件的根目录下注入,而使用 ).

    您必须为Swagger文档生成器创建一个装饰器,而不是使用此键,如本文档条目中所述: https://api-platform.com/docs/core/swagger/#overriding-the-swagger-documentation

    Decorator允许访问整个Swagger结构,并对其进行修改。所以你可以添加 组件 结构。

        2
  •  0
  •   Abdel    5 年前

    有可能。。。看到了吗 How can I annotate my attribute which is Value Object in order that API Platform would generate its fields for swagger documentation?

    在我的示例中,我有实体检查器,我创建了两个组:

     * @ApiResource(
     *     attributes={
     *          "normalization_context"={"groups"={"read"}},
     *          "denormalization_context"={"groups"={"write"}},
     *     },
    

    然后在大摇大摆的背景下回答:

     *                  "responses" = {
     *                      "201" = {
     *                          "description" = "....",
     *                          "schema" =  {
     *                              "type" = "object",
     *                              "properties" = {
     *                                   "myresult" = {
     *                                      "$ref"="#/definitions/Checker-read"
     *                                   }