DEV Community

loading...

Generating Swagger Example Dynamic Request Payload with Swashbuckle

Joni 【ジョニー】
Full-Stack Web Dev. Early adopter. C#/.NET technology lover.
Originally published at Medium on ・2 min read

This post originally appeared on Medium


New to Swashbuckle? See this official “Get started with Swashbuckle and ASP.NET Core.”

We can generate Swagger example requests with Swashbuckle, see this post.

Let’s say we have the following endpoint:

Ignore the weird implementation, it just a sample. Focus on the method signature.

And here is the Swagger UI page:

Note that this post is using Swashbuckle.AspNetCore version 3.0.

Now suppose we have no choice but to make it accepting dynamic payload — perhaps due to requirement changes from the client. So we change the PostThorRequest request to Jobject request like so:

Now when we run it…

Uh oh…See that, the Example Value is gone!

How to fix this?

Changing the first parameter of SwaggerRequestExample type to Jobject seems to fix it.

and…

Voila! It’s back!

Note that this might be just one way of the possible solutions that work. While it does work, personally, I don’t recommend this kind of dynamic request approach for public facing APIs.

Discussion (0)