From fca1668c8c3c0ddde662ed434606ac73dc167f4d Mon Sep 17 00:00:00 2001 From: xwltz <61488281@qq.com> Date: Thu, 17 Apr 2025 17:43:15 +0800 Subject: [PATCH] =?UTF-8?q?swagger=E6=B7=BB=E5=8A=A0=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/OpenApiProperty.cs | 3 +++ .../Plugins/SwaggerPlugin.cs | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/TouchSocket.WebApi.Swagger/Common/OpenApiProperty.cs b/src/TouchSocket.WebApi.Swagger/Common/OpenApiProperty.cs index b094fce6f..e6cebe48e 100644 --- a/src/TouchSocket.WebApi.Swagger/Common/OpenApiProperty.cs +++ b/src/TouchSocket.WebApi.Swagger/Common/OpenApiProperty.cs @@ -30,4 +30,7 @@ internal class OpenApiProperty [JsonProperty("items")] public OpenApiProperty Items { get; set; } + + [JsonProperty("description")] + public string Description { get; set; } } \ No newline at end of file diff --git a/src/TouchSocket.WebApi.Swagger/Plugins/SwaggerPlugin.cs b/src/TouchSocket.WebApi.Swagger/Plugins/SwaggerPlugin.cs index 7fbffe692..f10ac5e91 100644 --- a/src/TouchSocket.WebApi.Swagger/Plugins/SwaggerPlugin.cs +++ b/src/TouchSocket.WebApi.Swagger/Plugins/SwaggerPlugin.cs @@ -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(); foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { - properties.Add(propertyInfo.Name, this.CreateProperty(propertyInfo.PropertyType)); + var description = propertyInfo.GetCustomAttribute()?.Description; + properties.Add(propertyInfo.Name, this.CreateProperty(propertyInfo.PropertyType, description)); } schema.Properties = properties.Count == 0 ? default : properties; components.Schemas.TryAdd(this.GetSchemaName(type), schema);