Tsed: How to add optional path parameter using decorators?

Created on 2 Jun 2018  路  4Comments  路  Source: tsedio/tsed

Type |聽Version
ts-express-decorators|3.7.0
Question


Description

How to make an optional path parameter using decorators? I used the following code that works well
with postman but doesn;t work with swagger :(

    @Get('/:page/:offset')
    @Returns(InventoryItem)
    async getRecipe(@PathParams('page') page: number, @PathParams('offset') offset: number): Promise<InventoryItem[]> {
        return await this.inventoryService.getAllInventoryItems(page, offset);
    }
bug

All 4 comments

Just add ? to optional param:

    @Get('/test/:required/:optional?')
    test(@PathParams('required') requiredParam, @PathParams('optional') optionalParam) {
        // your code
    }

Yup I did that but it doesn't work right with the swagger

here's my code:

@Get('recipe/:page?/:offset?')
@Returns(Recipe)
async getRecipe(@PathParams('page') page: number, @PathParams('offset') offset: number): Promise return await this.recipeService.getAllRecipes(page, offset);
}

In swagger it creates 4 parametes, 2 with the names 'page?' and 'offset?' and 2 with the name 'page' and 'offset' (without the ?). Also when I try to execute it from swagger,
it hits the paremters with a '?' for eg.

http://localhost:3000/v1/recipe/0?/1?

Hi @haroon407,
Ok we have a little bug with the swagger description. Need to fix that :)

Thanks for the response :) looking forward to the next release

Was this page helpful?
0 / 5 - 0 ratings