Skip to content

OpenAPI Schema and Pagination #9195

Closed

Description

@vsviridov

Please verify that this bug has NOT been raised before.

  • I checked and didn't find a similar issue

Describe the bug*

I'm running Inventree 0.17.6 (from the shell script installer), api version 294.

The OpenAPI definition for most endpoints describe a Paginated... object with following structure:

{
  "count": 123,
  "next": "http://...",
  "previous": "http://...",
  "data": []
}

however the data returned is simply an array of items, no additional pagination properties. They do appear if I manually supply a limit= parameter, but the openapi spec does not seem to reflect that.

Image Image

Steps to Reproduce

Access any API list endpoint without providing a limit

Expected behaviour

OpenAPI schema should match the server output

Image

Deployment Method

  • Docker
  • Package
  • Bare metal
  • Other - added info in Steps to Reproduce

Version Information

0.17.6

Please verify if you can reproduce this bug on the demo site.

  • I can reproduce this bug on the demo site.

Relevant log output

Activity

  1. added
    bug
    question
    triage:not-checked
    on Feb 27, 2025
  2. SchrodingersGat commented on Feb 27, 2025

    @SchrodingersGat
    Member

    @vsviridov an interesting problem. Have you tested this across multiple API endpoints? Or only the part-category-tree endpoint?

  3. added
    api
    and removed
    bug
    triage:not-checked
    on Feb 27, 2025
  4. vsviridov commented on Feb 27, 2025

    @vsviridov
    Author

    I've tried with category tree, category list, and parts list so far... Could try a few more...

    admin_config_list fails if pagination is requested

    Traceback (most recent call last):
    
    File "/root/.local/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
    
    response = handler(request, *args, **kwargs)
    
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    File "/root/.local/lib/python3.11/site-packages/rest_framework/generics.py", line 199, in get
    
    return self.list(request, *args, **kwargs)
    
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    File "/root/.local/lib/python3.11/site-packages/rest_framework/mixins.py", line 40, in list
    
    page = self.paginate_queryset(queryset)
    
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    File "/root/.local/lib/python3.11/site-packages/rest_framework/generics.py", line 171, in paginate_queryset
    
    return self.paginator.paginate_queryset(queryset, self.request, view=self)
    
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    File "/root/.local/lib/python3.11/site-packages/rest_framework/pagination.py", line 395, in paginate_queryset
    
    return list(queryset[self.offset:self.offset + self.limit])
    
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    TypeError: unhashable type: 'slice'
    

    attachment_list behaves as outline in the ticket
    background_task_failed_list same
    background_task_pending_list same
    barcode_history_list does not paginate, all results are just an array (however it might be affected by the setting for collection of scanned barcodes).
    bom_list same as outlined in the ticket
    bom_substitute_list same
    build_list same
    build_item_list same
    build_line_list same
    company_list same

    I could go on, but the pattern seems to be consistent

  5. SchrodingersGat commented on Mar 1, 2025

    @SchrodingersGat
    Member

    I have a fix for the config list here - #9210

    Regarding the overarching issue - it looks like we are missing the PAGE_SIZE attribute in our REST_FRAMEWORK setup:

    If I add PAGE_SIZE: 25 to this setup dict, then the default behaviour is to paginate.

    Currently, if you supply a ?limit=<x> query parameter, the API data will paginate. If not supplied, you get all the items.

    Changing this behaviour will break a lot of front-end features which expect a full dataset.

    Alternatively the tables (and other places where large datasets are expected) require the pagination functionality to work as expected.

    Not sure how to consolidate all this.

  6. vsviridov commented on Mar 1, 2025

    @vsviridov
    Author
  7. SchrodingersGat commented on Mar 1, 2025

    @SchrodingersGat
    Member

    Also, some fields are not even described in the swagger, e.g. I was trying to get parts based on category, it works if I pass the parameter over the query string, but the codegen version doesn't have it...

    There are some legacy filtering attributes which are not exposed to the auto-doc based on how we do the filtering. I am working (gradually) to migrate to the "proper" approach - so that they are documented correctly. If you find any instances like this please raise separate issues so that we can tackle them individually.

  8. SchrodingersGat commented on Mar 1, 2025

    @SchrodingersGat
    Member

    Any way to drive the output schema based on the presence of limit parameter?

    I'm not sure - perhaps if you can find a way to achieve this nicely, I'd happily review a PR :)

  9. vsviridov commented on Mar 1, 2025

    @vsviridov
    Author
  10. 1337joe commented on Mar 3, 2025

    @1337joe
    Contributor

    I'll chime in since I've been poking at this too.

    If you're willing to modify the api.yaml before running your code generator you can easily set the limit attribute to be required with this sed 1-liner:

    sed -i -z -E 's/name: (limit\W+)required: false/name: \1required: true/g' api.yaml

    Alternately, you can regenerate the schema without paging by setting up a dev environment, modifying src/backend/InvenTree/InvenTree/settings.py to change DEFAULT_PAGINATION_CLASS to None, then running invoke dev.schema --ignore-warnings --filename api.yaml

  11. 1337joe commented on Mar 15, 2025

    @1337joe
    Contributor

    Regarding setting a oneOf discriminator, it may be possible to annotate with some variation of @extend_schema to add a PolymorphicProxySerializer to generate a oneOf discriminator offering paginated object / list of objects.

    I just can't figure out where that would go considering that pagination is set by configuration.

  12. wolflu05 commented on Mar 15, 2025

    @wolflu05
    Member

    The question is, why do we have such a shape changing api based on parameters? Wound it be the cleanest to remove that as a braking Ching for 1.0.0? So that we always return the paginated way and remove the full response entirely?

  13. SchrodingersGat commented on Mar 15, 2025

    @SchrodingersGat
    Member

    The question is, why do we have such a shape changing api based on parameters?

    Let's say my lack of experience and clear design goals when I first started developing the API ;)

    There are four main areas where the API is "mutated" based on parameters in an undocumented fashion:

    Pagination

    Most (but not all) list endpoints can be passed pagination parameters to return a paginated queryset. By default, you get the whole list.

    This is "non standard" for sure - but I kind of like the flexibility it allows. The client can define whether they want a paginated response or not - for example the tables enforce pagination, but some elements (like fetching all settings) you want to ensure that you capture everything in a single query.

    Not sure how best to proceed here - potentially I think I'd like to see it documented more clearly but retain the current behaviour.

    Filtering

    Filtering the querysets via query params can have some "hidden" features. That is, query parameters which are not exposed to the API schema because they are hard-coded into the filter_queryset method. This is becoming less of a problem as I have been working to reduce this approach. Now, we use the django-rest-framework-filters to define filters which are exposed to the API auto-doc.

    There are still some to transfer across but it's getting there!

    Related Fields

    This is the most "inconsistent" part of the API - and one I'd like to address (somehow). At multiple points across the API we have the ability to specify which "detail" attributes are annotated against the serializer.

    For example, you can do /api/stock/?part_detail=true which will add in the part_detail field against each stock item. However, there is no auto-doc for this.

    Additionally, this is not consistent across the API. Some x_detail fields are always there, some are default-on, some are default-off. There is not really a clear methodology around these choices to be honest.

    The reason for this approach is to reduce serialization burden on the server when it is not needed. In some cases, you don't need the part_detail (or whatever) against a particular dataset. A good example would be when fetching all stock items for a given part. You already have the part information, and it's going to be the same for every stock item, so why waste time serializing it?

    However, potentially an argument could be made that it is better to reduce the complexity of the API calls, at the expense of query time. Happy to discuss this further.

    Brief Serializers

    Finally, at different points in the API we have the concept of a "brief" version of a particular serializer. This has a reduced set of fields allowing "important" data to be serialized into an x_detail field. Might be worth considering removing these in favor of a single defined serializer for each model.


    In summary, there are a lot of legacy implementations which are just baggage from my journey from embedded software to web development ;) Any of these are up for change, let's discuss :)

  14. wolflu05 commented on Mar 16, 2025

    @wolflu05
    Member

    Do not get me wrong, this was not meant in any offensive way, every project starts small onetime. Awesome what you built up with inventree. It was just some idea I'd liked to throw in which may solve that issue, and now with the v1.0 release this may be the right time. Happy to discuss this @matmair wasn't that also somwthing that you like'd to have, non shape changing API endpoint responses?

  15. matmair commented on Mar 23, 2025

    @matmair
    Member

    Non-shape changing APIs would make things a lot easier - would be breaking probably.

  16. matmair commented on Apr 18, 2025

    @matmair
    Member

    Also see #9481

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions