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

Integer lookup_field with lookup_value_regex evaluates to a string pattern instead of integer #1316

Open
lotvall opened this issue Oct 21, 2024 · 0 comments

Comments

@lotvall
Copy link

lotvall commented Oct 21, 2024

Describe the bug

When using a lookup_field for an integer on a model, together with a lookup_value_regex for runtime validation of the type, I expect the Open API type to be an integer.

The lookup_value_regex is used for validation, DRF will return 404 before entering the domain code if the lookup value does not match the regex.

However, when the Open API type is changed to a pattern string, information on the actual type is lost as it is no longer an integer.

To Reproduce

A regression test

import pytest
from django.db import models
from django.urls import include, re_path
from rest_framework import serializers, viewsets
from rest_framework.routers import SimpleRouter

from tests import assert_schema, generate_schema


class Book(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=255)

class BookSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Book
        fields = ('name',)

def _generate_simple_router_schema(viewset):
    router = SimpleRouter()
    router.register('books', viewset, basename='books')
    urlpatterns = [
        re_path('', include(router.urls)),
    ]
    return generate_schema(None, patterns=urlpatterns)

@pytest.mark.contrib('rest_framework_lookup_value')
def test_drf_lookup_value_regex_integer(no_warnings):
    class BookViewSet(viewsets.ModelViewSet):
        queryset = Book.objects.all()
        serializer_class = BookSerializer
        lookup_field = 'id'
        lookup_value_regex = r'\d+'

    assert_schema(
        _generate_simple_router_schema(BookViewSet),
        'tests/contrib/test_drf_lookup_value.yml',
    )

Expected behavior
A clear and concise description of what you expected to happen.

I expect the following yaml to be generated.

openapi: 3.0.3
info:
  title: ''
  version: 0.0.0
paths:
  /books/:
    get:
      operationId: books_list
      tags:
      - books
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Book'
          description: ''
    post:
      operationId: books_create
      tags:
      - books
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Book'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Book'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Book'
        required: true
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'
          description: ''
  /books/{id}/:
    get:
      operationId: books_retrieve
      parameters:
      - in: path
        name: id
        schema:
          type: integer
          maximum: 9223372036854775807
          minimum: -9223372036854775808
          format: int64
        description: A unique value identifying this book.
        required: true
      tags:
      - books
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'
          description: ''
    put:
      operationId: books_update
      parameters:
      - in: path
        name: id
        schema:
          type: integer
          maximum: 9223372036854775807
          minimum: -9223372036854775808
          format: int64
        description: A unique value identifying this book.
        required: true
      tags:
      - books
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Book'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Book'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Book'
        required: true
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'
          description: ''
    patch:
      operationId: books_partial_update
      parameters:
      - in: path
        name: id
        schema:
          type: integer
          maximum: 9223372036854775807
          minimum: -9223372036854775808
          format: int64
        description: A unique value identifying this book.
        required: true
      tags:
      - books
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PatchedBook'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/PatchedBook'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/PatchedBook'
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'
          description: ''
    delete:
      operationId: books_destroy
      parameters:
      - in: path
        name: id
        schema:
          type: integer
          maximum: 9223372036854775807
          minimum: -9223372036854775808
          format: int64
        description: A unique value identifying this book.
        required: true
      tags:
      - books
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '204':
          description: No response body
components:
  schemas:
    Book:
      type: object
      properties:
        name:
          type: string
          maxLength: 255
      required:
      - name
    PatchedBook:
      type: object
      properties:
        name:
          type: string
          maxLength: 255
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
    cookieAuth:
      type: apiKey
      in: cookie
      name: sessionid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant