netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.25k stars 2.59k forks source link

Swagger not updating when new API endpoints added in plugin #18021

Open tbotnz opened 22 hours ago

tbotnz commented 22 hours ago

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.3

Python Version

3.11

Steps to Reproduce

1) add an endpoint to an API plugin 2) reload netbox

Expected Behavior

swagger should update showing the new endpoint

Observed Behavior

Problem is caused by the caching of the swagger in redis, this needs to be cleared every time in order for the swagger to update. It does appear to load after the cache expires but this takes 24 hours

Bacloud commented 20 hours ago

Same problem with v4.1.6

bctiemann commented 11 hours ago

Can this be addressed with documentation, e.g. a note that if you add an API endpoint in your plugin you need to flush your redis cache (with accompanying instructions)?

tbotnz commented 8 hours ago

In my mind what should happen is the cached swagger should clear on container/app startup if its deemed out of date

tbotnz commented 7 hours ago

here a simple management command that fixes it once called python manage.py $command_file_name

$command_file_name.py

from django.core.management.commands.makemigrations import Command as _Command

from django.core.cache import cache

class Command(_Command):

    def handle(self, *args, **kwargs):
        """ Temporary command to clear cache keys that are not cleared by the cache clear command. """
        _KEYS_PREFIX_REQUIRING_CLEARING = [
            "*api_schema_*",
        ]
        for key_prefix in _KEYS_PREFIX_REQUIRING_CLEARING:
            matched_keys_to_delete = cache.keys(key_prefix)
            for matched_key in matched_keys_to_delete:
                cache.delete(matched_key)