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

swashbuckle openapi 3 write example and description for the dynamically generated model classes

$
0
0

My model properties definition is coming from a json file so using reflection to write the classes to be shown under schema on resulting swagger page.

foreach (var model in Models)            {                if (!ModelTypes.ContainsKey(model.Key))                {                    anyNonCompiledModel = true;                    BuildModelCodeClass(modelComponentBuilder, model.Value);//Build model classes                }            }            BuildModelCodeEnd(modelComponentBuilder);            if (anyNonCompiledModel)            {                CSharpCompiler compiler = new CSharpCompiler();                compiler.AddReference(typeof(object));                compiler.AddReference(typeof(ResourceFactory));                compiler.AddReference(typeof(System.Runtime.Serialization.DataContractResolver));                compiler.AddReference(typeof(System.Runtime.Serialization.DataContractAttribute));                var types = compiler.Compiler(modelComponentBuilder.ToString()); //write model classes                foreach (var type in types)                {                    ModelTypes.Add(type.Name, type);                }            }public void BuildModelCodeClass(StringBuilder modelComponentBuilder, MetadataModelEntity model)        {            modelComponentBuilder.AppendLine($"public class {model.Name} {{");            foreach (var p in model.Data.Properties)            {                if (p.Obsoleted) continue;                if (p.Type.Type == "array")                {                    modelComponentBuilder.AppendLine($" public {p.Type.ArrayType.ObjectName}[] {p.Name} {{get;set;}}");                }                else                {                    //primitive types                    modelComponentBuilder.AppendLine($" public {p.Type.ObjectName} {p.Name} {{get;set;}}");                }            }            modelComponentBuilder.AppendLine(@"}");        }

If i provide the description and example like following (in BuildModelCodeClass, inside the loop) then the example and description displays for me.

if (!string.IsNullOrWhiteSpace((string)p.Example))                {                    modelComponentBuilder.AppendLine($" ///<example>{p.Example}</example>");                }                if (!string.IsNullOrWhiteSpace((string)p.Description))                {                    modelComponentBuilder.AppendLine($" ///<description>{p.Description}</description>");                }

However, i dont want to do above.

  1. I want to write my models via the open api and not via the C# Compiler, is it possible?

  2. I want to show example and description via schema (may be under paths some where). How can i do this? Context has my models info available that i can interact with here.

    public class SwaggerDocumentFilter : IDocumentFilter{SwaggerDocument _swaggerDocument;public SwaggerDocumentFilter(object apiConfigure){_swaggerDocument = ((ApiGatewayConfiguration)apiConfigure).SwaggerDocument;}

        public void Apply(OpenApiDocument document, DocumentFilterContext context)    {        if (document.Info.Extensions == null || !document.Info.Extensions.ContainsKey(SwaggerEndpoint.ExtensionDocName)) return;        var openIdString = document.Info.Extensions[SwaggerEndpoint.ExtensionDocName] as OpenApiString;        if (openIdString == null) return;        var docName = openIdString.Value;        SwaggerEndpoint endpoint = _swaggerDocument.SwaggerEndpoints.SingleOrDefault(x => x.Name == docName);        if (endpoint == null) return;        //Add server objects        document.Servers = endpoint.ServerObjects;        //Add Tags objects        document.Tags = endpoint.Tags;        //Set swagger paths objects        var pathsObjects = _swaggerDocument.GetPathsObject(docName, context);        if (pathsObjects.IsValid())        {            pathsObjects.ToList().ForEach(                item => document.Paths.Add(item.Key, item.Value)                );        }        //Add Schema components        //Add Example/Examples    }}

Viewing all articles
Browse latest Browse all 38

Trending Articles



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