AzureExtensions.Swashbuckle icon indicating copy to clipboard operation
AzureExtensions.Swashbuckle copied to clipboard

Support for JsonProperty?

Open ben-002 opened this issue 3 years ago • 5 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)

ben-002 avatar Mar 23 '21 11:03 ben-002

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.

vitalybibikov avatar Mar 24 '21 13:03 vitalybibikov

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 avatar Mar 24 '21 13:03 cosminstirbu

@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?

vitalybibikov avatar Mar 24 '21 14:03 vitalybibikov

As per https://github.com/Azure/azure-functions-host/issues/5469#issuecomment-573899544 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.

cosminstirbu avatar Mar 24 '21 14:03 cosminstirbu

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: https://github.com/vitalybibikov/AzureExtensions.Swashbuckle/pull/108

jfjalburquerque avatar Mar 07 '24 12:03 jfjalburquerque

Fixed, please reopen if needed

vitalybibikov avatar May 02 '24 11:05 vitalybibikov