yuka1984 / azure-functions-extensions-swashbuckle

MIT License
33 stars 29 forks source link

How To - Request Parameters #36

Closed kevinchatham closed 4 years ago

kevinchatham commented 4 years ago

Edit: I figured it out! If you're not interested in the problem see below for the answer.

Hello,

I've been trying to figure this out and I'm sure I've missed something simple. If anyone here could point me in the right direct I'd be very grateful.

I have an Azure Function v3 project built that implements nuget packages from this fork. My csproj is generating the XML doc and Swagger Gen/UI seems to be reading the function summaries. Just for clarity my hosts.json file looks like this...

// removed for brevity //
"Swashbuckle": {
    "XmlPath": "Functions.xml"
}

My problem is XML parameter properties like <param name="Id">Id of menu</param> are not reflected in the Swagger UI as input parameters. The only input parameter I have 'code' which I believe is the function specific API key. I am just trying to get the function input parameters into my Swagger doc.

Does anyone have this working and, if so, can you provide an example? I have tried various settings adjustments and looking through this repo's issues. I also removed my XML configuration and confirmed that all of my summaries / etc went away.

Previously I've implemented this in asp.net core api projects and it always picks up the method model properties as inputs with little issue. In functions the entire request is passed in so I though I would use <param>.

Thanks, Kevin

kevinchatham commented 4 years ago

Sometimes you just have to dig deeper to answer your own questions! I got it working! To elaborate, use this...

Query parameters in a GET method

[QueryStringParameter("Id", "Id of menu.", DataType = typeof(int), Required = true)] // * THIS
[FunctionName(nameof(Routes.GetMenuById))]
public async Task<IActionResult> GetMenuById(
    [HttpTrigger(AuthorizationLevel.Function, Http.Get, Route = Routes.GetMenuById)]
    HttpRequest req,
    ILogger log)
{}

Request body in POST method

[FunctionName(nameof(Routes.AddMenu))]
public async Task<IActionResult> AddMenu(
    [HttpTrigger(AuthorizationLevel.Function, Http.Post, Route = Routes.AddMenu)] 
    [RequestBodyType(typeof(AddMenuRequestModel), description: "Add Menu request body")] // * THIS
    HttpRequest req,
    ILogger log)
{}

This part of the repo really helped out. Hopefully this helps someone in the future.

Kevin