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.

How to create an example of request object?

See original GitHub issue

Do we support example values for request objects?

		public async Task<ActionResult<IEnumerable<DummyDto>>> GetConfigurations(
			[FromQuery] DummyQueryDto query) =>
			Ok(await configurationRepository.GetQueryable()
				.ProjectTo<DummyDto>(m_mapper.ConfigurationProvider)
				.ApplyFilter(query.ToFilter())
				.ApplySorting(query.ToSorter())
				.ApplyPagination(query.Offset, query.Count).ToListAsync());
	}

How do I supply example value for this DummyQueryDto object into Swagger UI?

I use OpenApi, SwaggerUi3, AddOpenApiDocument - a really basic setup.

Is there a support for Swashbuckle.AspNetCore.Filters ?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
davidkeavenycommented, Aug 31, 2020

I’ve created a custom IOperationProcessor that can create example responses based on attributes on the controller actions e.g.

[ProducesResponseType(StatusCodes.Status200OK, typeof(Country[]))
[ProducesResponseTypeExample(typeof(CountryExampleGenerator))]
public async Task<IActionResult> GetCountries([FromQuery]Country query, CancellationToken cancellationToken) {
  return await Execute(query, cancellationToken);
}

The first attribute is the ASP.NET Core-provided attribute, which gives the overall shape of the response; the second response is a custom attribute, where the CountryExampleGenerator mentioned exposes an IEnumerable method that returns sample data:

public abstract class ExampleResponseGenerator
{
    private readonly List<object> _examples = new List<object>();

    protected void Register(params object[] exampleData)
    {
        _examples.AddRange(exampleData);
    }

    public IEnumerable Generate()
    {
        foreach (var example in _examples)
        {
            yield return example;
        }
    }
}

public class CountriesExampleProvider : ExampleResponseGenerator
{
    public CountriesExampleProvider()
    {
        Register(new Country
        {
            Name = "Australia",
            CurrencySymbol = 36,
            CurrencyText = "AUD",
            DiallingCode = "61",
            Id = 13,
            IsoCode2 = "AU"
        }, new Country
        {
            Name = "New Zealand",
            CurrencySymbol = 36,
            CurrencyText = "NZD",
            DiallingCode = "64",
            Id = 149,
            IsoCode2 = "NZ"
        }
        );
    }
}

The custom IOperationProcessor then resolves those attributes when generating the OpenApi document (and generates a standard set of responses for e.g. 400 Bad Request, 404 Not Found. However, at the moment, I don’t know exactly where to place the final rendered result. On the older Swagger spec, it was on the example property of the response, but with OpenApi, it’s in the schema definition (https://swagger.io/docs/specification/2-0/adding-examples/). Any suggestions as to how I can close that particular circle? I’m happy to post the code for my processor if I can get that working.

2reactions
RicoSutercommented, Oct 8, 2019

I think it would make sense to add an attribute to specify this out of the box. However what example object/schema would we use? I think we should only support OpenAPI schemas directly (not ReDoc custom formats).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request Object | Branch CMS Documentation
The request object allows you to submit a POST or GET request to an URL. Essentially it provides a way to make REST...
Read more >
Request - Web APIs - MDN Web Docs
Chrome Edge Request Full support. Chrome42. more. Toggle history Full s... Request() constructor Full support. Chrome40. footnote. Toggle history Full s... init.priority parameter. Experimental Full support....
Read more >
Request: Request() constructor - Web APIs - MDN Web Docs
The Request() constructor creates a new Request object.
Read more >
ASP Request Object
The Request object is used to get information from a visitor. QueryString Collection Examples. Send query information when a user clicks on a...
Read more >
Node.js - Request Object
Request Object Properties ; 1. req.app. This property holds a reference to the instance of the express application that is using the middleware....
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