azure-functions-openapi-extension icon indicating copy to clipboard operation
azure-functions-openapi-extension copied to clipboard

An optional string parameter is not passed correctly by swagger ui

Open GilShalit opened this issue 4 years ago • 4 comments

An Azure function (V3) is declared with an optional string parameter. it is sent incorrectly to the function from the swagger UI regardless if the parameter is specified or left empty.

        [OpenApiOperation(operationId: "Function1", tags: new[] { "Function" }, Summary = "Show ",Visibility = OpenApiVisibilityType.Important)]
        [OpenApiParameter("filter", Type = typeof(string), In = ParameterLocation.Path, Visibility = OpenApiVisibilityType.Important, Description = "Optional string", Required =false )]
        [Function("Function1")]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "MyRoute/{filter?}")] HttpRequestData req,
            FunctionContext executionContext, string filter)
        {
            var logger = executionContext.GetLogger("Function1");
            logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString($"The filter value is {(filter==null?"null":filter)}");

            return response;
        }
    }

when tested from the address bar of a browser, the response is as expected: http://localhost:7071/api/myroute/XYZ returns: The filter value is XYZ http://localhost:7071/api/myroute returns: The filter value is null

but going through the Swagger UI, the response is: The filter value is {filter

I have created a repository at https://github.com/GilShalit/SwaggerBug to demonstrate.

GilShalit avatar Oct 21 '21 13:10 GilShalit

@GilShalit Thanks for the issue! I'll take a look.

justinyoo avatar Oct 27 '21 22:10 justinyoo

Hi, This is still happening in OpenAPI 1.0.0 on .Net 6 :-\

Thank you for your work!

GilShalit avatar Nov 18 '21 15:11 GilShalit

Still the case in v1.1.0 Code by TS is the way to reproduce

vrooijen avatar Feb 23 '22 13:02 vrooijen

I checked the OpenAPI spec (v3.0.1) about this: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.1.md#paths-object

Unfortunately, the spec doesn't support the wildcard characters in the path template. In other words, if you declare path template, the path part MUST be required value, not optional - it MUST NOT be nullable.

justinyoo avatar Feb 24 '22 04:02 justinyoo