vitalybibikov / AzureExtensions.Swashbuckle

This extension enriches Azure Functions with Swagger/ Open API support
https://www.linkedin.com/in/vitali-bibikov/
MIT License
68 stars 54 forks source link

Compatibility Issues with .NET 8 In-Process Model #117

Open abaazzi opened 4 months ago

abaazzi commented 4 months ago

Hi, The last version is compatible only with isolated worker model in azure function, is there something planned for in-process model?

vitalybibikov commented 4 months ago

Hello, at the moment no, as In-Process model is going to be deprecated beginning 10 November 2026.

If there will be enough requests, I will implement it, though I need to understand how many people need it.

kkdubey commented 2 months ago

@vitalybibikov I'm using in-process model in multiple solution. getting compatibility issue after upgrade to .Net 8

jcoolsen commented 1 month ago

We are moving to .NET 8 because of the end-of-support for .NET 6/7. Switching to isolated model is too large a task for us at the moment. Since this project (v4) does not currently support the in-process model, we must stay at v3. Unfortunately there is a new issue with JsonSerializerOptions in .NET 8 that breaks v3. The issue is described here: https://github.com/dotnet/aspnetcore/issues/55692 with the simple solution/work-around noted in this comment: https://github.com/dotnet/aspnetcore/issues/55692#issuecomment-2392448375 It occurs in the AddSwashBuckle method: image If version 3 could be updated with the TypeInfoResolver initialized on JsonSerializerOptions in this mehtod, that would be great.

jcoolsen commented 1 month ago

Wow, the referenced issue was deleted just now! That makes no sense to me. Anyway I had it open in another tab and was able to save a copy of the page here: Dotnet 8 JsonSerializerOptions Breaking change · Issue #55692 · dotnet_aspnetcore.pdf The last comment has the solution.

EDIT: ... and now it's undeleted. Oh, well.

fahdalaoui commented 1 month ago

That's so needed in our project that has multiple micro services that use in-process model

we can't migrate all the project to .Net 8 and to be isolated model in specific

AyoubOd commented 1 month ago

Hi, I’m facing the same issue. We have nearly 20 projects that rely on the in-process model, so it would be really helpful if support for the latest version, which is currently compatible only with the isolated worker model in Azure Functions, could be added

senthilkcg commented 1 month ago

We are also facing the same issue in .Net8 Inproc mode. Since we have time till 2026 for inproc, could you please release the fix for InProc mode ?

jvmlet commented 1 month ago

Same here, we are migrating in-proc AZ functions to dotnet8 in-proc support. Please support this as well. Thanks

jvmlet commented 3 weeks ago

@vitalybibikov , can you please at least patch the 3.3.2 by changing

SystemTextJsonOutputFormatter implementationInstance = new SystemTextJsonOutputFormatter(new JsonSerializerOptions());

to

SystemTextJsonOutputFormatter implementationInstance = new SystemTextJsonOutputFormatter(new JsonSerializerOptions({
TypeInfoResolver = new DefaultJsonTypeInfoResolver(); 
}));

in AddSwashBuckle method .

jcoolsen commented 3 weeks ago

Our current workaround may be helpful for some of you.

    internal class SwashbuckleStartup : FunctionsStartup {
        public override void Configure(IFunctionsHostBuilder builder) {
            // https://github.com/vitalybibikov/AzureExtensions.Swashbuckle/issues/117
            // https://github.com/dotnet/aspnetcore/issues/55692
            // builder.AddSwashBuckle(Assembly.GetExecutingAssembly());
             AddSwashBuckleCustom(builder, Assembly.GetExecutingAssembly());
        }

        public static IFunctionsHostBuilder AddSwashBuckleCustom(IFunctionsHostBuilder builder, Assembly assembly, Action<SwaggerDocOptions> configureDocOptionsAction = null) {
            AddSwashBuckleCustom(builder.Services, assembly, configureDocOptionsAction);
            return builder;
        }

        public static IServiceCollection AddSwashBuckleCustom(IServiceCollection services, Assembly assembly, 
            Action<SwaggerDocOptions> configureDocOptionsAction = null, IWebJobsBuilder webJobsBuilder = null) {

            webJobsBuilder ??= services.AddWebJobs(_ => { });

            webJobsBuilder.AddExtension<SwashbuckleConfig>()
                .BindOptions<SwaggerDocOptions>()
                .ConfigureOptions<SwaggerDocOptions>((configuration, section, options) => configureDocOptionsAction?.Invoke(options));

            services.AddSingleton<IModelMetadataProvider>(new EmptyModelMetadataProvider());
            services.AddSingleton(new SwashBuckleStartupConfig {
                Assembly = assembly
            });

            var formatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions() {
                TypeInfoResolver = new DefaultJsonTypeInfoResolver() // This is required in .NET 8 but is not part of the SwashBuckle v3 implementation.
            });
            services.AddSingleton<IOutputFormatter>(formatter);

            // Below is reflection implementation of this:
            // services.AddSingleton<IApiDescriptionGroupCollectionProvider, FunctionApiDescriptionProvider>();

            Type providerInterfaceType = typeof(IApiDescriptionGroupCollectionProvider);
            Type providerType = typeof(SwashBuckleStartupExtension).Assembly.GetType("AzureFunctions.Extensions.Swashbuckle.SwashBuckle.Providers.FunctionApiDescriptionProvider");

            MethodInfo method=typeof(ServiceCollectionServiceExtensions).GetMethod(nameof(ServiceCollectionServiceExtensions.AddSingleton), 2,
                BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IServiceCollection) }, null);

            MethodInfo genericMethod = method.MakeGenericMethod(providerInterfaceType, providerType);
            genericMethod.Invoke(null, new object[] { services });

            return services;
        }
    }