RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

Asp Net Core MVC controller method [FromHeader] parameter attribute #1488

Open alex-leroux opened 6 years ago

alex-leroux commented 6 years ago

Hi, if I generate an Asp Net Core MVC controller using SwaggerToCSharpControllerGenerator, with the following operation specification:

    "/api/Values/echoheaders":
    {
        "get":
        {
            "tags":
            [
                "Values"
            ],
            "operationId": "Values_EchoHeaders",
            "parameters":
            [
                {
                    "type": "string",
                    "name": "Authorization",
                    "in": "header",
                    "required": true,
                    "x-nullable": true
                },
                {
                    "type": "string",
                    "name": "customHeader",
                    "in": "header",
                    "required": true,
                    "x-nullable": true
                }
            ],
            "responses":
            {
                "200":
                {
                    "x-nullable": true,
                    "description": "",
                    "schema":
                    {
                        "type": "array",
                        "items":
                        {
                            "type": "string"
                        }
                    }
                }
            }
        }
    },

I get the following code for the operation:

    [Microsoft.AspNetCore.Mvc.HttpGet, Microsoft.AspNetCore.Mvc.Route("api/Values/echoheaders")]
    public System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<string>> EchoHeaders(string authorization, string customHeader)
    {
        return _implementation.EchoHeadersAsync(authorization, customHeader);
    }

I think that for this method to work, the parameters require the [FromHeader] attribute. Is this correct, or does this work some other way?

Thank you for your time. Alex le Roux

RicoSuter commented 6 years ago

You are right, we need to generate [FromHeader] attributes here, but they don't exist for ASP.NET (not core) so maybe we need to exclude header parameters for old ASP.NET and add [FromHeader] for ASP.NET Core

RicoSuter commented 5 years ago

PR: https://github.com/RicoSuter/NSwag/pull/2033