You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just getting started with this excellent package, so please forgive me if I'm doing something stupid.
I have an API, and want to create a response type that will indicate if the request was successful (in which case we return the data requested), if the specified object wasn't found, or if there was a server error.
In this case, I get the exception shown in the subject line. I guess this is because both the data and the generic type of Error are string, so it doesn't know which to use, but could be way out here.
I guess I could get around this by declaring the type by inheriting from OneOfBase<Error<string>, NotFound, Success<T>>, but that makes the code more verbose for the few cases when I would be returning string data.
Anyone able to advise? Thanks
Update: I just tried changing the endpoint to return a DateTime instead of a string...
OneOf<T0,...> or OneOfBase<T0,...> have properties which are implemented like
publicT0AsT0=>
_index ==0?_value0:thrownewInvalidOperationException($"Cannot return as T0 as result is T{_index}");
After you return your object from the lambda in MapGet, ASP.NET core will serialize your response object and then send that over HTTP back to the client.
For the serialization, usually System.Text.Json is used, which goes through all the public properties of a class and serializes them recursively. Once it hits the AsT0 property, an exception is thrown and the serialization fails.
To prevent this, I'd suggest you use Results<T0, ...> from the Microsoft.AspNetCore.Http.HttpResults namespace (Source). This has the benefit that the proper status codes for each of the response types are used and that your OpenApi-spec can be generated automatically.
Just getting started with this excellent package, so please forgive me if I'm doing something stupid.
I have an API, and want to create a response type that will indicate if the request was successful (in which case we return the data requested), if the specified object wasn't found, or if there was a server error.
I created the following...
This mostly works fine, but throws an exception if the data to be returned is a plain string, as in this rather dumb test case...
In this case, I get the exception shown in the subject line. I guess this is because both the data and the generic type of
Error
arestring
, so it doesn't know which to use, but could be way out here.I guess I could get around this by declaring the type by inheriting from
OneOfBase<Error<string>, NotFound, Success<T>>
, but that makes the code more verbose for the few cases when I would be returning string data.Anyone able to advise? Thanks
Update: I just tried changing the endpoint to return a
DateTime
instead of a string......and got the same exception, so it looks like my guess above was wrong.
The text was updated successfully, but these errors were encountered: