0

我在 Php Laravel 中编写 API 并使用 swagger (2.0) 注释(lib: darkaonline/l5-swagger使用swagger-php)来生成 swagger.json。我有以下招摇:

@SWG\Definition(
    definition="Space",
    @SWG\Property( property="id", type="integer", example=33),
    @SWG\Property( property="name", type="string" ),
    @SWG\Property( property="dataA", type="string", example="very long data string" ),
    @SWG\Property( property="dataB", type="string", example="very long data string" ),
),

@SWG\Get(
    path="/api/v1/client/space/list",
    @SWG\Response( response=200, description="OK", 
        @SWG\Schema(
               type="array", 
               @SWG\Items(ref="#/definitions/Space"), 
        )
    )
)

上面的 api 应该返回Spaces 列表(显示在表中),但我只需要获取idname - 但是Space也有非常重的字段dataAdataB - 在表中不需要这些字段。有没有办法在不为响应创建单独的空间定义的情况下排除这些字段(以避免违反“不要重复自己”规则)?是否有一些机制可以做这样的事情:

@SWG\Items(ref="#/definitions/Space", exclude={"dataA","dataB"}),  

和/或排除更多嵌套字段,例如

exclude={"dataA.securityField","dataA.someList[].heavyField"}

?

PS:我在这里也将其报告为问题/问题。

4

1 回答 1

1

目前不存在诸如排除之类的功能的实现(看这里)。但是,您可以尝试遵循部分可接受的方法(您创建新定义,但此定义重用空间定义的部分):

@SWG\Definition(
    definition="SpaceListEntry",
    @SWG\Property( property="id", ref="#/definitions/Space/properties/id" ),
    @SWG\Property( property="name", ref="#/definitions/Space/properties/name" ),
) 

@SWG\Get改为@SWG\Items(ref="#/definitions/Space"),_

@SWG\Items(ref="#/definitions/SpaceListEntry"),

此解决方案部分令人满意,但比空间定义的完整副本更好。

于 2017-12-01T14:24:01.670 回答