vitalybibikov/AzureExtensions.Swashbuckle

Support for JsonProperty?

Closed this issue · 6 comments

Is there a way to make use of JsonProperty? I am trying to have a specific name for some fields in swagger ui and swagger json file. Currently the JsonProperty has no effects.

I tried with services.AddSwaggerGenNewtonsoftSupport() from Swashbuckle.AspNetCore.Newtonsoft V6.0.3, but no results.

If there is no support for this, what other possibilities do I have to customize filed names?
I am using an Azure Function targeting .NET Core 3.1 and the latest AzureExtensions.Swashbuckle(v3.2.2)

Hi @ben-002

You can achieve it with JsonPropertyName of System.Text.Json.Serialization;

Newtonsoft is not supported at the moment. And it might happen that it won't be.

Hi @vitalybibikov,

We're also interested in some way to configure what JSON library is used for handling these annotations.

For now we have forked the repo and added in SwashbuckleConfig.Initialize the following line services.AddSwaggerGenNewtonsoftSupport();

It would be great if we could be able to configure this from outside.

Thank you,
Cosmin

@cosminstirbu

Hi, Is it possible for you to use System.Text.Json instead?
Is there any specific use case why you've stuck with the Newtonsoft?

I don't want to add Newton, due to the fact, that it will bring more issues.

Technically, it's possible, for instance to unseal SwashbuckleConfig class, make Initialize virtual => to allow something like:

        public CustomConfig(
            IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider,
            IOptions<SwaggerDocOptions> swaggerDocOptions,
            SwashBuckleStartupConfig startupConfig,
            IOptions<HttpOptions> httpOptions)
            : base(apiDescriptionGroupCollectionProvider, swaggerDocOptions, startupConfig, httpOptions)
        {
        }


        public override void Initialize(ExtensionConfigContext context)
        {
            this.Services.AddSwaggerGenNewtonsoftSupport();
            base.Initialize(context);
        }

Will it be OK?

As per Azure/azure-functions-host#5469 (comment) it seems that functions v3 uses Newtonsoft.Json.

Switching to System.Text.Json would require a relatively significant effort on our side at this point.

Your suggested approach seems like a good start, however Services.AddSwaggerGenNewtonsoftSupport(); should be called after calling base.Initialize(context) which currently calls Services.AddSwaggerGen

Also SwashbuckleConfig is used in the startup extensions, so we would need a way to control that as well.

Hi, I am experiencing same problem here is exposed so I have created a new PR that allow to use Newtonsoft if the developer desire it and it is disabled by default.

I hope it could help to this amazing project.
PR: #108

Fixed, please reopen if needed