Skip to content

Regression when serializing Field(alias=...) #41

Description

@dzonecat

Serializing a schema that uses Field(alias=...) worked up to 0.13.6 but fails beginning with 0.14.0.

Reproducer:

models.py

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)

schemas.py

from ninja import Field, ModelSchema
from ninja_schema import Schema

from .models import Choice


class ChoiceSchema(ModelSchema):
    question_text: str = Field(alias="question.question_text")

    class Meta:
        model = Choice
        fields = ["choice_text"]


class ParentSchema(Schema):
    choice: ChoiceSchema

Note that I import ModelSchema from the "wrong" module. Importing it from ninja_schema makes it fail even with 0.13.6.

api.py

from ninja_extra import NinjaExtraAPI, api_controller, http_get

from .models import Choice
from .schemas import ParentSchema

api = NinjaExtraAPI()


@api_controller("/test", tags=["test"])
class TestController:
    @http_get("/test")
    def test(self) -> ParentSchema:
        return ParentSchema(choice=Choice.objects.get())


api.register_controllers(TestController)

Create one Question object and one Choice that references it.

Response up to 0.13.6

{
  "choice": {
    "question_text": "Question",
    "choice_text": "Choice"
  }
}

Response after 0.14.0

Traceback (most recent call last):
  File "/home/test/.pyenv/versions/3.13.4/lib/python3.13/site-packages/ninja_extra/operation.py", line 216, in run
    _processed_results = self._result_to_response(
        request, result, ctx.response
    )
  File "/home/test/.pyenv/versions/3.13.4/lib/python3.13/site-packages/ninja/operation.py", line 280, in _result_to_response
    validated_object = response_model.model_validate(
        resp_object, context={"request": request, "response_status": status}
    )
  File "/home/test/.pyenv/versions/3.13.4/lib/python3.13/site-packages/pydantic/main.py", line 705, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj, strict=strict, from_attributes=from_attributes, context=context, by_alias=by_alias, by_name=by_name
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 1 validation error for NinjaResponseSchema
response.choice.`question.question_text`
  Field required [type=missing, input_value=<DjangoGetter: ChoiceSche..., choice_text='Choice')>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.11/v/missing

By the way, thanks for working on ninja-extra/schema!

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions