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
Hi, I recently used OneOf to make some code quality improvements and encoutered an issue when serializing data.
I have an endpoint (.NET 6 API) in which I can return multiple types for the same response code. I know it is not a good thing, but it can't be changed due to business reasons. My goal here was to send this type of response to the front :
[GenerateOneOf]
public partial class MyClass : OneOfBase<T0, T1, T2> {}
// Sent like this :
public IActionResult MyMethod() {
var myClassInstance = this.MyOtherMethod();
return this.Ok(myClassInstance)
}
// The goal was to be able to get the right type in my front :
new MyService().MyMethod().then(result => this.data = result.isT0 ? result.asT0 : null)
The thing is, it can't be serialized as-is because of the exception thrown if the index does not correspond with the type currently used (cf. OneOfBase.AsT0).
As a workaround, I had to create a record in which I could copy the structure of MyClass :
I tried multiple solutions using other libraries to serialize MyClass but then it would not be the same structure, which is problematic as I generate my TS client using NSwag, so I would get undefined value for the object properties when handling the promise response. Also, custom converters would not be triggered so I wasn't able to handling the serializing as I want.
Was I missing something for serializing as I wanted, or is this something not supported at the moment ?
I think it would be useful if the objects inheriting from OneOfBase could be serialized with AsTx as null if x does not correspond to the index of the type used.
The text was updated successfully, but these errors were encountered:
Hi, I recently used OneOf to make some code quality improvements and encoutered an issue when serializing data.
I have an endpoint (.NET 6 API) in which I can return multiple types for the same response code. I know it is not a good thing, but it can't be changed due to business reasons. My goal here was to send this type of response to the front :
The thing is, it can't be serialized as-is because of the exception thrown if the index does not correspond with the type currently used (cf. OneOfBase.AsT0).
As a workaround, I had to create a record in which I could copy the structure of MyClass :
I tried multiple solutions using other libraries to serialize MyClass but then it would not be the same structure, which is problematic as I generate my TS client using NSwag, so I would get undefined value for the object properties when handling the promise response. Also, custom converters would not be triggered so I wasn't able to handling the serializing as I want.
Was I missing something for serializing as I wanted, or is this something not supported at the moment ?
I think it would be useful if the objects inheriting from OneOfBase could be serialized with AsTx as null if x does not correspond to the index of the type used.
The text was updated successfully, but these errors were encountered: