Skip to content

Incorrect schema for non-integer Primary keys using PrimaryKeyRelatedField #14

Description

@BeryJu

With the default PrimaryKeyRelatedFieldConverter (https://github.com/maykinmedia/drf-jsonschema-serializer/blob/main/drf_jsonschema_serializer/converters.py#L267), using PrimaryKeyRelatedField with a foreign key field which does not use Integer primary keys creates an incorrect schema with {"type": "integer"}.

A potential fix could look something like this, however I haven't found a good method to test for all string-based fields:

@converter
class PrimaryKeyRelatedFieldConverter:
    """Custom primary key field converter which is aware of non-integer based PKs

    This is not an exhaustive fix for other non-int PKs, however in authentik we either
    use UUIDs or ints"""

    field_class = PrimaryKeyRelatedField

    def convert(self, field: PrimaryKeyRelatedField):
        model: Model = field.queryset.model
        pk_field = model._meta.pk
        if isinstance(pk_field, fields.UUIDField):
            return {"type": "string", "format": "uuid"}
        return {"type": "integer"}

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions