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.

Is it possible to generate a client method executing a GET request with a filter parameter having a complex data type?

See original GitHub issue

Hi there,

my controller currently looks like this:

[Route("/api/[controller]")]
public class MyController
{
    ...
    [HttpGet]
    public async Task<ActionResult<...>> ListAsync([FromQuery] MyFilterModel filter)
    {
        ...
    }
}

The MyFilterModel is defined as

public class MyFilterModel 
{
    public string Name { get; set; }
    public string Description { get; set; }
}

Now when I generate a C# client from this controller via NSwag the resulting method signature looks like this

Task<...> ListAsync(string name, string description)

Is it possible to generate a method signature that uses the filter model class instead like

Task<...> ListAsync(MyFilterModel filter)

I am asking because currently calling ListAsync("foo", "bar") in multiple places has some drawbacks like you would need to revisit and change these calls when MyFilterModel is changed and its not pretty readable at the first glance. I would rather like to call it like ListAsync(new MyFilterModel { Name = "foo", Description = "bar" }).

Perhaps it is just not possible at the moment due to the still ongoing work for issue #1594 which is part of epic #945.

So lonG

Daniel

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
Shaddixcommented, Nov 14, 2021

If anyone’s interested, I implemented it using a custom liquid template. You could take the folder, and run nswag pointing to it (/templateDirectory:PATH).

It will add another overload to Client for all GET requests having at least 1 parameter. E.g. for initial method like this: public Task<ResultDto> GetSomething(string searchQuery, bool? filter1, int filter2) it will generate:

public Task<ResultDto> GetSomething(ControllerClientGetSomethingParametersDto parameters)
public class ControllerClientGetSomethingParametersDto  {
  public string searchQuery {get; set;}
  public bool filter1 {get; set;}
  public int filter2 {get; set;}
}
4reactions
luizfernando1996commented, Apr 5, 2022

Hello @RicoSuter , I want this feature that is disponible in OpenApi 3.0. So, I guess I will implement this and create a pull request for you. However, can you help me saying some words that help me in implement this feature? I don’t know nothing about nswag implementation and how I should send for you my pull request. I want this feature just for typescript client but if I implement I will should implement for all languages? In this project we have a pattern for pull requests or not?

Reference: https://github.com/RicoSuter/NSwag/issues/3301#issuecomment-779043342

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a proper way to do a complex RESTful search ...
Following REST principles, I would want to create a GET method for my API that make a search using some criteria and return...
Read more >
rest - What is the best way to design a HTTP request when ...
There's no perfect way to do this. The correct HTTP/REST way would be to use a GET and have all your parameters in...
Read more >
REST API Design: Filtering, Sorting, and Pagination
The problem is URL parameters only have a key and a value but filters are composed of three components: The property or field...
Read more >
Why an HTTP Get Request Shouldn't Have a Body
In this tutorial, we'll learn why an HTTP GET request shouldn't be sent with a body. First, we'll see why that request is...
Read more >
Parameter Binding in ASP.NET Web API - ASP.NET 4.x
The id parameter is a "simple" type, so Web API tries to get the value from the request URI. The item parameter is...
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