vaclavnovotny / NSwag.Examples

NSwag processor to programmatically define strongly-typed examples for response and request parameters.
MIT License
13 stars 11 forks source link

Set multiple examples to request body #24

Closed AnushreeSoman21 closed 5 months ago

AnushreeSoman21 commented 6 months ago

I want to set multiple examples for request body. I have added multiple examples like mentioned in Nswag.Examples documentation,

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"}
            }
        };
    }
}

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"}
            }
        };
    }
}

It shows like below image in my request body section, image

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!

vaclavnovotny commented 6 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.

AnushreeSoman21 commented 6 months ago

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.

AnushreeSoman21 commented 6 months ago

Hi @vaclavnovotny ,

Any update on this.

Thanks

vaclavnovotny commented 6 months ago

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

AnushreeSoman21 commented 6 months ago

Hi @vaclavnovotny thank you for your response. I will once check and confirm.

vaclavnovotny commented 5 months ago

Closed due to inactivity