2017-12-01 17 views
0

Aspnet.Core의 Swashbuckle은 일반적으로 메소드 서명에서 필요한 매개 변수를 읽습니다.매개 변수를 수동으로 추가 하시겠습니까?

[HttpGet] 
[Route("/api/datasets/{id}")] 
[SwaggerOperation("DatasetsIdGet")] 
[SwaggerResponse(200, type: typeof(DataSet))] 
public IActionResult DatasetsIdGet([FromRoute]string id) 
{ 
    string exampleJson = null; 

    var example = exampleJson != null ? JsonConvert.DeserializeObject<DataSet>(exampleJson) : default(DataSet); 
    return new ObjectResult(example); 
} 

ID는 경로에서 가져온 것으로 Swagger-UI 및 생성 된 사양을 통해 제공됩니다.

불행히도, 난 아주 큰 파일을 업로드해야하고 어떻게이 방법이 있음을 Swashbuckle을 설득 할 수 나는 쉽게 scenario-을 설명 할 수 있지만, 자신감-편집기를 사용하는 방법

public async Task<IActionResult> Upload() 
{ 
// drain fields manually. see https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads 
// assume that there is the field bigupload. 
} 

에 대한 formbinding하지 않도록하고 싶습니다 필수 입력란으로 bigupload가 있습니까?

편집

다음

가 swashbuckle의 GitHub의에서 스레드에서 내 솔루션을 기반으로

public class ImportFileParamType : IOperationFilter 
{ 

    /// <summary> 
    /// Adds formData Attributes to the Swagger Documentation. 
    /// Must be registered in Startup.cs 
    /// </summary> 
    /// <param name="operation"></param> 
    /// <param name="context"></param> 
    public void Apply(Operation operation, OperationFilterContext context) 
    { 
     Console.WriteLine("ok"); 

     var attributes = context.ApiDescription.ActionAttributes() 
     .OfType<SwaggerFormParameter>(); 

     foreach (var attribute in attributes) 
     { 
      if (operation.Parameters == null) 
      { 
       operation.Parameters = new List<IParameter>(); 
      } 

      if (operation.Consumes.Count == 0) 
      { 
       operation.Consumes.Add("multipart/form-data"); 
      } 

      var collectionFormat = attribute.CollectionFormat == CollectionFormat.None ? "" : attribute.CollectionFormat.ToString(); 

      operation.Parameters.Add(new NonBodyParameter() 
      { 
       Name = attribute.Name, 
       Description = attribute.Description, 
       In = "formData", 
       Required = attribute.IsRequired, 
       Type = attribute.Type, 
       CollectionFormat = collectionFormat 
      }); 
     } 

     Console.WriteLine("ok"); 
    } 
} 

public enum CollectionFormat 
{ 
    csv, 
    ssv, 
    tsv, 
    pipes, 
    None 
} 

/// <summary> 
/// Adds pure FormData Objects to a Swagger Description. Useful if you cannot do Modelbinding because the uploaded Data is too large. 
/// Set the type to "file" if you want files. Otherwise all supported primitve swagger-types should be ok. 
/// </summary> 
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] 
public sealed class SwaggerFormParameter : Attribute 
{ 
    public string Name { get; private set; } 
    public string Type { get; private set; } 
    public string Description { get; set; } 
    public bool IsRequired { get; set; } 

    public CollectionFormat CollectionFormat { get; set; } 

    public SwaggerFormParameter(string name, string type) 
    { 
     Name = name; 
     Type = type; 
    } 
} 

답변

1

당신은 여기 IOperationFilter

public class AddRequiredParameters : IOperationFilter 
    { 
     public void Apply(Operation operation, SchemaRegistry s, ApiDescription a) 
     { 
      if (operation.operationId == "ControllerName_Upload") 
      { 
       if (operation.parameters == null) 
        operation.parameters = new List<Parameter>(); 
       operation.parameters.Add(
        new Parameter 
        { 
         name = "bigupload", 
         @in = "body", 
         @default = "123", 
         type = "string", 
         description = "bla bla", 
         required = true 
        } 
       );      
      } 
     } 
    } 

를 사용하여 전체 예라고 할 수 있습니다 SwaggerConfig.cs#L505

+0

대단히 감사합니다! 이것은 훌륭한 출발점이었습니다! 메서드에 적용 할 수있는 사용자 지정 특성을 추가했으며 작업 필터에서 선택했습니다. –

+0

@ChristianSauer 좋은 아이디어! 사용자 정의 attrib를 사용하면 operationId에서 해당 조건을 제거 할 수 있습니다. 다른 사용자가 그 코드를 공유 할 수 있다면 – HelderSepu

+0

완료 : 자세한 내용은 내 게시물을 참조하십시오. –