Skip to content

Commit

Permalink
Merge pull request #33 from jsheely/feat-description
Browse files Browse the repository at this point in the history
SwaggerDescriptionAttribute
  • Loading branch information
heldersepu committed Dec 25, 2017
2 parents a6d9f2e + bfe1017 commit 63f0899
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions Swagger.Net/Swagger.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Swagger\Annotations\ApplySwaggerSchemaFilterAttributes.cs" />
<Compile Include="Swagger\Annotations\ApplySwaggerOperationFilterAttributes.cs" />
<Compile Include="Swagger\Annotations\ApplySwaggerOperationApiKey.cs" />
<Compile Include="Swagger\Annotations\SwaggerDescriptionAttribute.cs" />
<Compile Include="Swagger\Annotations\SwaggerOperationAttribute.cs" />
<Compile Include="Swagger\Annotations\SwaggerSchemaFilterAttribute.cs" />
<Compile Include="Swagger\Annotations\SwaggerOperationFilterAttribute.cs" />
Expand Down
20 changes: 20 additions & 0 deletions Swagger.Net/Swagger/Annotations/SwaggerDescriptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Swagger.Net.Swagger.Annotations
{
public class SwaggerDescriptionAttribute: Attribute
{
public SwaggerDescriptionAttribute(string description = null, string summary = null)
{
Description = description;
Summary = summary;
}

public string Description { get; set; }
public string Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Http.Description;
using Swagger.Net.Swagger.Annotations;

namespace Swagger.Net.Swagger.Extensions
{
Expand All @@ -12,9 +13,9 @@ public static RegularExpressionAttribute GetRegularExpressionAttribute(this ApiP
return paramDesc.ParameterDescriptor.GetCustomAttributes<RegularExpressionAttribute>().FirstOrDefault();
}

public static DescriptionAttribute GetDescriptionAttribute(this ApiParameterDescription paramDesc)
public static SwaggerDescriptionAttribute GetDescriptionAttribute(this ApiParameterDescription paramDesc)
{
return paramDesc.ParameterDescriptor.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault();
return paramDesc.ParameterDescriptor.GetCustomAttributes<SwaggerDescriptionAttribute>().FirstOrDefault();
}
}
}
13 changes: 13 additions & 0 deletions Swagger.Net/Swagger/Extensions/SchemaExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json.Serialization;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Swagger.Net.Swagger.Annotations;

namespace Swagger.Net
{
Expand Down Expand Up @@ -54,6 +56,17 @@ public static Schema WithValidationProperties(this Schema schema, JsonProperty j
return schema;
}

public static Schema WithDescriptionProperty(this Schema schema, JsonProperty jsonProperty)
{
var propInfo = jsonProperty.PropertyInfo();
if (propInfo == null)
return schema;

var attrib = propInfo.GetCustomAttributes(false).OfType<SwaggerDescriptionAttribute>().FirstOrDefault();
schema.description = attrib?.Description;
return schema;
}

public static void PopulateFrom(this PartialSchema partialSchema, Schema schema)
{
if (schema == null) return;
Expand Down
12 changes: 7 additions & 5 deletions Swagger.Net/Swagger/SchemaRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SchemaRegistry(JsonSerializerSettings jsonSerializerSettings, SwaggerGene

public Schema GetOrRegister(Type type, string typeName = null)
{
var schema = CreateInlineSchema(type, typeName );
var schema = CreateInlineSchema(type, typeName);

// Iterate outstanding work items (i.e. referenced types) and generate the corresponding definition
while (_workItems.Any(entry => entry.Value.Schema == null && !entry.Value.InProgress))
Expand Down Expand Up @@ -205,21 +205,23 @@ public Schema CreateArraySchema(JsonArrayContract arrayContract, bool isWrapped
type = "array",
items = CreateInlineSchema(itemType)
};
if( itemType.Namespace != "System" && itemType.Namespace != "Enum" )
if (itemType.Namespace != "System" && itemType.Namespace != "Enum")
{
s.xml = new Xml { name = typeName ?? itemType.Name, wrapped = isWrapped };
}
return s;
}

public Schema CreateObjectSchema(JsonObjectContract jsonContract, bool addXmlName = false )
public Schema CreateObjectSchema(JsonObjectContract jsonContract, bool addXmlName = false)
{
var properties = jsonContract.Properties
.Where(p => !p.Ignored)
.Where(p => !(_options.IgnoreObsoleteProperties && p.IsObsolete()))
.ToDictionary(
prop => prop.PropertyName,
prop => CreateInlineSchema(prop.PropertyType).WithValidationProperties(prop)
prop => CreateInlineSchema(prop.PropertyType)
.WithValidationProperties(prop)
.WithDescriptionProperty(prop)
);

var required = jsonContract.Properties.Where(prop => prop.IsRequired())
Expand All @@ -232,7 +234,7 @@ public Schema CreateObjectSchema(JsonObjectContract jsonContract, bool addXmlNam
properties = properties,
type = "object"
};
if( addXmlName )
if (addXmlName)
{
s.xml = new Xml { name = jsonContract.UnderlyingType.Name };
}
Expand Down
13 changes: 11 additions & 2 deletions Swagger.Net/Swagger/SwaggerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Linq;
using System.Net.Http;
using System.Web.Http.Description;
using System.Reflection;
using Swagger.Net.Swagger.Annotations;

namespace Swagger.Net
{
Expand Down Expand Up @@ -52,7 +54,8 @@ public SwaggerDocument GetSwagger(string rootUrl, string apiVersion)

var controllers = apiDescriptions
.GroupBy(x => x.ActionDescriptor.ControllerDescriptor)
.Select(x => new {
.Select(x => new
{
name = x.Key.ControllerName,
context = new ModelFilterContext(x.Key.ControllerType, null, null)
});
Expand Down Expand Up @@ -151,6 +154,9 @@ private Operation CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaR
})
.ToList();

var description = apiDesc.ActionDescriptor.GetCustomAttributes<SwaggerDescriptionAttribute>()
.FirstOrDefault();

var responses = new Dictionary<string, Response>();
var responseType = apiDesc.ResponseType();
if (responseType == null || responseType == typeof(void))
Expand All @@ -162,6 +168,8 @@ private Operation CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaR
{
tags = new[] { _options.GroupingKeySelector(apiDesc) },
operationId = this.GetUniqueFriendlyId(apiDesc, operationNames),
description = description?.Description,
summary = description?.Summary,
produces = apiDesc.Produces().ToList(),
consumes = apiDesc.Consumes().ToList(),
parameters = parameters.Any() ? parameters : null, // parameters can be null but not empty
Expand Down Expand Up @@ -208,10 +216,11 @@ private string GetParameterLocation(ApiDescription apiDesc, ApiParameterDescript

private Parameter CreateParameter(string location, ApiParameterDescription paramDesc, SchemaRegistry schemaRegistry)
{

var parameter = new Parameter
{
@in = location,
name = paramDesc.Name
name = paramDesc.Name,
};

if (paramDesc.ParameterDescriptor == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swagger.Net.Swagger.Annotations;

namespace Swagger.Net.Dummy.Controllers
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public HttpResponseMessage Patch()

[HttpPut]
[SwaggerOperation("UpdateMessage", Tags = new[] { "messages" }, Schemes = new[] { "ws" })]
public void Put([Description("param description")]int id, Message message)
public void Put([SwaggerDescription("param description")]int id, Message message)
{
throw new NotImplementedException();
}
Expand All @@ -56,7 +57,7 @@ public class Message2: Message { }
[SwaggerSchemaFilter(typeof(AddMessageDefault))]
public class Message
{
[Description("param model description")]
[SwaggerDescription("param model description")]
public string Title { get; set; }
public string Content { get; set; }
}
Expand Down

0 comments on commit 63f0899

Please sign in to comment.