0

I'm trying to dynamically build a MinimalApi in ASP.Net Core application and I'm struggling with generating a requestBody example.

I tried to solved it like this:

.WithOpenApi(x =>
{
    x.RequestBody = new()
    {
        Content = new Dictionary<string, OpenApiMediaType>()
        {
            { "application/json", new OpenApiMediaType() { Example = new OpenApiString(JsonSerializer.Serialize(new TestClass())) } }
        }
    };
}

I'm getting an error 😱 Could not render Dt, see the console. when I open a method in Swagger-UI.

In the console, I have a message TypeError: Cannot read properties of undefined (reading 'toJS').

What is wrong with my approach and how can I set an example of requestBody in OpenAPI?

1 Answer 1

1

I've figured that out. I was missing a Scheme parameter in my OpenApiMediaType. The complete solution looks like this:

.WithOpenApi(x =>
{
    var scheme = new OpenApiSchema()
    {
        Type = typeof(TestClass).ToString(),
        Example = new OpenApiString(JsonSerializer.Serialize(new TestClass()))
    };
    x.RequestBody = new()
    {
        Content = new Dictionary<string, OpenApiMediaType>()
        {
            ["application/json"] = new()
            {
                 Schema = scheme
            }
        }
    };
}
Sign up to request clarification or add additional context in comments.

3 Comments

what are we using WithOpenApi for? I ask because it doesn't actually augment the OpenAPI document generation, and so doesn't impact the swagger UI if you have one.
I call WithOpenApi per endpoint on RouteHandlerBuilder to specify open api documentation for the endpoint. It definitely impacts the swagger UI. For example endpointRouteBuilder.MapRoute(...).WithOpenApi(x => x.Summary = "Some summary") adds summary to the open api documentation as well as to the swagger.
Yikes. This makes me want to go back to generating code from a hand-written API spec. Thanks for the info though.

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.