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

New feature: `JsonSerializerSettings` or `NamingStrategy` to be injectable

Open justinyoo opened this issue 3 years ago • 5 comments

Currently, both JsonSerializerSettings and NamingStrategy are not injectable. It should be injectable by devs.

justinyoo avatar Aug 30 '22 15:08 justinyoo

Hi, I injected for my v4 azure func in start up file settings for NewtonJson builder.Services.AddMvcCore() .AddNewtonsoftJson(x => x.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new DefaultNamingStrategy() }); This solution works for my http trigger response, but it doesn`t work in my swagger definition image

power911 avatar Oct 04 '22 13:10 power911

Hello!

If you're looking for a way to customize the naming strategy, you can create a new class that inherits from the "OpenApiHttpTriggerContext" class and override the "NamingStrategy" property with your desired naming strategy. Then, you can register the new class with the Dependency Injection container so that it will override the default registration.

Here's an example of how you could do this in your code:

// Create a new class that inherits from OpenApiHttpTriggerContext and override the NamingStrategy property
public class SnakeCaseNamingOpenApiHttpTriggerContext : OpenApiHttpTriggerContext
{
    public override NamingStrategy NamingStrategy { get; } = new SnakeCaseNamingStrategy();
}

// Then register it for type IOpenApiHttpTriggerContext
services.AddSingleton<IOpenApiHttpTriggerContext, SnakeCaseNamingOpenApiHttpTriggerContext>();

kevindost avatar Apr 23 '23 11:04 kevindost

I have a class implementing OpenApiHttpTriggerContext

using Microsoft.Azure.WebJobs.Extensions.OpenApi;
using Newtonsoft.Json.Serialization;

namespace MyProject
{
    public class SnakeCaseNamingOpenApiHttpTriggerContext : OpenApiHttpTriggerContext
    {
        public override NamingStrategy NamingStrategy { get; }

        public SnakeCaseNamingOpenApiHttpTriggerContext() => NamingStrategy = new SnakeCaseNamingStrategy();
    }
}

and tried to inject it in Startup:

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(MyProject.Startup))]
namespace MyProject
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IOpenApiHttpTriggerContext, SnakeCaseNamingOpenApiHttpTriggerContext>();
        }
    }
}

but the injection never happens and the default camel case is still used.

Edit My original approach was to use:

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Include,
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK",
                Formatting = Formatting.Indented,
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new SnakeCaseNamingStrategy()
                }
            };

but this throws an exception

Object reference not set to an instance of an object.

   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.<>c.<GetRuntimeFilenameAsync>b__45_0(DependencyManifest manifest)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.GetRuntimeFilenameAsync(String functionAppDirectory)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.SetApplicationAssemblyAsync(String functionAppDirectory, Boolean appendBin)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions.OpenApiHttpTriggerContextExtensions.AuthorizeAsync(Task`1 context, IHttpRequestDataObject req)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctions.RenderSwaggerUI(OpenApiHttpTriggerContext openApiContext, HttpRequest req, ExecutionContext ctx, ILogger log)

toriverly avatar Jul 25 '23 21:07 toriverly

Hi contributors,

I've been following the progress of this library and I'm particularly interested in configuring JsonSerializerSettings. I noticed that this was included in a milestone but it seems the timeline has passed several months without an update.

Could you please share any updates or insights on when this functionality might be available?

Thank you!

m-khooryani avatar Aug 09 '23 19:08 m-khooryani