Swashbuckle HealthCheck Endpoint Exposing Exception Details After Switching from Newtonsoft.Json to System.Text.Json

60 Views Asked by At

I recently switched my .NET 6 project from using Newtonsoft.Json to System.Text.Json. After making this change, I've encountered an unexpected behavior with one of my health check endpoints that utilizes Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheck.

Before the switch, the Exception object in the Swagger UI response schema for this endpoint was not exposing any details, as desired. Now, after removing AddSwaggerGenNewtonsoftSupport and making the switch, the same endpoint's Exception object is exposing internal properties (see image below with System.Text.Json).

Current Swagger UI with System.Text.Json:

For comparison, here is the Swagger UI with Newtonsoft.Json where the exception object was empty:

I have not made any other changes to the configuration of Swashbuckle or the health check endpoint. It seems like the default behavior for handling exceptions has changed with System.Text.Json.

I'm looking for a way to configure Swashbuckle or System.Text.Json to prevent these internal details from being exposed in the Swagger UI. Any suggestions on how to revert to the previous behavior where the Exception class does not expose its properties would be greatly appreciated.

0

There are 0 best solutions below