2016-01-25 12 views
7

मैं अपने एएसपी.NET वेबएपीआई कार्यों को कैसे एनोटेट कर सकता हूं ताकि swagger मेटाडाटा में सामग्री-प्रकार शामिल हो जो मेरे संसाधनों का समर्थन करते हैं?स्वाशबकल स्वैगर - सामग्री प्रकारों को एनोटेट कैसे करें?

विशेष रूप से, मैं प्रलेखन दिखाने के लिए कि मेरी संसाधनों में से एक application/json और application/xml लौट सकते हैं 'मूल' लेकिन यह भी अब एक नया प्रारूप, application/vnd.blah+json या +xml रिटर्न चाहते हैं।

+0

शशबकल 5 को आपके लिए यह ध्यान रखना चाहिए यदि आप वेब एपीआई कॉन्फ़िगरेशन के दौरान MediaTypeFormatter पंजीकृत करते हैं। –

+0

धन्यवाद। यह चालाक लगता है, लेकिन मैं इसे प्रति कार्य/मार्ग चाहता हूं। –

+1

मुझे लगता है कि आपको इसे अपने वेबकॉन्फिग में एक फॉर्मेटर के रूप में जोड़ने की आवश्यकता है - यह प्रति क्रिया के आधार पर वैश्विक नहीं है। आप क्या कर सकते थे अपने स्वयं के ऑपरेशन बना सकते थे फ़िल्टर करें और इसे केवल उन परिचालनों पर लागू करें जो नए प्रारूप – VisualBean

उत्तर

7

आपको यह करने की क्या ज़रूरत है; स्वैगर कल्पना: आपको लगता है कि आपरेशन

"produces": [ 
      "application/json", 
      "text/json" 
      ], 

यह एक OperationFilter

छद्म कोड भेजे साथ किया जा सकता के लिए प्रतिक्रिया-प्रकारों की सूची के प्रति आपकी प्रतिक्रिया प्रकार जोड़ने की जरूरत है !!!

public class CustomResponseType : IOperationFilter 
{   
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    {    
      if (operation.operationId == "myCustomName") 
      { 
       operation.produces.Add("application/vnd.blah+json"); 
      }    
    }  
} 

OperationId [SwaggerOperation("myCustomName")] एनोटेशन के माध्यम से स्थापित किया जा सकता।

फिर swaggerConfig.cs

में operationsFilter लागू
c.OperationFilter<CustomResponseType>(); 

नोट: operation.operationId == "myCustomName" के बजाय आप एक विशेष मार्ग या मूल रूप से कुछ और के लिए यह कर सकता है। ApiDescription संदर्भ के बारे में जानकारी के बहुत सारे देता है।

26

@ VisualBean के जवाब

विस्तार एक नियंत्रक के एपीआई विधि पर आप की तरह एक गुण सेट करने के लिए नीचे दिए गए कोड का उपयोग कर सकता:

[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)] 
public HttpResponseMessage GetAuthorityForm(string id) 
{ 
.... 

नोट: 'विशेष = सच' अन्य सभी प्रकार की सामग्री को हटा देगा, अन्यथा नई विशेषता का उपयोग करके स्वैगर यूआई ड्रॉप डाउन में एक नया प्रतिक्रिया सामग्री प्रकार जोड़ देगा। यह आपके कंट्रोलर या एपीआई को सिर्फ दस्तावेज को संशोधित नहीं करेगा।

SwaggerConfig.cs

GlobalConfiguration.Configuration 
      .EnableSwagger(c => 
// Set filter to apply Custom Content Types to operations 
// 
c.OperationFilter<ResponseContentTypeOperationFilter>(); 

SwaggerReponseContentTypeAttribute.cs

/// <summary> 
/// SwaggerResponseContentTypeAttribute 
/// </summary> 
[AttributeUsage(AttributeTargets.Method)] 
public sealed class SwaggerResponseContentTypeAttribute : Attribute 
{ 
    /// <summary> 
    /// SwaggerResponseContentTypeAttribute 
    /// </summary> 
    /// <param name="responseType"></param> 
    public SwaggerResponseContentTypeAttribute(string responseType) 
    { 
     ResponseType = responseType; 
    } 
    /// <summary> 
    /// Response Content Type 
    /// </summary> 
    public string ResponseType { get; private set; } 

    /// <summary> 
    /// Remove all other Response Content Types 
    /// </summary> 
    public bool Exclusive { get; set; } 
} 

ResponseContentTypeOperationFilter.cs

public class ResponseContentTypeOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>().FirstOrDefault(); 

     if (requestAttributes != null) 
     { 
      if (requestAttributes.Exclusive) 
       operation.produces.Clear(); 

      operation.produces.Add(requestAttributes.ResponseType); 
     } 
    } 
} 
5

@ ओज़बॉब का जवाब मानता है कि आप केवल एक विधि में एक ही विशेषता जोड़ना चाहते हैं। आप जोड़ना चाहते हैं और एक ही विधि आप निम्नलिखित का उपयोग कर सकते के लिए एक से अधिक सामग्री प्रकार के दस्तावेज हैं:

SwaggerReponseContentTypeAttribute.cs

/// <summary> 
/// SwaggerResponseContentTypeAttribute 
/// </summary> 
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] 
public class SwaggerResponseContentTypeAttribute : Attribute 
{ 
    /// <summary> 
    /// SwaggerResponseContentTypeAttribute 
    /// </summary> 
    /// <param name="responseType"></param> 
    public SwaggerResponseContentTypeAttribute(string responseType) 
    { 
     ResponseType = responseType; 
    } 
    /// <summary> 
    /// Response Content Type 
    /// </summary> 
    public string ResponseType { get; private set; } 

    /// <summary> 
    /// Remove all other Response Content Types 
    /// </summary> 
    public bool Exclusive { get; set; } 
} 

ResponseContentTypeOperationFilter।सीएस

public class ResponseContentTypeOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>(); 

     foreach (var requestAttribute in requestAttributes) 
     { 
      if (requestAttribute.Exclusive) 
      { 
       operation.produces.Clear(); 
      } 
      operation.produces.Add(requestAttribute.ResponseType); 
     } 
    } 
} 

ध्यान दें कि आप एक ही विधि पर एक से अधिक गुण हैं और आप मौजूदा सामग्री प्रकार को बदलने के लिए चाहते हैं, तो आपको पहले विशेषता केवल पर Exclusive = true स्थापित करना चाहिए। अन्यथा आपको दस्तावेज़ीकरण में सभी विशेषताओं को नहीं मिलेगा।

+1

को वापस लेते हैं Exclusive = true को पहली विशेषता के रूप में रखने के लिए डेवलपर के लिए स्पष्ट नहीं है। मुझे लगता है कि गुणों के क्रम को आउटपुट को प्रभावित नहीं करना चाहिए। फिल्टर में विशेषताओं की सूची को सॉर्ट करना सीमा को हटा देना चाहिए: requestAttributes.OrderByDescending (a => a.Exclusive) –

संबंधित मुद्दे