Swashbuckle.aspnetcore: 5.0.0-rc5 with AddSwaggerGenNewtonsoftSupport() fails to generate swagger for HttpResponseMessage

Created on 16 Dec 2019  路  13Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Versions used:

<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc5" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.0.0-rc5" />

Configuration:

public void ConfigureServices(IServiceCollection services) {
    services.AddControllers();

    services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    });

    services.AddSwaggerGenNewtonsoftSupport();
}

Example controller:

[Route("v1")]
[AllowAnonymous]
public class OperationIdOverrideController : Controller
{
    [SwaggerOperation(OperationId = "CustomOperationId1")]
    [Route("clients/{clientId}"), HttpGet]
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
}

Exception thrown:

System.NotSupportedException: Cannot generate schema for type System.Version
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorBase.GenerateSchema(Type type, SchemaRepository schemaRepository)   at Swashbuckle.AspNetCore.Newtonsoft.NewtonsoftObjectHandler.GeneratePropertySchema(JsonProperty jsonProperty, IEnumerable`1 customAttributes, Required required, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.Newtonsoft.NewtonsoftObjectHandler.CreateDefinitionSchema(Type type, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorBase.<>c__DisplayClass4_1.<GenerateSchema>b__0()
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository.GetOrAdd(Type type, String schemaId, Func`1 factoryMethod)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorBase.CreateReferenceSchema(Type type, SchemaRepository schemaRepository, Func`1 factoryMethod)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorBase.GenerateSchema(Type type, SchemaRepository schemaRepository)   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateResponseMediaType(ModelMetadata modelMetadata, SchemaRepository schemaRespository)

Problem goes away if AddSwaggerGenNewtonsoftSupport() is not used. Unfortunately I rely on multiple Newtonsoft.Json to properly document the generated swagger.

Most helpful comment

I've got same exception but for System.Exception class. Also goes away when AddSwaggerGenNewtonsoftSupport is removed.

System.NotSupportedException
  Message=Cannot generate schema for type System.Exception
  Source=Swashbuckle.AspNetCore.SwaggerGen

All 13 comments

Which version of ASP.NET Core are you using?

3.1

Which serializer are you using - System.Text.Json (default with 3.1) or Newtonsoft (requires separate package and explicit opt-in)

Newtonsoft.

Installed Swashbuckle.AspNetCore.Newtonsoft and it up using services.AddSwaggerGenNewtonsoftSupport(); (as described in the latest release notes)

I had the exact same issue except for the exception,

swashbucklexception.txt

All my actions look like this

[HttpPost] [SwaggerOperation(OperationId = "Create")] [ProducesResponseType(typeof(SomeType), StatusCodes.Status200OK)] [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task<ActionResult<SomeType>> Create([FromBody] Dto dto) { ... }

I've got same exception but for System.Exception class. Also goes away when AddSwaggerGenNewtonsoftSupport is removed.

System.NotSupportedException
  Message=Cannot generate schema for type System.Exception
  Source=Swashbuckle.AspNetCore.SwaggerGen

I have the same issue, except for mine it says

Cannot generate schema for type System.IntPtr

        [HttpGet("/{fileId:long}")]
        [ProducesResponseType(typeof(FileStream), (int)OK)]
        [ProducesResponseType((int)HttpStatusCode.BadRequest)]
        [ProducesResponseType((int)HttpStatusCode.NotFound)]
        public Task<IActionResult> GetFile(long fileId, CancellationToken cancellationToken) =>

@domaindrivendev We have similar issues if we switch from 5.0.0-rc4 (everything works fine) to 5.0.0 release. We are using the Swashbuckle.AspNetCore.Newtonsoft package/AddSwaggerGenNewtonsoftSupport as we use Newtonsoft for all other serialization. Same issue as @pechkarus we get "Unhandled exception. System.NotSupportedException: Cannot generate schema for type System.Exception" but if we comment out AddSwaggerGenNewtonsoftSupport it compiles and generates. Also it seems from rc4 to release version it is adding in core .net types into the schema things like MethodAttributes, MethodImplAttributes, CallingConventions, RuntimeMethodHandle, MemberTypes, ModuleHandle whereas it did not used to add those to my swagger before. And that also causes issues with nswag etc....

Adding services.AddSwaggerGenNewtonsoftSupport(); is also causing the same problems for me in the 5.0 release. I'm also seeing the same additional types in the schema (MethodAttributes, MethodImplAttributes, CallingConventions, RuntimeMethodHandle, MemberTypes, ModuleHandle).

Same issue here with "System.Data.DataTable" - and I'm pretty sure it worked some time and just stopped? ... Is it possible to define a type for the generator? A JavaScript Object would be just fine for me in this case.

Adding services.AddSwaggerGenNewtonsoftSupport(); is also causing the same problems for me in the 5.0 release. I'm also seeing the same additional types in the schema (MethodAttributes, MethodImplAttributes, CallingConventions, RuntimeMethodHandle, MemberTypes, ModuleHandle).

Same for me. Adding AddSwaggerGenNewtonsoftSupport causes System.NotSupportedException for Exception type.

Adding services.AddSwaggerGenNewtonsoftSupport(); is also causing the same problems for me in the 5.0 release. I'm also seeing the same additional types in the schema (MethodAttributes, MethodImplAttributes, CallingConventions, RuntimeMethodHandle, MemberTypes, ModuleHandle).

Same for me. Adding AddSwaggerGenNewtonsoftSupport causes System.NotSupportedException for Exception type.

In my case, it was the same, using the NewtonsoftSupport cause the NotSupportedException for System.Exception type.

For now, I have solved that adding a custom mapping to the SwaggerGen:
options.MapType(() => new OpenApiSchema { Type = "object" });

Adding services.AddSwaggerGenNewtonsoftSupport(); is also causing the same problems for me in the 5.0 release. I'm also seeing the same additional types in the schema (MethodAttributes, MethodImplAttributes, CallingConventions, RuntimeMethodHandle, MemberTypes, ModuleHandle).

Same for me. Adding AddSwaggerGenNewtonsoftSupport causes System.NotSupportedException for Exception type.

In my case, it was the same, using the NewtonsoftSupport cause the NotSupportedException for System.Exception type.

For now, I have solved that adding a custom mapping to the SwaggerGen:
options.MapType(() => new OpenApiSchema { Type = "object" });

Nice, ty! "MapType" was what I'm looking for. Now I can at least keep my Models and do not need "dummy object types" until this gets fixed.

Was this page helpful?
0 / 5 - 0 ratings