khellang/Nancy.Swagger — pull request #14 by liddellj (2014-07-24 15:44:07+00:00)
Title: Model support

*Note*: this PR builds on top of https://github.com/khellang/Nancy.Swagger/pull/13.

Added support for model metadata. Firstly, you declare that a given route uses a specified model using the DSL as follows:

```c#
this.Describe["GetUsers"] = desc =>
{
return desc.AsSwagger(with =>
{
...
with.Model();
...
});
};
```

Without doing anything else, the model properties will now be picked up automatically, however - if you want to alter any of the model metadata, you can do so by providing an implementation of `ISwaggerModelDataProvider` like this:

```c#
public class UserModelDataProvider : ISwaggerModelDataProvider
{
public SwaggerModelData GetModelData()
{
return SwaggerModelData.ForType(with =>
{
with.Description("A customer or prospect.");
with.Property(x => x.subscribed)
.Description("Indicates whether the user should be subscribed or not");
with.Property(x => x.email)
.Description("Email address");
with.Property(x => x.id)
.Description("Unique Id for the user");
with.Property(x => x.created)
.Description("When the user record was created (ISO 8601)");
with.Property(x => x.updated)
.Description("When the user record was last updated (ISO 8601)");
with.Property(x => x.gender)
.Description("Gender of the user")
.Enum("m", "f", "male", "female");
with.Property(x => x.dateOfBirth)
.Description("Date of birth (ISO 8601)");
with.Property(x => x.language).Description(
"A lowercase two-letter code derived from ISO 639-1. Can optionally also include a regional code derived from ISO 3166-1");
with.Property(x => x.name).Description("The name of the user");
});
}
}
```

Such implementations will be picked up at runtime via `DefaultSwaggerModelCatalog`.

@TimSchlechter - I have not provided an attribute based implementation of this, but it should be straightforward for you to do so by implementing an alternate `ISwaggerModelCatalog` which retrieves model metadata through reflection.