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

View File

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

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;
@@ -313,6 +314,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
{
var openApiParameter = this.GetParameter(parameter.ParameterInfo);
openApiParameter.In = "query";
openApiParameter.Description = parameter.ParameterDesc;
this.AddSchemaType(parameter.Type, schemaTypeList);
openApiParameters.Add(openApiParameter);
}
@@ -349,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:
@@ -379,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;
@@ -399,7 +400,7 @@ public sealed class SwaggerPlugin : PluginBase, IServerStartedPlugin, IHttpPlugi
}
openApiProperty.Format = this.GetSchemaFormat(type);
openApiProperty.Description = description;
return openApiProperty;
}
@@ -485,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);