Search code examples
phpannotationsswagger-2.0swagger-php

How to exclude some (nested) properties from models used in @SWG\Response


I write API in Php Laravel and use swagger (2.0) annotations (lib: darkaonline/l5-swagger which use swagger-php) to generate swagger.json. I have following swagger:

@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"), 
        )
    )
)

Above api should return list of Spaces (to show in table) but I only need to get id and name - however Space also have very heavy fields dataA and dataB - which are non needed in table. Is there a way to exclude these fields without creating separate Space definition for response (to avoid breaking "don't repeat yourself" rule)? Is there some mechanism to do something like this:

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

And/or exclude more nested fields like

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

?

PS: I also report this as question/issue here.


Solution

  • Currently there no exists implementation for such functionality as exclude (look here). However you can try following partially acceptable approach (you create new definition but this definition reuse parts of Space definition):

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

    And in @SWG\Get change @SWG\Items(ref="#/definitions/Space"), to

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

    This solution is partially satisfying but better than full copy of Space definition.