Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供QuickApi对NSwag的兼容支持 #8

Open
vipwan opened this issue Oct 13, 2023 · 9 comments
Open

提供QuickApi对NSwag的兼容支持 #8

vipwan opened this issue Oct 13, 2023 · 9 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed

Comments

@vipwan
Copy link
Owner

vipwan commented Oct 13, 2023

兼容NSwag的成本较高,需要做的工作比较多,欢迎有兴趣的伙伴贡献代码 😃

@vipwan
Copy link
Owner Author

vipwan commented Oct 13, 2023

RicoSuter/NSwag#4163

@vipwan vipwan added the help wanted Extra attention is needed label Oct 13, 2023
vipwan added a commit that referenced this issue Oct 13, 2023
@vipwan
Copy link
Owner Author

vipwan commented Oct 13, 2023

    [QuickApi("content", Group = "hello", Verbs = Verb.GET)]
    [QuickApiSummary("ContentApi", "ContentApi")]
    public class ContentApi : BaseQuickApi<EmptyRequest, ContentResponse>
    {
        public override Task<ContentResponse> ExecuteAsync(EmptyRequest request)
        {
            return Task.FromResult(new ContentResponse("Hello World content!"));
        }
    }

当前NSwag不支持使用WithOpenApi()扩展设置 Summary & Description 需要使用[QuickApiSummary] 设置.
RicoSuter/NSwag#3802

@vipwan
Copy link
Owner Author

vipwan commented Oct 13, 2023

    [QuickApi("world2", Group = "hello", Verbs = Verb.POST)]
    [QuickApiSummary("this is summary", "this is description")]
    public class Hello2Api : BaseQuickApi<HelloApiRequest, HelloApiResponse>
    {
        public override async Task<HelloApiResponse> ExecuteAsync(HelloApiRequest request)
        {
            await Task.CompletedTask;
            return new HelloApiResponse
            {
                Message = $"Hello {request.Name}  {request.Alias} ",
                Alias = $"{request.Alias} {request.HelloService.Hello(request.Name)} " //别名测试 Alias -> a
            };
        }
       
        public override RouteHandlerBuilder HandlerBuilder(RouteHandlerBuilder builder)
        {
            //如果请求是POST,可以添加Example.否则会忽略
            builder.WithExample(new HelloApiRequest
            {
                Name = "vipwan",
                Alias = "alias",
                Q = "q54543534",
                UserName = "u545435",
                Password = "p234565",
            });
            return base.HandlerBuilder(builder);
        }
    }

提供 WithExample()扩展,用于兼容 NSwag的 RequestBody.ActualSchema.Example

image

@vipwan vipwan added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 13, 2023
@vipwan
Copy link
Owner Author

vipwan commented Oct 13, 2023

How to Use

1. Step 1

//swagger
builder.Services.AddQuickApiDocument(options =>
{
    options.UseControllerSummaryAsTagDescription = true;
    options.PostProcess = document =>
    {
        document.Info = new OpenApiInfo
        {
            Version = "Quick API Demo V1",
            Title = "Quick API Demo",
            Description = "Biwen.QuickApi Demo",
            TermsOfService = "https://github.com/vipwan",
            Contact = new OpenApiContact
            {
                Name = "Contact Me",
                Url = "https://github.com/vipwan/Biwen.QuickApi"
            },
            License = new OpenApiLicense
            {
                Name = "MIT License",
                Url = "https://github.com/vipwan/Biwen.QuickApi/blob/master/LICENSE.txt"
            }
        };
    };
},
new SecurityOptions()); //提供UI Authorization支持
  1. Step 2
//swagger
app.UseQuickApiSwagger();

@vipwan vipwan changed the title 提供SwaggerUI 兼容 提供QuickApi对NSwag的兼容支持 Oct 13, 2023
vipwan added a commit that referenced this issue Oct 14, 2023
@vipwan
Copy link
Owner Author

vipwan commented Oct 14, 2023

image

提供内置验证器对NSwag的支持!

@vipwan vipwan self-assigned this Oct 14, 2023
@vipwan vipwan pinned this issue Oct 14, 2023
@vipwan
Copy link
Owner Author

vipwan commented Oct 14, 2023

请注意 NSwag和WithOpenApi()兼容问题,如果传参需要描述信息 不可使用

.WithOpenApi(generatedOperation =>
{
var parameter = generatedOperation.Parameters[0];
parameter.Description = "The ID associated with the created Todo";
return generatedOperation;
});

请使用 : [Description]:

    public class MyRequest : BaseRequest<MyRequest>
    {
        /// <summary>
        /// 别名测试
        /// </summary>
        [AliasAs("a")]
        [Description("别名测试使用:a")]
        public string? Alias { get; set; }

        [FromQuery]
        [Description("测试FromQuery:Q")]
        public string? Q { get; set; }

        [FromKeyedServices("hello")]
        public HelloService HelloService { get; set; }
    }

vipwan added a commit that referenced this issue Oct 14, 2023
@vipwan
Copy link
Owner Author

vipwan commented Oct 14, 2023

支持使用[DefaultValue]标注默认值

    public abstract class AuthRequest<T> : BaseRequest<T> where T : class, new()
    {
        [Description("登录用户名")]
        [DefaultValue("vipwan@ms.co.ltd")]
        public string? UserName { get; set; }

        [Description("登录密码")]
        public string? Password { get; set; }
    }

@vipwan
Copy link
Owner Author

vipwan commented Oct 15, 2023

EndpointGroupName支持. 当前支持多文档的情况下EndpointGroupName将非常有用

QuickApi 支持 [EndpointGroupName]特性标注,以及HandlerBuilder.WithGroupName().

    [QuickApi("endpointgroup")]
    [QuickApiSummary("分组测试", "分组测试")]
    [EndpointGroupName("group1")]
    public class EndpointGroupApi : BaseQuickApi
    {
        public override Task<EmptyResponse> ExecuteAsync(EmptyRequest request)
        {
            return Task.FromResult(EmptyResponse.New);
        }
        public override RouteHandlerBuilder HandlerBuilder(RouteHandlerBuilder builder)
        {
            builder.WithTags("group");
            //builder.WithGroupName("group1");
            return base.HandlerBuilder(builder);
        }
    }

如果需要批量设置, 比如路由Group下:

var apis = app.MapBiwenQuickApis();
var groupAdmin = apis.FirstOrDefault(x => x.Group == "admin");
groupAdmin.RouteGroupBuilder?
    .WithTags("authorization")         //自定义Tags
    .WithGroupName("admin")        //自定义EndpointGroupName
    ;

@vipwan
Copy link
Owner Author

vipwan commented Oct 16, 2023

如果接口过时可以标记 : QuickApiSummaryAttribute 或者 ObsoleteAttribute

    [QuickApi("world5", Verbs = Verb.GET)]
    [QuickApiSummary("过期测试", "过期测试",IsDeprecated =true)]
    [Obsolete("过期测试",false)]
    public class Hello5Api : BaseQuickApi
    {
        public override async Task<EmptyResponse> ExecuteAsync(EmptyRequest request)
        {
            await Task.CompletedTask;
            return EmptyResponse.New;
        }
    }

vipwan added a commit that referenced this issue Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant