Skip to content

Commit

Permalink
Add support for extra properties on a Route
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
jvyden committed Oct 17, 2023
1 parent fa8bd20 commit f3baa90
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AttribDoc.Example/ExampleApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static int Add(int a, int b)

[DocSummary("Adds FirstNumber to SecondNumber.")]
[DocError(typeof(NotImplementedException), "A number is negative")]
[DocCustomInfo("test", "Custom information")]
[DocCustomInfo("test2", 42)]
[DocRequestBody(typeof(AddBody))]
public static int AddWithBody(AddBody body) => Add(body.FirstNumber, body.SecondNumber);

Expand Down
21 changes: 21 additions & 0 deletions AttribDoc/Attributes/DocCustomInfoAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Reflection;

namespace AttribDoc.Attributes;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class DocCustomInfoAttribute : DocAttribute
{
private readonly string _key;
private readonly object _value;

public DocCustomInfoAttribute(string key, object value)
{
_key = key;
_value = value;
}

public override void AddDataToRouteDocumentation(MethodInfo method, Route route)
{
route.ExtraProperties.Add(this._key, this._value);
}
}
2 changes: 2 additions & 0 deletions AttribDoc/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Route

public List<Parameter> Parameters { get; set; } = new();
public List<Error> PotentialErrors { get; set; } = new();

public Dictionary<string, object> ExtraProperties { get; set; } = new();

public object? ExampleRequestBody { get; set; }
public object? ExampleResponse { get; set; }
Expand Down
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,58 @@ public static Weather GetWeather()
"IsFahrenheit": false
}
}
```

# Custom information

As of v1.2.0, you can also include objects to include in a key/value pair to be included in the object.

The easiest way is to simply use the `DocCustomInfo` attribute:

```csharp
[DocSummary("Adds FirstNumber to SecondNumber.")]
[DocError(typeof(NotImplementedException), "A number is negative")]
[DocCustomInfo("test", "Custom information")]
[DocCustomInfo("test2", 42)]
[DocRequestBody(typeof(AddBody))]
public static int AddWithBody(AddBody body) => Add(body.FirstNumber, body.SecondNumber);
```

You can also do this from a custom attribute:

```csharp
public class DocCustomAttribute : DocAttribute
{
public override void AddDataToRouteDocumentation(MethodInfo method, Route route)
{
route.ExtraProperties.Add("Test", 42);
}
}
```

Either method will result in something like this:

```json
{
"Method": "GET",
"RouteUri": "/addwithbody",
"Summary": "Adds FirstNumber to SecondNumber.",
"AuthenticationRequired": false,
"Parameters": [],
"PotentialErrors": [
{
"Name": "NotImplementedException",
"OccursWhen": "A number is negative"
}
],
"ExtraProperties": {
"test": "Custom information",
"test2": 42
},
"ExampleRequestBody": {
"FirstNumber": 0,
"SecondNumber": 0
},
"ExampleResponse": null
}
```

0 comments on commit f3baa90

Please sign in to comment.