Swagger json validation unused model warnings

Viewed 328

I am having an issue with OpenApi json data validation. Based on the OpenApi validation (or editor.swagger.io) my JSON file has unused models. I am supposed to get rid of the warnings and almost all of them comes from System.Reflection Namespace.

Example:

: System.Reflection
- Unused model: PropertyInfo
- Unused model: MethodImplAttributes
- Unused model: FieldAttributes
- Unused model: EventAttributes
- Unused model: MethodAttributes
- Unused model: FieldInfo
- Unused model: PropertyAttributes 

: All the rest

- Unused model: Type    --system
- Unused model: SecurityRuleSet --system.security

The json is generated when I run the app (.NET Core 3.1 with swagger package)

Install-Package Swashbuckle.AspNetCore -Version 5.6.3
Startup.cs

// Clears servers list and adds default server entry with relative url respecting
app.UseSwagger(c=>c.ForceSingleServerWithRelativeAddress());
 
app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
 
                c.RoutePrefix = string.Empty;
 
                c.EnableDeepLinking();
            });

How can I get rid of those warnings if the System.Reflection package is used somewhere else in the project (or any other namespace like System.Security, etc). I can't just customize the MS package to get rid of unused models, it is how it is.

Are there any Startup options that I missed during the Swagger setup and is it causing the issues?

The warnings (unused models) don't give me any stack trace, root or anything simply because the unused models does not exist in the project?

The exmaple method that uses system.reflection somewhere in the project:

public async Task<IEnumerable<IssueSimple>> GetSimpleIssues(int id)
        {
            string method = MethodBase.GetCurrentMethod().Name; //that one using System.Reflection
            return null; // not to expose business logic
        }

Open Api command used to validate (both json and open api file must be in the same folder)

java -jar openapi-generator-cli-5.1.1.jar validate -i gittesting.json

Part of the json file:

"PropertyInfo": {
       "type": "object",
       "properties": {
         "name": {
           "type": "string",
           "nullable": true,
           "readOnly": true
         },
         "declaringType": {
           "$ref": "#/components/schemas/Type"
         },
         "reflectedType": {
           "$ref": "#/components/schemas/Type"
         },
         "customAttributes": {
           "type": "array",
           "items": {
             "$ref": "#/components/schemas/CustomAttributeData"
           },
           "nullable": true,
           "readOnly": true
         },
         "isCollectible": {
           "type": "boolean",
           "readOnly": true
         },
         "metadataToken": {
           "type": "integer",
           "format": "int32",
           "readOnly": true
         },
         "memberType": {
           "$ref": "#/components/schemas/MemberTypes"
         },
         "propertyType": {
           "$ref": "#/components/schemas/Type"
         },
         "attributes": {
           "$ref": "#/components/schemas/PropertyAttributes"
         },
         "isSpecialName": {
           "type": "boolean",
           "readOnly": true
         },
         "canRead": {
           "type": "boolean",
           "readOnly": true
         },
        "canWrite": {
           "type": "boolean",
           "readOnly": true
         }
       },
       "additionalProperties": false

////////////////////////////////////////////////////
////////////////////////////////////////////////////
"PropertyAttributes": {
       "enum": [
         "None",
         "SpecialName",
         "RTSpecialName",
         "HasDefault",
         "Reserved2",
         "Reserved3",
         "Reserved4",
         "ReservedMask"
       ],
       "type": "string",
       "format": "int32"
     },

General question is why is this added when this model is unused? How can I get rid of the unused models when those are added globally by MS packages?

1 Answers

It was caused by internal controller attributes and the error return type (it was set incorrectly). Nothing to do with open api/swagger itself.

Related