简体   繁体   中英

.NET Framework Swashbucket Examples SwaggerRequestExample throws System.ArgumentNullException

I use Swashbucket and Swashbucket.Examples NugetPackages to provide a Swagger API in .NET Framework (v.4.7.2)

Following the documentation onhttps://github.com/mattfrear/Swashbuckle.AspNetCore.Filters I am trying to use the SwaggerRequestExample Attribute like this:

[HttpPost]
[Route("search")]
[SwaggerRequestExample(typeof(OrderRequestExample), typeof(OrderRequestExampleProvider))]
public async Task<HttpResponseMessage> SearchAsync()
{
...
}

My Startup class is configured as described in the docs:

config.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "MyApi");
                c.OperationFilter<ExamplesOperationFilter>();
                c.IncludeXmlComments(System.String.Format(@"{0}\bin\MyApi_Api.xml",
                    System.AppDomain.CurrentDomain.BaseDirectory));
                //c.OperationFilter<AddResponseHeadersFilter>();
            }).EnableSwaggerUi(c => { c.DocumentTitle("My API");});

When I use the SwaggerRequestExample Attribute, the UI displays an error:

500 : {"Message":"An error has occurred.","ExceptionMessage":"Der Wert darf nicht NULL sein.\r\nParametername: source","ExceptionType":"System.ArgumentNullException","StackTrace":" bei System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)\r\n bei Swashbuckle.Examples.ExamplesOperationFilter.SetRequestModelExamples(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)\r\n bei Swashbuckle.Examples.ExamplesOperationFilter.Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)\r\n bei Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry)\r\n bei Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n bei Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping`2 group)\r\n bei System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)\r\n bei Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n bei Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n bei System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n bei System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n bei System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n bei System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()"} https://localhost:44384/swagger/docs/v1

obviously, a "source" (IEnumerable) is missing (null). I can't find where to add it.

Example Classes:

public class OrderRequestExampleProvider : IExamplesProvider
{
    public object GetExamples()
    {
        return new OrderRequestExample()
        {
            Name = "some name"
        };
    }
}

public class OrderRequestExample
{
    public string Name { get; set; }
}

招摇参数空异常

The solution seems quite obvious but in case you don't use deserialization of the body in the signature of the method, you get this error.

After adding the request object to the method signature the exception does not occur.

public async Task<HttpResponseMessage> SearchAsync([FromBody]OrderRequestExample request) {
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM