Open spmanjunath opened 1 year ago
I can do this with APIs, but would require ISchemaFilter
support - see #400
Looking at the documentation, it seems to assume this to "just work". There are comments in StackOverflow that say this to work with previous versions on Newtonsoft.Json
(12.0), but not anymore.
I wonder if this has worked before or not.
Alright. I was able to find a reason after creating a completely new function app. By default my already existing code had using System.Text.Json
which has JsonConverter
defined. However, this extension requires Newtonsoft.Json.JsonConverter
.
Try using [Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
. Worked for me.
Made a quick example
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Example
{
// Define the enum type with the JsonConverter attribute.
// NOTE: Check that this uses Newtonsoft.Json instead of System.Text.Json.Serialization
[JsonConverter(typeof(StringEnumConverter))]
public enum BusinessFunction
{
Liner = 100000000,
Trucking = 100000001
}
public static class Function
{
// Use the OpenApiParameter attribute to specify the enum type and name
[FunctionName("Function")]
[OpenApiOperation(operationId: "run", tags: new[] { "example" })]
[OpenApiParameter(name: "businessFunction", In = ParameterLocation.Query, Required = true, Type = typeof(BusinessFunction))]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) // Use the enum type as the function parameter
{
var dict = req.GetQueryParameterDictionary();
dict.TryGetValue("businessFunction", out var val );
// Do something with the ruleType parameter
return new OkObjectResult($"You selected {val}");
}
}
}
Any updates on the issue? This workaround doesn't work if you annotate a property with an enum. For example: if you have a property with the standard System.DayOfWeek.
{
[JsonConverter(typeof(StringEnumConverter))]
public DayOfWeek DayOfWeekEnd { get; init; }
}
I have defined a Transport enum as below, i want the Open API Specification to show the enum values as string instead of numeric.
Current behaviour
**Environment