Replies: 3 comments 14 replies
-
This type of scenario makes a lot of sense. I don't get a ton of questions about it so I don't know if people have figured out there own devices or what. A custom The blog post you referenced has a lot of the mechanics you need. Rather than using a magic string key, you can just rely on the API version that's included in the request. The condition should be something like: public override bool CanRead(InputFormatterContext context) =>
base.CanRead(context) &&
context.HttpContext.GetRequestedApiVersion() is ApiVersion apiVersion &&
apiVersion == ApiVersion.Default; public override bool CanWriteResult(OutputFormatterCanWriteContext context) =>
base.CanWriteResult(context) &&
context.HttpContext.GetRequestedApiVersion() is ApiVersion apiVersion &&
apiVersion == ApiVersion.Default; This should work for both input and output. It will be organically paired to the API version mapping you've already configured. At the point in the pipeline where a formatter comes into place, an API version should be set and available. The one and only exception is if the API is version-neutral, but then there's no real way to distinguish different APIs. That seems to not a problem you would face.
|
Beta Was this translation helpful? Give feedback.
-
@commonsensesoftware am I able to get the HttpContext or ApiVersion information in a TypeInfoResolver Modifier? I'd like to make a contract that is based on a [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class JsonIgnoreInVersionAttribute : JsonAttribute {.
public JsonIgnoreInVersionAttribute(int version) {
Version = version;
}
public int Version { get; }
} var quirkOptions = new JsonSerializerOptions() {
...
TypeInfoResolver = new DefaultJsonTypeInfoResolver {
Modifiers = {
new JsonIgnoreInVersionModifier(
// seems it can only be passed in here?
_log.CreateLogger<JsonIgnoreInVersionModifier>()
).IgnorePropertiesConverter
}
}
}; |
Beta Was this translation helpful? Give feedback.
-
I just switched to minimal api's which undid all of this work. Are minimal api's ready for this kind of serialization @commonsensesoftware? |
Beta Was this translation helpful? Give feedback.
-
I have a .net framework api with 1 version. I am converting that .net framework api to dotnet 7 and adding a second version to fix some of the goofy functionality of the existing api. But I need to recreate some of the goofy functionality so the dotnet 7 code can seamlessly replace the framework code.
In the framework api, json.net was used and camelCase property names are littered all over the response models. If a request has a specific query string value passed in, then a response delegating handler sets some items in a dictionary and those keys were left in title case while the rest of the response was camelCase. e.g.
There are other dictionaries on other routes that are formatted based on input request parameters so the formatting has to be configurable per route and per version.
I can't figure out how to configure STJ to be aware of the version so I can replicate this dictionary key behavior. I can create a JsonNamingPolicy and set it on the DictionaryKeyPolicy but the policy isn't version or route aware.
Do you have any suggestions for how versioning can influence STJ serialization?
I'm currently reading this blog about using multiple serializer settings. But this situation is so specific to one route, on a specific version, when an certain input parameter is used.
I read some microsoft advice that creating a serializer per request really impacts performance. So try to reuse them.
This stack overflow talks about converting all my routes to return a JsonResult with settings specific to the route. This could work because the route is aware of the version and can set different settings. Does this fall into the trap of not reusing the serializer?
I know this is kind of a versioning question and kind of not a versioning question but I hope we can still have a discussion about different ways to solve this.
Beta Was this translation helpful? Give feedback.
All reactions