2016-08-25 29 views
5

Utknąłem próbując zmusić Swashbuckle 5 do generowania kompletnych stron pomocy dla ApiController z żądaniem Post, używając parametrów wieloczęściowych/danych formularza. Strona pomocy dla działania pojawia się w przeglądarce, ale nie zawiera informacji o parametrach przekazywanych w formularzu. Stworzyłem filtr operacji i włączyłem go w SwaggerConfig, strona internetowa zawierająca parametry URI, typ zwrotu i inne informacje pochodzące z komentarzy XML wyświetlanych na stronach pomocy przeglądarki; jednak nie ma nic określonego w filtrze operacji na temat parametrów, a strona pomocy nie zawiera żadnych informacji o parametrach.Swashbuckle 5 i wieloczęściowe/pomocnicze dane formularzy

Pewnie czegoś brakuje. Czy są jakieś sugestie co do tego, co mogłem przegapić?

Operacja Kod filtra:

public class AddFormDataUploadParamTypes : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)   { 
     if (operation.operationId == "Documents_Upload") 
     { 
      operation.consumes.Add("multipart/form-data"); 
      operation.parameters = new[] 
      { 
       new Parameter 
       { 
        name = "anotherid", 
        @in = "formData", 
        description = "Optional identifier associated with the document.", 
        required = false, 
        type = "string", 
        format = "uuid" 

       }, 
       new Parameter 
       { 
        name = "documentid", 
        @in = "formData", 
        description = "The document identifier of the slot reserved for the document.", 
        required = false, 
        type = "string", 
        format = "uuid" 
       }, 
       new Parameter 
       { 
        name = "documenttype", 
        @in = "formData", 
        description = "Specifies the kind of document being uploaded. This is not a file name extension.", 
        required = true, 
        type = "string" 
       }, 
       new Parameter 
       { 
        name = "emailfrom", 
        @in = "formData", 
        description = "A optional email origination address used in association with the document if it is emailed to a receiver.", 
        required = false, 
        type = "string" 
       }, 
       new Parameter 
       { 
        name = "emailsubject", 
        @in = "formData", 
        description = "An optional email subject line used in association with the document if it is emailed to a receiver.", 
        required = false, 
        type = "string" 
       }, 
       new Parameter 
       { 
        name = "file", 
        @in = "formData", 
        description = "File to upload.", 
        required = true, 
        type = "file" 
       } 
      }; 
     } 
    } 
} 
+0

Czy można dodać kod metody kontrolera do pytania? – venerik

+0

Mam nadzieję, że podłączyłeś filtr operacji w konfiguracji przechwytującej. –

Odpowiedz

1

Przypuszczam, że zorientowali się, jaki był twój problem. Udało mi się wykorzystać opublikowany kod, aby uzyskać idealnie wyglądający interfejs "uagger ui" wraz z kontrolkami wprowadzania danych file [BROWSE...].

Tylko nieznacznie zmodyfikowałem twój kod, więc zostanie zastosowany, gdy wykryje mój preferowany atrybut ValidateMimeMultipartContentFilterstolen from Damien Bond. Tak więc, moja lekko zmodyfikowana wersja klasy wygląda następująco:

public class AddFormDataUploadParamTypes<T> : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var actFilters = apiDescription.ActionDescriptor.GetFilterPipeline(); 
     var supportsDesiredFilter = actFilters.Select(f => f.Instance).OfType<T>().Any(); 

     if (supportsDesiredFilter) 
     { 
      operation.consumes.Add("multipart/form-data"); 
      operation.parameters = new[] 
      { 
      //other parameters omitted for brevity 
      new Parameter 
      { 
       name = "file", 
       @in = "formData", 
       description = "File to upload.", 
       required = true, 
       type = "file" 
      } 
     }; 
     } 
    } 
} 

Oto mój Swagger UI: enter image description here

FWIW:

Moi NuGets

<package id="Swashbuckle" version="5.5.3" targetFramework="net461" /> 
<package id="Swashbuckle.Core" version="5.5.3" targetFramework="net461" /> 

Swagger Przykład konfiguracji

public class SwaggerConfig 
{ 
    public static void Register() 
    { 
     var thisAssembly = typeof(SwaggerConfig).Assembly; 

     GlobalConfiguration.Configuration 
      .EnableSwagger(c => 
       { 

        c.Schemes(new[] { "https" }); 

        // Use "SingleApiVersion" to describe a single version API. Swagger 2.0 includes an "Info" object to 
        // hold additional metadata for an API. Version and title are required but you can also provide 
        // additional fields by chaining methods off SingleApiVersion. 
        // 
        c.SingleApiVersion("v1", "MyCorp.WebApi.Tsl"); 


        c.OperationFilter<MyCorp.Swashbuckle.AddFormDataUploadParamTypes<MyCorp.Attr.ValidateMimeMultipartContentFilter>>(); 

       }) 
      .EnableSwaggerUi(c => 
       { 

        // If your API supports ApiKey, you can override the default values. 
        // "apiKeyIn" can either be "query" or "header"             
        // 
        //c.EnableApiKeySupport("apiKey", "header"); 
       }); 
    } 


}