선택적 (및 null 입력 가능) 매개 변수가있는 asp.net core 2.0
API 컨트롤러 동작이 있습니다. Swashbuckle.AspNetCore 1.0.0
이 매개 변수가 메서드 서명에 있으면 Swagger 설명서가 생성되지 않지만 서명에서 제거하면 성공합니다. https://github.com/Microsoft/aspnet-api-versioning/wiki/Swashbuckle-Integration선택적 매개 변수는 Swashbuckle.AspNetCore에서 null 예외를 발생시킵니다.
컨트롤러 액션 :
내가 ASPNET-API-버전으로 Swashbuckle 통합을 사용하고 ... 선택적 매개 변수는 오류의 원인하지만 난 그 이유를 말할 수 없다 나타납니다
: 여기[HttpGet("{id}")]
public IActionResult GetPerson(int id, [FromQuery] bool? includeContactInfo = null)
{
var includeInfo = (includeContactInfo.HasValue && includeContactInfo.Value == true);
var person = _repo.GetPerson(id, includeInfo);
if (person == null)
{
return NotFound();
}
if (includeInfo)
{
// using AutoMapper to map between entity and dto
var personFullDto = Mapper.Map<PersonFullDto>(person);
return Ok(personFullDto);
}
var personBasicDto = Mapper.Map<PersonBasicDto>(person);
return Ok(personBasicDto);
}
는 ConfigureServices 내 startup.cs
에서 구성 방법이 있습니다 나는 자신감 URL로 이동 https://github.com/Microsoft/aspnet-api-versioning/wiki/Swashbuckle-Integration#aspnet-core
public class SwaggerDefaultValues : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
foreach (var parameter in operation.Parameters.OfType<NonBodyParameter>())
{
var description = context.ApiDescription
.ParameterDescriptions
.First(p => p.Name == parameter.Name);
if (parameter.Description == null)
{
parameter.Description = description.ModelMetadata.Description;
}
if (parameter.Default == null)
{
parameter.Default = description.RouteInfo.DefaultValue;
}
parameter.Required |= !description.RouteInfo.IsOptional;
}
}
}
에서 나는 또한 SwaggerDefaultValues.cs 클래스를 사용하고
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore().AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
services.AddMvc();
services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
});
services.AddSwaggerGen(options =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}
options.OperationFilter<SwaggerDefaultValues>();
options.IncludeXmlComments(XmlCommentsFilePath);
});
var connectString = Startup.Configuration["connectionStrings:ContactsAppDb"];
services.AddDbContext<ContactsDbContext>(o => o.UseSqlServer(connectString));
services.AddScoped<IContactsRepository, ContactsRepository>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider)
{
if (env.IsEnvironment("PROD"))
{
app.UseExceptionHandler();
}
else
{
app.UseDeveloperExceptionPage();
}
app.UseStatusCodePages();
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Entities.Person, Models.PersonBasicDto>();
cfg.CreateMap<Entities.Person, Models.PersonFullDto>();
cfg.CreateMap<Entities.ContactInfo, Models.ContactInfoDto>();
});
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
// build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
}
는 다음 코드 줄을합니다 (SwaggerDefaultValues.cs 클래스에서) 실패
나는includeContactInfo
옵션/쿼리 매개 변수에 대한
description
개체를 검사 할 때
parameter.Default = description.RouteInfo.DefaultValue;
는 description.RouteInfo
은 null입니다.
오류 메시지 :
Object reference not set to an instance of an object.
스택 추적 :
at ContactsApp.Services.SwaggerDefaultValues.Apply(Operation operation, OperationFilterContext context) in C:\Users\me\Documents\Visual Studio 2017\Projects\ContactsApp\ContactsApp\Services\SwaggerDefaultValues.cs:line 37
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext()