question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polymorphic response support

See original GitHub issue

Is 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:closed
  • Created 10 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dj-nitehawkcommented, Nov 7, 2022

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.

1reaction
dj-nitehawkcommented, Nov 7, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found