Skip to content
\n

Thanks.

","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

Ah... I see the issue. This is because the JsonSerializerContext is defined by a source code generator. The Error Object types are protected to the writer itself, but are internal for the JsonSerializerContext. 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 the JsonSerializerContext, but since it's internal and the only thing that uses it is IConfigureOptions<T>, which is also internal, there's no way to access it without resorting to Reflection. 🤮

\n

Arguably, this is a design flaw. It wasn't really clear to me how this might ever be extended. The correct, long-term library solution here is to simply provide a way to access or create the associated JsonSerializerContext; for example, some method or property on ErrorObjectWriter. That would certainly address your scenario. I'll queue that up for the next release. Unfortunately, that affects the public surface area and will require a minor version bump (but, that's not really a huge deal).

\n

In the meantime, the best, most flexible option is to simply fork the entire implementation of ErrorObjectWriter verbatim. It's up to you if you want to use an alternate name (e.g. ConditionalErrorObjectWriter). Your implementation can be sealed and even internal if you like. No need for virtual members. The only difference in the fork will be the condition in CanWrite (as shown above in the original override). It's a bit unfortunate, but this approach seems to be the one with least amount of other weirdness. This type is already for backcompat. I don't expect this to ever change so you shouldn't have to worry about keeping your fork up-to-date. The only way I foresee that happening would be a bug in the implementation. Possible, but unlikely. Once there is a way to access ErrorObjectWriter.JsonSerializerContext in some future release, you can opt to replace the fork with the originally proposed subclass if you wish. The net result will be same though.

\n

I don't have a better answer at the moment, but that should unblock you and get you back on your way.

","upvoteCount":0,"url":"https://github.com/dotnet/aspnet-api-versioning/discussions/1072#discussioncomment-8671227"}}}
Discussion options

You must be logged in to vote

Ah... I see the issue. This is because the JsonSerializerContext is defined by a source code generator. The Error Object types are protected to the writer itself, but are internal for the JsonSerializerContext. 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 the JsonSerializerContext, but since it's internal and the only thing that uses it is IConfigureOptions<T>, which is also internal, 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…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@commonsensesoftware
Comment options

Comment options

You must be logged in to vote
3 replies
@commonsensesoftware
Comment options

Answer selected by mhsimkin
@mhsimkin
Comment options

@commonsensesoftware
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants