Closed AnushreeSoman21 closed 5 months ago
Hey there,
What is the version of NSwag.AspNetCore and Swashbuckle.AspNetCore packages?
Also, please provide me with the code of Startup.cs and the controller.
In the meantime, also try annotating examples with [ExampleAnnotation(Name = "Brno")]
and [ExampleAnnotation(Name = "Prague")]
attribute respectively.
Hi @vaclavnovotny,
Thanks for your response.
I am using NSwag.AspNetCore- 14.0.7 and Nswag.Examples - 1.0.14 and using .NET 8
//Startup.cs
services.AddOpenApiDocument((config, provider) =>
{ config.AddExamples(provider);})
// Example classes
[ExampleAnnotation(Name = "Brno")]
public class BrnoExample : IExampleProvider<City>
{
public City GetExample()
{
return new City {
Id = 5,
Name = "Brno",
People = new List<Person> {
new Person {Id = 1, FirstName = "Henry", LastName = "Cavill"},
new Person {Id = 2, FirstName = "John", LastName = "Doe"}
}
};
}
}
[ExampleAnnotation(Name = "Prague")]
public class PragueExample : IExampleProvider<City>
{
public City GetExample() {
return new City {
Id = 420,
Name = "Prague",
People = new List<Person> {
new Person {Id = 1, FirstName = "Henry", LastName = "Cavill"},
new Person {Id = 2, FirstName = "John", LastName = "Doe"}
}
};
}
}
//Controller method
[HttpGet("{id}/city")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
[EndpointSpecificExample(typeof(BrnoExample), typeof(PragueExample))] // <-----
public IActionResult GetPersonCity([FromRoute] int id) {...}
Thanks in advance.
Hi @vaclavnovotny ,
Any update on this.
Thanks
Hey @AnushreeSoman21, I tried to reproduce your problem and the main issue is with the controller.
//Controller method
[HttpGet("{id}/city")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))]
[EndpointSpecificExample(typeof(BrnoExample), typeof(PragueExample))] // <-----
public IActionResult GetPersonCity([FromRoute] int id) {...}
You are essentially asking to show "City" example for type int
which does not make sense. The GetPersonCity is returning int
and is accepting int
, none of which is the type of both examples - City
.
I created very simple repro demo with your code, check it out and let me know if that helped to resolve the issue. Issue24.zip
Cheers
Hi @vaclavnovotny thank you for your response. I will once check and confirm.
Closed due to inactivity
I want to set multiple examples for request body. I have added multiple examples like mentioned in Nswag.Examples documentation,
It shows like below image in my request body section,
but when I am selecting dropdown options it is not changing the examples based on options in request body.
Can you please help me with this.
Thanks in advance!