Git Product home page Git Product logo

Comments (3)

k-nishizaki avatar k-nishizaki commented on June 16, 2024 2

You can include the PagingArgs parameter in the OpenAPI document using the following code.

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class PagingAttribute: Attribute
    {
    }
    public class PagingOperationFilter : IOperationFilter
    {
        private class AttrInfo
        {
            public string? Name { get; set; }
            public string? Description { get; set; }
            public string? TypeName { get; set; }
        }

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            var attributes = (context.MethodInfo.DeclaringType?.GetCustomAttributes<PagingAttribute>(true) ?? new PagingAttribute[0])
                .Union(context.MethodInfo.GetCustomAttributes<PagingAttribute>(true));


            AttrInfo[] items = 
            {
                new AttrInfo{
                    Name = "$take",
                    Description = "Takes",
                    TypeName = "number",
                },
                new AttrInfo{
                    Name = "$skip",
                    Description = "Skips",
                    TypeName = "number",
                },
                new AttrInfo{
                    Name = "$page",
                    Description = "Pages",
                    TypeName = "number",
                },
            };


            foreach (var attribute in attributes)
            {

                foreach (var item in items)
                {
                    operation.Parameters.Add(new OpenApiParameter
                    {
                        Name = item.Name,
                        Description = item.Description,
                        In = ParameterLocation.Query,
                        Required = false,
                        Schema = new OpenApiSchema { Type = item.TypeName },
                    });

                }
            }
        }
    }
    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddSwaggerGen(options =>
        {
            ...
            options.OperationFilter<OpenApi.PagingOperationFilter>();
        });
    }

Then, add the [Paging] attribute to the operations that are ready to be paginated (you can do this either manually or by editing the template).

        [Paging]
        public Task<IActionResult> GetByArgs(string? firstName = default, string? lastName = default)

from beef.

chullybun avatar chullybun commented on June 16, 2024 1

@k-nishizaki, this recommendation is awesome thanks. I will integrate this into CoreEx and Beef given prevalence of paging. Minor tweak, the key three properties should really be $skip, $take and $count; $page and $size are alternatives to $skip and $take (they calculate back to).

FYI, there are other special case $ prefixes; however, I don't think these should be included into the swagger output.

Will update this thread once updated and published :-)

from beef.

chullybun avatar chullybun commented on June 16, 2024 1

@k-nishizaki, step 1 complete, integration into CoreEx v2.9.0: https://github.com/Avanade/CoreEx/pull/63/files

from beef.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.