!68 添加 Swagger 的参数描述

Merge pull request !68 from xwltz/dev
This commit is contained in:
若汝棋茗
2025-04-20 12:27:47 +00:00
committed by Gitee
4 changed files with 19 additions and 5 deletions

View File

@@ -11,6 +11,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
using System; using System;
using System.ComponentModel;
using System.Reflection; using System.Reflection;
using TouchSocket.Core; using TouchSocket.Core;
@@ -37,6 +38,11 @@ public class RpcParameter
/// </summary> /// </summary>
public ParameterInfo ParameterInfo { get; } public ParameterInfo ParameterInfo { get; }
/// <summary>
/// 参数描述
/// </summary>
public string ParameterDesc => this.ParameterInfo.GetCustomAttribute<DescriptionAttribute>()?.Description;
/// <summary> /// <summary>
/// 参数名称 /// 参数名称
/// </summary> /// </summary>

View File

@@ -21,6 +21,9 @@ internal class OpenApiParameter
[JsonProperty("in")] [JsonProperty("in")]
public string In { get; set; } public string In { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("schema")] [JsonProperty("schema")]
public OpenApiSchema Schema { get; set; } public OpenApiSchema Schema { get; set; }

View File

@@ -30,4 +30,7 @@ internal class OpenApiProperty
[JsonProperty("items")] [JsonProperty("items")]
public OpenApiProperty Items { get; set; } 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;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -313,6 +314,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
{ {
var openApiParameter = this.GetParameter(parameter.ParameterInfo); var openApiParameter = this.GetParameter(parameter.ParameterInfo);
openApiParameter.In = "query"; openApiParameter.In = "query";
openApiParameter.Description = parameter.ParameterDesc;
this.AddSchemaType(parameter.Type, schemaTypeList); this.AddSchemaType(parameter.Type, schemaTypeList);
openApiParameters.Add(openApiParameter); openApiParameters.Add(openApiParameter);
} }
@@ -349,11 +351,10 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
openApiPathValue.Responses.Add("200", openApiResponse); openApiPathValue.Responses.Add("200", openApiResponse);
} }
private OpenApiProperty CreateProperty(Type type) private OpenApiProperty CreateProperty(Type type, string description = "")
{ {
var openApiProperty = new OpenApiProperty(); var openApiProperty = new OpenApiProperty();
var dataTypes = this.ParseDataTypes(type); var dataTypes = this.ParseDataTypes(type);
switch (dataTypes) switch (dataTypes)
{ {
case OpenApiDataTypes.String: case OpenApiDataTypes.String:
@@ -379,7 +380,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
case OpenApiDataTypes.Array: case OpenApiDataTypes.Array:
{ {
openApiProperty.Type = dataTypes; 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; break;
@@ -399,7 +400,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
} }
openApiProperty.Format = this.GetSchemaFormat(type); openApiProperty.Format = this.GetSchemaFormat(type);
openApiProperty.Description = description;
return openApiProperty; return openApiProperty;
} }
@@ -485,7 +486,8 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
var properties = new Dictionary<string, OpenApiProperty>(); var properties = new Dictionary<string, OpenApiProperty>();
foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) 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; schema.Properties = properties.Count == 0 ? default : properties;
components.Schemas.TryAdd(this.GetSchemaName(type), schema); components.Schemas.TryAdd(this.GetSchemaName(type), schema);