1

I have the following C# ASP.Net Core Web API controller method for creating an "entity" using a POST:

[HttpPost("example")]
[SwaggerResponse(200,"Ok")]
public async Task<IActionResult> Create([FromForm]MyModel create)
{
    return Ok();
}

The model is defined as this:

public class MyModel
{
    public string PropA { get; set; }
    public string PropB { get; set; }
    public List<OtherProp> Other { get; set; }
}

public class OtherProp
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

When this is shown in swagger, you can see the "Other" property array looks like this:

enter image description here

Instead of the broken down model. How do I get swagger to breakdown this model?

1
  • Did you found your solution? Commented Jun 13, 2023 at 5:06

1 Answer 1

1

Your are missing the Swagger Decorator Attributes, follow this below and replace the attributes with your specific Types / MyModel


Since you didn't put up your actual code, to see how it works, use the default samples for e.g. you can Install my Swashbuckle.Examples NuGet package. Decorate your methods with the new SwaggerResponseExample attributes below and you will see it work just fine!

// These attributes will help with your nested objects
[SwaggerResponse(HttpStatusCode.OK, Type=typeof(IEnumerable<Country>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(CountryExamples))]
[SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(IEnumerable<ErrorResource>))]
public async Task<HttpResponseMessage> Get(string lang)

Also ensure you have it configured like so

configuration
    .EnableSwagger(c =>
    {
        c.OperationFilter<ExamplesOperationFilter>();
    })
    .EnableSwaggerUi();
Sign up to request clarification or add additional context in comments.

1 Comment

These attributes decorate the swagger response - my problem is with the request model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.