NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

InvalidCastException when loading schema with a $ref to SecurityScheme

Open karol-gro opened this issue 2 years ago • 2 comments

I get System.InvalidCastException when trying load schema with $ref to a security scheme. I reproduced this on both NSwag.msbuild and NSwag.CodeGeneration.CSharp (13.16.0) package, yaml and json. Below are example reproduction steps.

This is a minimized version of an API specs produced by AWS tool (API Gateway if I'm correct). Even if it can be loaded by NSwag after some fixing, it'd be good to be able to use their specs out-of-the-box, without parsing.

Parsing and generating C# code works well in https://editor.swagger.io/ so I assume it's correct

Reproduction steps:

  1. Create a file OpenApi scheme:
{
  "openapi": "3.0.1",
  "info": {
    "title": "test",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://example.com/"
    }
  ],
  "paths": {
    "/some/path": {
      "get": {
        "parameters": [
          {
            "$ref": "#/components/securitySchemes/xss-request-authorization"
          }
        ],
        "responses": {
          "200": {
            "description": "Stuff successfully created and success status is returned.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Response"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "xss-request-authorization": {
        "type": "apiKey",
        "description": "Cognito Access token",
        "name": "Authorization",
        "in": "header"
      }
    },
    "schemas": {
      "Response": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "example": "Created Successfully"
          }
        }
      }
    }
  }
}
  1. Execute following csx script:
#r "nuget: NSwag.CodeGeneration.CSharp, 13.16.0"

using NSwag;

_ = await OpenApiDocument.FromFileAsync(@"C:\Path\To\File.json");

Expcted result

script should run without an error

Actual result

following exception:

System.InvalidCastException: System.InvalidCastException: Unable to cast object of type 'NSwag.OpenApiSecurityScheme' to type 'NJsonSchema.References.IJsonReference'.
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReferenceWithoutDereferencing(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReferenceWithoutDereferencing(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReferenceWithoutDereferencing(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReferenceWithoutDereferencing(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(object, List<string>, System.Type, Newtonsoft.Json.Serialization.IContractResolver, HashSet<object>)
  + NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(object, string, System.Type, Newtonsoft.Json.Serialization.IContractResolver)

karol-gro avatar May 26 '22 11:05 karol-gro

Hi,

I have the exact same issue. Hopefully this will be fixed soon.

Ay1000 avatar Jun 21 '22 08:06 Ay1000

Hello,

Currently I'm receiving the same exception when I try to generate a client. For me the issue occurs when I reference a general component within #/components/responses. Turning it into a #/components/schema component seems to work around the issue, but is not ideal.

decevy avatar Jun 21 '22 08:06 decevy