RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

NSwag doesnt show endpoints parameters data types #4866

Closed VladisS-Vostok2000Work closed 2 months ago

VladisS-Vostok2000Work commented 2 months ago

Hello there! For some reason Swagger UI does not show endpoint parameters data types: image

I found that Swagger UI needs parameter schema for that. I cant find it in json document:

"parameters": [
          {
            "type": "integer",  <-- correct data type
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Page number starits with 1",
            "x-position": 1,
            "format": "int32",
            "maximum": 2147483647.0,
            "minimum": 1.0,
            "nullable": false
          }
    ...
]
...

I can add schema manually:

        "parameters": [
          {
            "type": "integer",
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID карточки документа",
            "schema": {  <-- added with processor
              "type": "integer",
              "format": "int32"
            },

with processor:

public bool Process(OperationProcessorContext context)
    {
         MethodInfo methodInfo = context.MethodInfo;
         foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
         {
             if (!parameterInfo.IsDefined(typeof(EndpointParameterAttribute)))
             {
                 continue;
             }

             if (Type.GetTypeCode(parameterInfo.ParameterType) == TypeCode.Object)
             {
                 continue;
             }

             OpenApiParameter parameter = context.Parameters[parameterInfo];
             parameter.Schema = new()
             {
                 Type = Type.GetTypeCode(parameterInfo.ParameterType) switch
                 {
                     TypeCode.Empty => JsonObjectType.Object,
                     TypeCode.DBNull => JsonObjectType.Object,
                     TypeCode.Boolean => JsonObjectType.Boolean,
                     TypeCode.Char => JsonObjectType.String,
                     TypeCode.SByte => JsonObjectType.Number,
                     TypeCode.Byte => JsonObjectType.Number,
                     TypeCode.Int16 => JsonObjectType.Integer,
                     TypeCode.UInt16 => JsonObjectType.Integer,
                     TypeCode.Int32 => JsonObjectType.Integer,
                     TypeCode.UInt32 => JsonObjectType.Integer,
                     TypeCode.Int64 => JsonObjectType.Integer,
                     TypeCode.UInt64 => JsonObjectType.Integer,
                     TypeCode.Single => JsonObjectType.Number,
                     TypeCode.Double => JsonObjectType.Number,
                     TypeCode.Decimal => JsonObjectType.Number,
                     TypeCode.DateTime => JsonObjectType.String,
                     TypeCode.String => JsonObjectType.String
                 },
                 Format = Type.GetTypeCode(parameterInfo.ParameterType) switch
                 {
                     TypeCode.Empty => "",
                     TypeCode.DBNull => "dbnull",
                     TypeCode.Boolean => "boolean",
                     TypeCode.Char => "char",
                     TypeCode.SByte => "sbyte",
                     TypeCode.Byte => "byte",
                     TypeCode.Int16 => "int16",
                     TypeCode.UInt16 => "uint16",
                     TypeCode.Int32 => "int32",
                     TypeCode.UInt32 => "uint32",
                     TypeCode.Int64 => "int64",
                     TypeCode.UInt64 => "uint64",
                     TypeCode.Single => "float",
                     TypeCode.Double => "double",
                     TypeCode.Decimal => "decimal",
                     TypeCode.DateTime => "DateTime",
                     TypeCode.String => "string"
                 }
             };

         }

         return true;

But this processor can't process complex parameters ([FromQuery] MyRequest, where MyRequest is class).

Is there any configuration to enable daya types displaying?

Thanks!

VladisS-Vostok2000Work commented 2 months ago
using NSwag;
using NSwag.Generation.Processors;
using NSwag.Generation.Processors.Contexts;

namespace SI.NonTrade.WebApi;

innal class DocCardsOperationProcessor : IOperationProcessor
{
    public bool Process(OperationProcessorContext context)
    {
         IList<OpenApiParameter> apiParameters = context.OperationDescription.Operation.Parameters;
         foreach (OpenApiParameter apiParameter in apiParameters)
         {
             apiParameter.Schema = new()
             {
                 Type = apiParameter.Type,
                 Format = apiParameter.Format,
                 IsNullableRaw = apiParameter.IsNullableRaw
             };
         }

         return true;
    }

}

Is my solution.