Custom Attributes - AlexanderKrutov/Radish GitHub Wiki
Note: It's recommended to familiarize with key concepts before reading this article.
Why?
If you want to extend Radish functionality, for example, display some extra data in the generated documentation, Radish provides a way to do it with custom attributes.
Let's say we want to display that some REST API methods were deprecated. In the generated documentation we would like to see it in the form:
This method was deprecated since version 1.2.
— where 1.2 is a parameter passed via attribute.
To do this, we need:
- Define custom attribute to mark deprecated methods;
- Define custom template (and/or extend existing) with a tag to be replaced by the attribute's metadata (version number);
- Add rule with replacement logic;
- Mark required methods with the attribute.
Defining custom attribute
Each custom attribute should be inherited from a base class RadishAttribute. Our custom attribute will have single property to store API version number indicating when the REST API method was deprecated — SinceVersion:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DeprecatedMethodAttribute : RadishAttribute
{
public string SinceVersion { private set; get; }
public DeprecatedMethodAttribute(string sinceVersion)
{
this.SinceVersion = sinceVersion;
}
}
Defining template
Let's choose the SimpleTemplateSet as a base class for the template set. You can use use any other existing template set as a base of course.
Thus, we should extend SimpleTemplateSet like:
public class CustomTemplateSet : SimpleTemplateSet
{
// template for displaying text about deprecated methods:
public string DeprecatedMethod { get; set; }
// template set constructor:
public CustomTemplateSet() : base()
{
// define content for 'DeprecatedMethod' template:
DeprecatedMethod = "<p>This method was deprecated since version {{version}}.</p>";
// override the 'Method' template content by inserting a tag before '{{request-parameters-list}}':
Method = base.Method.Replace("{{request-parameters-list}}", "{{deprecated}}{{request-parameters-list}}");
}
}
Note that we are overriding the base Method template content by embedding a new {{deprecated}} tag before another tag {{request-parameters-list}}. New tag will be replaced by DeprecatedMethod template content if the REST API method was marked with DeprecatedMethod attribute, or will be replaced by empty text otherwise. This logic should be implemented in replacement rules.
Defining replacement rules
We need 2 replacement rules to achieve the required functionality. First one is for {{deprecated}} tag in the Method template (will be replaced to DeprecatedMethod template content), and second one is for the {{version}} tag in the nested DeprecatedMethod template.
Than in the template set constructor we write:
// In the 'CustomTemplateSet' constructor:
...
this.AddRule<CustomTemplateSet>(ts => ts.Method, "deprecated", Method_Deprecated_Handler);
this.AddRule<CustomTemplateSet>(ts => ts.DeprecatedMethod, "version", DeprecatedMethod_Version_Handler);
And finally add rule handlers to the CustomTemplateSet class:
// In the 'CustomTemplateSet' class:
// rule handler for tag '{{deprecated}}' in 'Method' template
private string Method_Deprecated_Handler(DocumentorContext context, params object[] parameters)
{
// take REST API method info from parameters
MethodInfo restMethod = parameters[0] as MethodInfo;
// get DeprecatedMethod attribute if it was defined
DeprecatedMethodAttribute attr = restMethod.GetRadishAttribute<DeprecatedMethodAttribute>();
// render nested 'DeprecatedMethod' template or return empty string if DeprecatedMethod attribute was not defined
return (attr != null ? context.RenderTemplate<CustomTemplateSet>(ts => ts.DeprecatedMethod, restMethod) : "");
}
// rule handler for tag '{{version}}' in 'DeprecatedMethod' template
private string DeprecatedMethod_Version_Handler(DocumentorContext context, params object[] parameters)
{
// take REST API method info from parameters
MethodInfo restMethod = parameters[0] as MethodInfo;
// get DeprecatedMethod attribute
DeprecatedMethodAttribute attr = restMethod.GetRadishAttribute<DeprecatedMethodAttribute>();
// return SinceVersion value
return attr.SinceVersion;
}
Finally, we need to mark required REST API methods handlers with DeprecatedMethodAttribute like:
// GET: "/api/pets/{id}"
[DeprecatedMethod("1.2")]