swagger添加对象属性描述

This commit is contained in:
xwltz
2025-04-17 17:43:15 +08:00
parent 26efbbc247
commit fca1668c8c
2 changed files with 9 additions and 5 deletions

View File

@@ -30,4 +30,7 @@ internal class OpenApiProperty
[JsonProperty("items")]
public OpenApiProperty Items { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
}

View File

@@ -14,6 +14,7 @@ using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
@@ -350,11 +351,10 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
openApiPathValue.Responses.Add("200", openApiResponse);
}
private OpenApiProperty CreateProperty(Type type)
private OpenApiProperty CreateProperty(Type type, string description = "")
{
var openApiProperty = new OpenApiProperty();
var dataTypes = this.ParseDataTypes(type);
switch (dataTypes)
{
case OpenApiDataTypes.String:
@@ -380,7 +380,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
case OpenApiDataTypes.Array:
{
openApiProperty.Type = dataTypes;
openApiProperty.Items = this.CreateProperty(type.IsArray ? type.GetElementType() : type.GetGenericArguments()[0]);
openApiProperty.Items = this.CreateProperty(type.IsArray ? type.GetElementType() : type.GetGenericArguments()[0], description);
}
break;
@@ -400,7 +400,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
}
openApiProperty.Format = this.GetSchemaFormat(type);
openApiProperty.Description = description;
return openApiProperty;
}
@@ -486,7 +486,8 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
var properties = new Dictionary<string, OpenApiProperty>();
foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
properties.Add(propertyInfo.Name, this.CreateProperty(propertyInfo.PropertyType));
var description = propertyInfo.GetCustomAttribute<DescriptionAttribute>()?.Description;
properties.Add(propertyInfo.Name, this.CreateProperty(propertyInfo.PropertyType, description));
}
schema.Properties = properties.Count == 0 ? default : properties;
components.Schemas.TryAdd(this.GetSchemaName(type), schema);