yahehe / Nancy.Swagger

Nancy plugin for generated API documentation in Swagger format.
MIT License
133 stars 60 forks source link

Nancy.Swagger.Annotations - support of annotating querystring routeparam that is non primitive type #177

Closed foxkiong closed 4 years ago

foxkiong commented 4 years ago

The current way of annotating operations requires every query string to be a primitive type of parameter, such as:

`private async Task<PagedResult> GetUserGroupChannelsAsync(

        [RouteParam(ParameterIn.Path, Name = "usergroupid", ParamType = typeof(Guid)] Guid usergroupid,
        [RouteParam(ParameterIn.Query, Name = "keyword", ParamType = typeof(string)] string keyword,
        [RouteParam(ParameterIn.Query, Name = "page", ParamType = typeof(int)] int page,
        [RouteParam(ParameterIn.Query, Name = "pagecount", ParamType = typeof(int)] int pagecount,
        [RouteParam(ParameterIn.Query, Name = "sort", ParamType = typeof(string)] string sort,
        [RouteParam(ParameterIn.Query, Name = "channeltype", ParamType = typeof(string)] string channeltype,
        [RouteParam(ParameterIn.Query, Name = "field", ParamType = typeof(string] string field)`

It will be desirable to be able to annotate such method in such a way:

private async Task<PagedResult<Channel>> GetAllChannelsAsync( [RouteParam(ParameterIn.Query, Name = "QueryChannels", ParamType = typeof(QueryChannels)] QueryChannels queryChannels)

where the QueryChannels object is annotated on its own class:

` Model("Query Channels")] public class QueryChannels : ChannelDirectoryQuery<PagedResult> { [ModelProperty(Ignore = true)] public Guid? ChannelId { get; set; }

    [ModelProperty(Description = "search keyword")]
    public string Keyword { get; set; }

    [ModelProperty(Description = "channel type")]
    public string Type { get; set; }

    [ModelProperty(Description = "which field to sort", Required = true)]
    public string Sort { get; set; }

    [ModelProperty(Description = "page number", Minimum = 1, Maximum = 1000)]
    public int Page { get; set; } = 1;

    [ModelProperty(Description = "how many pages in total", Minimum = 1 ,Maximum = 65536)]
    public int PageCount { get; set; } = 50;

    [ModelProperty(Description = "field")]
    public string Field { get; set; }
}`
jnallard commented 4 years ago

For clarification, this would document an API that would be called like this: https://mysite.com/my-path?keyword=test1&type=test2&[...]&field=test6?

foxkiong commented 4 years ago

The changes seem fine to me. I don't mind the convenience class at all.

However, I like to always be able to look at an example! Can you add something to the respective sample project?

Good idea! I will work out an example in the sample project for clarification.

foxkiong commented 4 years ago

For clarification, this would document an API that would be called like this: https://mysite.com/my-path?keyword=test1&type=test2&[...]&field=test6?

yes ,exactly!

foxkiong commented 4 years ago

hi @jnallard , I'd added the sample , appreciate if you can review it. thanks!

jnallard commented 4 years ago

@foxkiong Thanks for making the changes!