2017-12-01 18 views
0

Swpbuckle on Aspnet.Coreは通常、メソッドシグネチャから必要なパラメータを読み込みます。手動でパラメータを追加しますか?

[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および生成された仕様で入手できます。

残念ながら、私はいくつかの非常に大きなファイルをアップロードする必要があり、私は簡単にこのscenario-を記述することができますが、どのように私は、このメソッドというSwashbuckleを説得できる方法威張っ-Editorを使用

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素敵なアイデア!カスタム属性を使用するとoperationIdでその条件を取り除きます。そのコードを共有して他の人が利益を得ることができる場合 – HelderSepu

+0

完了:詳細については私の投稿を参照してください –