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

DRAFT: 15404 schema map for SerializedPKRelatedField #15449

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

arthanson
Copy link
Collaborator

Fixes: #15404

Fixes drf-spectacular mapping for SerializedPKRelatedField

@arthanson arthanson marked this pull request as ready for review March 15, 2024 19:05
@deathbeam
Copy link
Contributor

deathbeam commented Mar 27, 2024

I tested this a bit (by putting this extension to my code locally) and it do not resolves #15404 fully as it is not generating references to the Nested objects like before and instead its generating schema with type "object" and fields inserted inside.

@arthanson
Copy link
Collaborator Author

@deathbeam can you please provide a screenshot or text of what you are expecting to see and what you are seeing?

@deathbeam
Copy link
Contributor

Sure. So for example VMInterface before the openapi changes:

      "required": [
        "virtual_machine",
        "name"
      ],
      "type": "object",
      "properties": {
        "id": {
          "title": "ID",
          "type": "integer",
          "readOnly": true
        },
        "url": {
          "title": "Url",
          "type": "string",
          "format": "uri",
          "readOnly": true
        },
        "display": {
          "title": "Display",
          "type": "string",
          "readOnly": true
        },
        "virtual_machine": {
          "$ref": "#/definitions/NestedVirtualMachine"
        },
        "name": {
          "title": "Name",
          "type": "string",
          "maxLength": 64,
          "minLength": 1
        },
        "enabled": {
          "title": "Enabled",
          "type": "boolean"
        },
        "parent": {
          "$ref": "#/definitions/NestedVMInterface"
        },
        "bridge": {
          "$ref": "#/definitions/NestedVMInterface"
        },
        "mtu": {
          "title": "MTU",
          "type": "integer",
          "maximum": 65536,
          "minimum": 1,
          "x-nullable": true
        },
        "mac_address": {
          "title": "MAC Address",
          "type": "string",
          "x-nullable": true
        },
        "description": {
          "title": "Description",
          "type": "string",
          "maxLength": 200
        },
        "mode": {
          "title": "Mode",
          "required": [
            "label",
            "value"
          ],
          "type": "object",
          "properties": {
            "label": {
              "type": "string",
              "enum": [
                "Access",
                "Tagged",
                "Tagged (All)"
              ]
            },
            "value": {
              "type": "string",
              "enum": [
                "access",
                "tagged",
                "tagged-all"
              ]
            }
          }
        },
        "untagged_vlan": {
          "$ref": "#/definitions/NestedVLAN"
        },
        "tagged_vlans": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/NestedVLAN"
          },
          "uniqueItems": true
        },
        "vrf": {
          "$ref": "#/definitions/NestedVRF"
        },
        "l2vpn_termination": {
          "$ref": "#/definitions/NestedL2VPNTermination"
        },
        "tags": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/NestedTag"
          }
        },
        "custom_fields": {
          "title": "Custom fields",
          "type": "object",
          "default": {}
        },
        "created": {
          "title": "Created",
          "type": "string",
          "format": "date-time",
          "readOnly": true,
          "x-nullable": true
        },
        "last_updated": {
          "title": "Last updated",
          "type": "string",
          "format": "date-time",
          "readOnly": true,
          "x-nullable": true
        },
        "count_ipaddresses": {
          "title": "Count ipaddresses",
          "type": "integer",
          "readOnly": true
        },
        "count_fhrp_groups": {
          "title": "Count fhrp groups",
          "type": "integer",
          "readOnly": true
        }
      }

