How to use legacy error format along side new ProblemDetails format depending upon API Version? #1072
-
Hi. I am porting a legacy API from .NET 4.X to .NET 8. As part of this process, I am adding API versioning and updating to use the new standards (ProblemDetails, etc). I need to maintain input and output format fidelity to the existing API. I have already determined how to make the original API the default, when a version is not specified. I have also determined that I can provide my own InvalidModelStateResponseFactory to handle custom format of the 400 response when the model fails validation. However, when the new V2 API, I don't want to use the custom format for the V2 API calls. I would like to use the ProblemDetails response type. What is the recommended approach to use switch between the two response formats at runtime depending upon what version of the API was called? Do I just do something like this:
Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
There's certainly some complexity if you're running both scenarios in the same application, but it's possible. I haven't done this myself, but I'm pretty sure what would work for you is something like this: Extend
|
Beta Was this translation helpful? Give feedback.
-
@commonsensesoftware, thank you. Not a lot of complexity, as the existing service only has one controller with one method, but two routes configured. Easy enough. I just know there are changes coming later this year where I will need to extended the API. Since I currently porting this to .NET 8 from 4.7+ and migrating from RavenDB to Sqllite/SqlExpress and I have the time with no hard deadline, now is the time for me to get this code structured for the future changes. Including updating the code to use the new problem details format for model/request validation, exceptions. A few questions:
Can you please let me know where I can read more about how ProblemDetails is implemented in .Net 8. Thanks -marc |
Beta Was this translation helpful? Give feedback.
-
My apologues for the delayed response. When I drop the above code into my project (just copy and paste, no changes) and compile, I'm getting the following errors:
I am going to assume that this has something to do with STJ converting the object serialization code at compile time and avoid the reflection based serialization. (As an aside, these errors are why I haven't turned this on for my own projects. Never seem to have figured out why there are errors being raised.). Thanks -marc |
Beta Was this translation helpful? Give feedback.
Ah... I see the issue. This is because the
JsonSerializerContext
is defined by a source code generator. The Error Object types areprotected
to the writer itself, but areinternal
for theJsonSerializerContext
. Once you inherit from it, they aren't visible in your derived implementation. The types themselves are structs so you can't inherit from them (which might have been a sleezy trick). The only thing you really need is theJsonSerializerContext
, but since it'sinternal
and the only thing that uses it isIConfigureOptions<T>
, which is alsointernal
, there's no way to access it without resorting to Reflection. 🤮Arguably, this is a design flaw. It wasn't really clear to me how this migh…