Skip to content

Commit e5a771f

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Added support for arbitrary metadata properties on API descriptions
1 parent db37e29 commit e5a771f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/VersionedApiDescription.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Web.Http.Description
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Linq.Expressions;
67
using System.Web.Http.Description;
@@ -49,6 +50,13 @@ public class VersionedApiDescription : ApiDescription
4950
}
5051
}
5152

53+
/// <summary>
54+
/// Gets arbitrary metadata properties associated with the API description.
55+
/// </summary>
56+
/// <value>A <see cref="IDictionary{TKey, TValue}">collection</see> of arbitrary metadata properties
57+
/// associated with the <see cref="VersionedApiDescription">API description</see>.</value>
58+
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
59+
5260
static Action<ApiDescription, ResponseDescription> CreateSetResponseDescriptionMutator()
5361
{
5462
var api = Expression.Parameter( typeof( ApiDescription ), "api" );

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/System.Web.Http/Description/ApiDescriptionExtensions.cs

+30
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,35 @@ public static string GetUniqueID( this ApiDescription apiDescription )
4747

4848
return apiDescription.ID;
4949
}
50+
51+
/// <summary>
52+
/// Gets a property of the specified type from the API description.
53+
/// </summary>
54+
/// <typeparam name="T">The <see cref="Type">type</see> of property to retrieve.</typeparam>
55+
/// <param name="apiDescription">The <see cref="VersionedApiDescription">API description</see> to get the property from.</param>
56+
/// <returns>The value of the property, if present; otherwise, the default value of <typeparamref name="T"/>.</returns>
57+
public static T GetProperty<T>( this VersionedApiDescription apiDescription )
58+
{
59+
Arg.NotNull( apiDescription, nameof( apiDescription ) );
60+
61+
if ( apiDescription.Properties.TryGetValue( typeof( T ), out object value ) )
62+
{
63+
return (T) value;
64+
}
65+
66+
return default( T );
67+
}
68+
69+
/// <summary>
70+
/// Sets a property of the specified type on the API description.
71+
/// </summary>
72+
/// <typeparam name="T">The <see cref="Type">type</see> of property to set.</typeparam>
73+
/// <param name="apiDescription">The <see cref="VersionedApiDescription">API description</see> to set the property on.</param>
74+
/// <param name="value">The value to add or update.</param>
75+
public static void SetProperty<T>( this VersionedApiDescription apiDescription, T value )
76+
{
77+
Arg.NotNull( apiDescription, nameof( apiDescription ) );
78+
apiDescription.Properties[typeof( T )] = value;
79+
}
5080
}
5181
}

0 commit comments

Comments
 (0)