VMInterface now (with your fix applied):

        "type": "object",
        "description": "Adds support for custom fields and tags.",
        "properties": {
          "id": {
            "type": "integer",
            "readOnly": true
          },
          "url": {
            "type": "string",
            "format": "uri",
            "readOnly": true
          },
          "display": {
            "type": "string",
            "readOnly": true
          },
          "virtual_machine": {
            "$ref": "#/components/schemas/NestedVirtualMachine"
          },
          "name": {
            "type": "string",
            "maxLength": 64
          },
          "enabled": {
            "type": "boolean"
          },
          "parent": {
            "allOf": [
              {
                "$ref": "#/components/schemas/NestedVMInterface"
              }
            ],
            "nullable": true
          },
          "bridge": {
            "allOf": [
              {
                "$ref": "#/components/schemas/NestedVMInterface"
              }
            ],
            "nullable": true
          },
          "mtu": {
            "type": "integer",
            "maximum": 65536,
            "minimum": 1,
            "nullable": true
          },
          "mac_address": {
            "type": "string",
            "nullable": true
          },
          "description": {
            "type": "string",
            "maxLength": 200
          },
          "mode": {
            "type": "object",
            "properties": {
              "value": {
                "enum": [
                  "access",
                  "tagged",
                  "tagged-all",
                  ""
                ],
                "type": "string",
                "description": "* `access` - Access\n* `tagged` - Tagged\n* `tagged-all` - Tagged (All)",
                "x-spec-enum-id": "79109bd9dbb73a3c"
              },
              "label": {
                "type": "string",
                "enum": [
                  "Access",
                  "Tagged",
                  "Tagged (All)"
                ]
              }
            }
          },
          "untagged_vlan": {
            "allOf": [
              {
                "$ref": "#/components/schemas/NestedVLAN"
              }
            ],
            "nullable": true
          },
          "tagged_vlans": {
            "type": "array",
            "items": {
              "type": "object",
              "description": "Represents an object related through a ForeignKey field. On write, it accepts a primary key (PK) value or a\ndictionary of attributes which can be used to uniquely identify the related object. This class should be\nsubclassed to return a full representation of the related object on read.",
              "properties": {
                "id": {
                  "type": "integer",
                  "readOnly": true
                },
                "url": {
                  "type": "string",
                  "format": "uri",
                  "readOnly": true
                },
                "display": {
                  "type": "string",
                  "readOnly": true
                },
                "vid": {
                  "type": "integer",
                  "maximum": 4094,
                  "minimum": 1,
                  "title": "VLAN ID",
                  "description": "Numeric VLAN ID (1-4094)"
                },
                "name": {
                  "type": "string",
                  "maxLength": 64
                }
              },
              "required": [
                "display",
                "id",
                "name",
                "url",
                "vid"
              ]
            }
          },
          "vrf": {
            "allOf": [
              {
                "$ref": "#/components/schemas/NestedVRF"
              }
            ],
            "nullable": true
          },
          "l2vpn_termination": {
            "allOf": [
              {
                "$ref": "#/components/schemas/NestedL2VPNTermination"
              }
            ],
            "readOnly": true,
            "nullable": true
          },
          "tags": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/NestedTag"
            }
          },
          "custom_fields": {
            "type": "object",
            "additionalProperties": {}
          },
          "created": {
            "type": "string",
            "format": "date-time",
            "readOnly": true,
            "nullable": true
          },
          "last_updated": {
            "type": "string",
            "format": "date-time",
            "readOnly": true,
            "nullable": true
          },
          "count_ipaddresses": {
            "type": "integer",
            "readOnly": true
          },
          "count_fhrp_groups": {
            "type": "integer",
            "readOnly": true
          }
        },
        "required": [
          "count_fhrp_groups",
          "count_ipaddresses",
          "created",
          "display",
          "id",
          "l2vpn_termination",
          "last_updated",
          "name",
          "url",
          "virtual_machine"
        ]

Notice how before tagged_vlans were referring to "$ref": "#/definitions/NestedVLAN". Now, without your change tagged_vlans are list of ints. With your change its list of objects instead of being reference to existing definition, e.g it is now:

"tagged_vlans": {
            "type": "array",
            "items": {
              "type": "object",
              "description": "Represents an object related through a ForeignKey field. On write, it accepts a primary key (PK) value or a\ndictionary of attributes which can be used to uniquely identify the related object. This class should be\nsubclassed to return a full representation of the related object on read.",
              "properties": {
                "id": {
                  "type": "integer",
                  "readOnly": true
                },
                "url": {
                  "type": "string",
                  "format": "uri",
                  "readOnly": true
                },
                "display": {
                  "type": "string",
                  "readOnly": true
                },
                "vid": {
                  "type": "integer",
                  "maximum": 4094,
                  "minimum": 1,
                  "title": "VLAN ID",
                  "description": "Numeric VLAN ID (1-4094)"
                },
                "name": {
                  "type": "string",
                  "maxLength": 64
                }
              },
              "required": [
                "display",
                "id",
                "name",
                "url",
                "vid"
              ]
            }
          },

@arthanson arthanson marked this pull request as draft April 18, 2024 15:50
@arthanson arthanson removed the request for review from jeremystretch April 18, 2024 15:50
@arthanson arthanson changed the title 15404 schema map for SerializedPKRelatedField DRAFT: 15404 schema map for SerializedPKRelatedField Apr 18, 2024
Copy link

This PR has been automatically marked as stale because it has not had recent activity. It will be closed automatically if no further action is taken.

@github-actions github-actions bot added the pending closure Requires immediate attention to avoid being closed for inactivity label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending closure Requires immediate attention to avoid being closed for inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Netbox openapi schema is generating incorrect response types
2 participants