thomaxxl / safrs

SqlAlchemy Flask-Restful Swagger Json:API OpenAPI
GNU General Public License v3.0
405 stars 73 forks source link

`count` property value is wrong for `GET` methods #156

Open AlexandreKavalerski opened 11 months ago

AlexandreKavalerski commented 11 months ago

When page limit is applied, the value for count property in get method is wrong. It returns the count of all records instead of the current page length.

Refer to methods:

count is being calculated based on initial length of object_query, but it should consider instances length after applying pagination.

thomaxxl commented 11 months ago

Hi,

This is by design: if you want the number of items returned you can use the length of data[]. You can change this behaviour by overriding _s_count , eg:

class BaseModel(SAFRSBase, db.Model):
    __abstract__ = True
    def _s_count():
        return 100

class User(BaseModel):
    """
    description: My User description
    """

    __tablename__ = "Users"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    email = db.Column(db.String)

I added the meta.total to be compatible with a particular frontend client but it's the same value as meta.count. I may make this more configurable in the future though because it's not too obvious.