Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI (Swagger) schema shouldn't provide choices for Status references #3215

Closed
glennmatthews opened this issue Feb 2, 2023 · 3 comments · Fixed by #3225
Closed

OpenAPI (Swagger) schema shouldn't provide choices for Status references #3215

glennmatthews opened this issue Feb 2, 2023 · 3 comments · Fixed by #3225
Assignees
Labels
type: bug Something isn't working as expected
Milestone

Comments

@glennmatthews
Copy link
Contributor

glennmatthews commented Feb 2, 2023

Environment

  • Nautobot version (Docker tag too if applicable): all

Steps to Reproduce

Currently the OpenAPI schema generated by Nautobot will list a specific enum of permissible options for references to Status objects, e.g.

    Circuit:
      ...
      properties:
        ...
        status:
          type: object
          properties:
            value:
              type: string
              enum:
              - active
              - decommissioned
              - deprovisioning
              - offline
              - planned
              - provisioning
            label:
              type: string
              enum:
              - Active
              - Decommissioned
              - Deprovisioning
              - Offline
              - Planned
              - Provisioning

The problem, of course, is that this is NOT actually a hard-coded list of choices, it's determined at schema generation time from whatever set of Status objects happen to be defined. The moment a user adds, deletes, or edits any Status record, this part of the schema becomes inaccurate.

Most likely, the best solution here (albeit probably a breaking, i.e. 2.0 change) will be to change the status API field serializers so that they use a nested serializer like any other foreign-key reference, i.e. instead of:

      "status": {
        "value": "active",
        "label": "Active"
      }

they instead use:

    {
      "id": "64864569-9d3f-4b17-bb00-47296d9ccd0e",
      "display": "Active",
      "url": "https://demo.nautobot.com/api/extras/statuses/64864569-9d3f-4b17-bb00-47296d9ccd0e/",
      "name": "Active",
      "slug": "active",
    },
@glennmatthews glennmatthews added the type: bug Something isn't working as expected label Feb 2, 2023
@glennmatthews glennmatthews added this to the v2.0.0 milestone Feb 2, 2023
@glennmatthews
Copy link
Contributor Author

glennmatthews commented Feb 2, 2023

This would also fix any schema generation warnings that can be seen after customizing the Statuses in your installation, e.g.:

(drf_spectacular.W001) Warning: enum naming encountered a non-optimally resolvable collision for fields named "status". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "Status5a0Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.

@glennmatthews
Copy link
Contributor Author

Straightforward enough to implement, thanks to extensive use of mixin classes. The bigger challenge will be making it backwards-compatible with older API versions - there are a lot of models with Statuses, so that'll be a lot of versioned serializers.

@glennmatthews glennmatthews changed the title OpenAPI (Swagger) schema shouldn't provide choices for Status or Role references OpenAPI (Swagger) schema shouldn't provide choices for Status references Feb 3, 2023
@glennmatthews glennmatthews linked a pull request Feb 6, 2023 that will close this issue
6 tasks
@glennmatthews
Copy link
Contributor Author

Implemented for 2.0 in #3225.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working as expected
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant