Define error responses for endpoints in the OpenAPI spec #7826

Closed
opened 2025-12-29 20:28:43 +01:00 by adam · 4 comments
Owner

Originally created by @amhn on GitHub (Mar 31, 2023).

NetBox version

v3.5-beta

Feature type

Change to existing functionality

Proposed functionality

This is based on #4986

The new API definition only includes the default response for a path. This makes it hard for code generators to create working code.

Adding the following responses to the relevant paths would help:
400: All responses?
404: All except post and bulk actions
403: All responses with security

Use case

More correct API definition to help library developers and automatic code generators

Database changes

No response

External dependencies

No response

Originally created by @amhn on GitHub (Mar 31, 2023). ### NetBox version v3.5-beta ### Feature type Change to existing functionality ### Proposed functionality This is based on #4986 The new API definition only includes the default response for a path. This makes it hard for code generators to create working code. Adding the following responses to the relevant paths would help: 400: All responses? 404: All except post and bulk actions 403: All responses with security ### Use case More correct API definition to help library developers and automatic code generators ### Database changes _No response_ ### External dependencies _No response_
adam added the type: featurepending closurestatus: under reviewtopic: OpenAPI labels 2025-12-29 20:28:43 +01:00
adam closed this issue 2025-12-29 20:28:43 +01:00
Author
Owner

@amhn commented on GitHub (Mar 31, 2023):

@arthanson This is related to the new OpenAPI spec. See also #11808

I did look into it and it seems adding something like the following to NetboxAutoschema might be sufficient:

    def _get_response_bodies(self, direction='response'):
        response_bodies = super()._get_response_bodies(direction)
        if len(self.get_auth()):
            response_bodies['403'] = {
                'description': 'Permission denied',
            }

        response_bodies['400'] = {
            'content': {
                'application/json': build_media_type_object(
                    build_generic_type(),
                )
            },
            'description': 'Generic API error',
        }

        if self.method == 'GET' and not self._is_list_view():
            response_bodies['404'] = {
                'description': 'Object not found',
            }
        return response_bodies

Instead of build_generic_type for the 400 response, may be there should be a Schema defined and referenced here.

This code is just a proof of concept and probably needs more work.

@amhn commented on GitHub (Mar 31, 2023): @arthanson This is related to the new OpenAPI spec. See also #11808 I did look into it and it seems adding something like the following to NetboxAutoschema might be sufficient: ```python def _get_response_bodies(self, direction='response'): response_bodies = super()._get_response_bodies(direction) if len(self.get_auth()): response_bodies['403'] = { 'description': 'Permission denied', } response_bodies['400'] = { 'content': { 'application/json': build_media_type_object( build_generic_type(), ) }, 'description': 'Generic API error', } if self.method == 'GET' and not self._is_list_view(): response_bodies['404'] = { 'description': 'Object not found', } return response_bodies ``` Instead of build_generic_type for the 400 response, may be there should be a Schema defined and referenced here. This code is just a proof of concept and probably needs more work.
Author
Owner

@arthanson commented on GitHub (Apr 19, 2023):

I think this is a partial solution, see the discussion: https://github.com/tfranzel/drf-spectacular/issues/101#issuecomment-1497790997 The ValidationError is problematic according to spectacular maintainer. Something like https://github.com/ghazi-git/drf-standardized-errors would be a more complete solution (also integrates with spectacular) but would change the error return format for all APIs, not sure what ramifications if any this would have - also would be another package to add... investigating.

@arthanson commented on GitHub (Apr 19, 2023): I think this is a partial solution, see the discussion: https://github.com/tfranzel/drf-spectacular/issues/101#issuecomment-1497790997 The ValidationError is problematic according to spectacular maintainer. Something like https://github.com/ghazi-git/drf-standardized-errors would be a more complete solution (also integrates with spectacular) but would change the error return format for all APIs, not sure what ramifications if any this would have - also would be another package to add... investigating.
Author
Owner

@github-actions[bot] commented on GitHub (Jul 24, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Jul 24, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Aug 24, 2023):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Aug 24, 2023): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7826