Polymorphic response support
See original GitHub issueIs there a way to work with polymorphism on response models with FastEndpoints?
In my usecase I have a response that has a “week” schema, so, my endpoint should return an array of itens that could be of different models.
Here a simplified version of the models I’m using:
public class BaseSale
{
public string Type { get; set; }
public string TransactionKey { get; set; }
}
public class CardSale : BaseSale
{
public string AuthorizationCode { get; set; }
}
public class OnlineSale : BaseSale
{
public string PaymentKey { get; set; }
}
I’ve created an endpoint that returns an enumerable of BaseSale
public class GetBaseSalesList : Endpoint<SalesListRequestQuery, IEnumerable<BaseSale>>
{
....
public override async Task HandleAsync(SalesListRequestQuery request, CancellationToken token)
{
var salesListFilter = _mapper!.Map<SalesListFilter>(request);
var sales = _repository!.GetBySpecification(
new BaseSaleSpecification(salesListFilter)
);
await SendAsync(sales);
}
}
My problem is that the children classes specific infos are ignored and I only get the BaseSale info at the responses.
[
{
"type": "pix",
"transactionKey": "E2c32640d-a170-46d4-877d-9e349086d771"
},
{
"type": "card",
"transactionKey": "46111757709832"
},
{
"type": "card",
"transactionKey": "46311780642853"
}
]
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to best handle "polymorphic" JSON response
How to best handle "polymorphic" JSON response ... I'm working with a 3rd party REST API which has a number of data types...
Read more >Handling Polymorphic response using KotlinX
Polymorphic JSON response is a bit different from your usual JSON response where the type of the object will change at runtime. This...
Read more >java - Polymorphism in a REST API - different data type in ...
One way to achieve this is to use Jackson's polymorphic type support. Model your data as: public interface Observation<T> { T ...
Read more >Support polymorphic response models #40
Search code, repositories, users, issues, pull requests... · Provide feedback · Saved searches · Support polymorphic response models #40 · Support ...
Read more >How to use and document polymorphism in API
On this page, I'll do my best to explain and illustrate how polymorphism can be used in API development to help achieve these...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you can use newtonsoft and achieve this with .net 6 mvc but not with minimal api. if you really must, you can however utilize newtonsoft with FE like this: https://fast-endpoints.com/docs/configuration-settings#custom-response-dto-serialization
but it’s not recommended to use newtonsoft due to poor performance.
you will have to wait for .net 7 as STJ does not support this in .net 6 https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-6-0