1
votes

Helloo everybody. Tell me please. Can I generate a Sample Request in Swagger depending on the model, so as not to write it manually. To see which fields the model has.
Is it even possible?
Because now I have to manually write a description of the request for each api.
I would like to automate this process. I am using Swashbuckle and ASP.NET Core

1
Personally I think swagger is description enough. Swagger also adds models which helps the reader. The reader can also do request directly on swagger (and then see the actual request). So I don't think it is necessary to describe each call manually. Besides that you can use github.com/RicoSuter/NSwag to generate a client - Van
Thanks for the answer, but in my case it is still necessary. Therefore, I decided to find out whether it is possible to simplify this process) - Igor

1 Answers

0
votes

There is a place for code examples, you should add /// <example>123</example> on the model instead of the remarks.

Is this close to what you need:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Company#/Company/Company_Post

The code for that is here:
https://github.com/heldersepu/Swagger-Net-Test/blob/c9b554fde26367418c84fcd3682d308ae1b40d11/Swagger_Test/Models/Company.cs


You can also make dynamic changes with an IDocumentFilter like this:

    private class AddExampleDocumentFilter : IDocumentFilter
    {
        private List<Guid> Guids
        {
            get
            {
                return new List<Guid>
                {
                    Guid.Empty, Guid.Empty
                };
            }
        }

        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
        {
            if (swaggerDoc.paths.ContainsKey("/api/Dictionary"))
            {
                var del = swaggerDoc.paths["/api/Dictionary"].delete;
                if (del != null)
                {
                    del.parameters[0].schema.example = Guids;
                }
            }
        }
    }

Here is the result of that:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Dictionary#/Dictionary/Dictionary_DeleteEcho

And once the user executes it with the sample data the UI shows the curl request: