abpframework / abp

Open Source Web Application Framework for ASP.NET Core. Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET and the ASP.NET Core platforms. Provides the fundamental infrastructure, production-ready startup templates, application modules, UI themes, tooling, guides and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.31k stars 3.32k forks source link

Illegal Characters in swagger.json - Unable to use in swagger generator. #19666

Closed sjdean closed 2 weeks ago

sjdean commented 2 weeks ago

Is there an existing issue for this?

Description

I have noticed a problem reported elsewhere but so far unchanged where the swagger.json is corrupt. This is especially true for things like PagedResultDto objects and you'll note within the name of the object there are "`" tick marks.

Please see below a sample of the created JSON:


      "Volo.Abp.Application.Dtos.PagedResultDto`1[[Application.Shared.ExtendedLookupDto`1[[System.Guid, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=XXX]], Application.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Application.Shared.ExtendedLookupDto`1[[System.Guid, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=XXX]]"
            },
            "nullable": true
          },
          "totalCount": {
            "type": "integer",
            "format": "int64"
          }
        },
        "additionalProperties": false
      },

In the _HttpApiHostModule.cs file, we see the following line:

options.CustomSchemaIds(type => type.FullName);

Being a previous user of AspNetZero, there is a function called CustomDefaultSchemaIdSelector() within SwaggerExtensions.cs which seems to do what we need here. I have incorporated the code within _HttpApiHostModule.cs by changing the above line to:

                options.CustomSchemaIds(type =>
                {
                    string SchemaIdSelector(Type mType)
                    {
                        if (!mType.IsConstructedGenericType)
                        {
                            return mType.FullName;
                        }
                        else
                        {
                            var prefix = mType.GetGenericArguments()
                                .Select(SchemaIdSelector)
                                .Aggregate((previous, current) => previous + current);
                            return mType.FullName.Split('`').First() + "Of" + prefix;
                        }
                    }
                    return SchemaIdSelector(type);
                });

This seems to work well for me, and I now have a valid JSON file I can pass into my swagger generator.

Reproduction Steps

Expected behavior

Correct swagger.json file should be created with no illegal characters.

Actual behavior

Current setup produces incorrect JSON.

Regression?

No response

Known Workarounds

No response

Version

8.1.1

User Interface

Angular

Database Provider

EF Core (Default)

Tiered or separate authentication server

None (Default)

Operation System

macOS

Other information

No response

maliming commented 2 weeks ago

hi @sjdean

Thank you for your suggestion. I have added an extension method.

See https://github.com/abpframework/abp/pull/19669