-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Make SerializerSetings
properties get-only and protected
#4409
Comments
Yeah this seems a bit weird. I don't think you can effectively change any of these settings after startup. You'd cause crazy race conditions and the world would likely end. @rynowak @dougbu any sense whether there was some scenario here at some point? I suspect we can delete some of this "guard" code, no? |
@Eilon this code may exist only for unit testing scenarios. I don't recall a production scenario. Then again, output formatters are more likely to be created from user code than input formatters e.g. within a custom |
You might be able to customize formatters on a per-request basis, but definitely not by modifying the instance that's already in the |
Yeah, we might want to just make it clearer what's supported and not by taking in the settings in the constructor. Having the settings in a mutable property is a holdover from WebAPI2 and it might be a distraction from the intended pattern for configuring the formatter. |
Yeah let's clean up the ctor's and make the properties get-only. |
I am a bit unclear. So what is the final change that needs to be made here?
|
What @rynowak said, take the settings on the constructor and make the property read only. We can have a parameter-less constructor that initializes it with the default settings and that the user can further tweak. The goal here is not to be correct as you can't be 100% without hiding the property or having it be immutable, the goal is to declare intention by not having extra code that implies this is an scenario we care to support. IMO any formater should be fully configured before it processes its first request/response (basically fully configure them during startup in options, or before passing them to ObjectResultExecutor). If you are doing something really advanced and require different settings, then have different formatter instances. The point is that assuming the formatter is only configured before its first use simplifies the code and makes clearer what the intended supported scenarios are. I wouldn't complain if you had a static property or something like that on the formatter that is DefaultSettings if @rynowak is ok with it, in any case, although "internal" the code you mention is totally accessible to customers, and the configuration done there is not something they can't do by themselves. |
|
|
@dougbu you meant |
@kichalla yes to both questions. |
New overall description: Remove extra options to manipulate `JsonSerializerSettings` - #4339: remove non-recommended JSON formatter constructors - affects `JsonInputFormatter`, `JsonOutputFormatter`, `JsonPatchInputFormatter` - `JsonOutputFormatter` cleanup also impacts `JsonHelper` - make `SerializerSettingsProvider` class public and use it - #4409: make `SerializerSetings` properties get-only and `protected` - affects `JsonInputFormatter`, `JsonOutputFormatter` Recommended patterns: - change `JsonSerializerSettings` values in `MvcJsonOptions` for almost all customizations - find `JsonOutputFormatter` in `MvcOptions.OutputFormatters` when limiting per-result formatters - start with `SerializerSettingsProvider.CreateSerializerSettings()` when customizing a per-result formatter
- #4339: remove non-recommended JSON formatter constructors - affects `JsonInputFormatter`, `JsonOutputFormatter`, `JsonPatchInputFormatter` - `JsonOutputFormatter` cleanup also impacts `JsonHelper` - rename and make `SerializerSettingsProvider` class public; use it as appropriate - #4409: make `SerializerSetings` properties get-only and `protected` - affects `JsonInputFormatter`, `JsonOutputFormatter` Recommended patterns: - change `JsonSerializerSettings` values in `MvcJsonOptions` for almost all customizations - find `JsonOutputFormatter` in `MvcOptions.OutputFormatters` when limiting per-result formatters - start with `JsonSerializerSettingsProvider.CreateSerializerSettings()` when customizing a per-result formatter
SerializerSetings
properties get-only and protected
Why SerializerSettings settings are now protected? How can I change default settings? for example private void ConfgureFormatters(MvcOptions options)
{
JsonInputFormatter input = (JsonInputFormatter)options.InputFormatters.First(item => item is JsonInputFormatter);
JsonOutputFormatter output = (JsonOutputFormatter)options.OutputFormatters.First(item => item is JsonOutputFormatter);
input.SerializerSettings.ContractResolver = new PascalCasePropertyNamesContractResolver();
output.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
output.SerializerSettings.Formatting = Formatting.Indented;
} It looks like overkill to drop all formatters and create new ones. And for now for some reason you do not have default settings for input and output formatters. As well as solution of @javiercn... it is very nice from point of view of tricking but absolutely bad for real development... 50 lines of code.. additional class... just to set 2 properties in some formatters. |
@maxim-boligatov I answered your questions in d8d2e54#commitcomment-18275275 |
JsonOutputFormatter "allows" to change the settings at runtime, this is different from what JsonInputFormatter does, so It leaves me wondering, is there really an scenario for changing the settings after startup, or is this just the case that we wrote the code but didn't think the scenarios through?
I don't think we should support this scenario as it's inconsistent with the scenarios in JsonInputFormatter and I can't see an advantage of doing this over replacing the formatter in the list of formatters in the options. I believe Options has support for this built in https://github.com/aspnet/Options/blob/dev/test/Microsoft.Extensions.Options.Test/OptionsMonitorTest.cs
The text was updated successfully, but these errors were encountered: