Quantcast
Channel: User learning... - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by learning... for swashbuckle openapi 3 write example and description for the dynamically generated model classes

$
0
0

Following helpedhttps://github.com/domaindrivendev/Swashbuckle.WebApi/issues/162

AddSchemaExamples.cs

public class AddSchemaExamples : ISchemaFilter{    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)    {        if (type == typeof(Product))        {            schema.example = new Product                {                    Id = 123,                    Type = ProductType.Book,                    Description = "Treasure Island",                    UnitPrice = 10.0M                };        }    }}

SwaggerConfig.cs

httpConfig     .EnableSwagger(c =>         {             c.SchemaFilter<AddSchemaExamples>()         });

My implementation for the Apply since model is dynamic

if (model != null)                {                    schema.Description = model.Description;                    foreach (var p in schema.Properties)                    {                        var mp = model.Data.Properties.SingleOrDefault(x => x.Name == p.Key);                        if (mp != null)                        {                            if (!string.IsNullOrWhiteSpace(mp.Description))                            {                                p.Value.Description = mp.Description;                            }                            if(!string.IsNullOrWhiteSpace(mp.Example))                            {                                p.Value.Example =                                    new Microsoft.OpenApi.Any.OpenApiString(mp.Example.ToString());                            }                        }                    }                }

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>