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

[BUG] Resolvers called even when schemas are created via __init__ #1398

Open
crbunney opened this issue Jan 23, 2025 · 0 comments
Open

[BUG] Resolvers called even when schemas are created via __init__ #1398

crbunney opened this issue Jan 23, 2025 · 0 comments

Comments

@crbunney
Copy link

crbunney commented Jan 23, 2025

Pydantic allows you to create model instances by explicitly passing values directly to the model via the __init__ method (see Basic model usage).

Generally speaking, the same can be done with ninja, except when the Schema defines resolvers, which run even though an explicit value has already been passed to the instance, and cause a ValidationError.

In my case, I want to manually create instances so that I can easily write unit tests for the schema without having to create Django model instances that are costly and complex to setup.

Seems like it's a similar issue, or even the same issue, as reported in #871

How to reproduce
Run the test code using pytest:

import django.db.models
from django.db.models import JSONField
from ninja import Schema


class SimplifiedExample(django.db.models.Model):
    json_data = JSONField()

    def __getitem__(self, item: str) -> Any:
        return self.json_data[item]


class SimplifiedExampleSchema(Schema):
    foo: str
    bar: str

    @staticmethod
    def resolve_foo(obj: SimplifiedExample) -> str:
        return obj["foo"]

    @staticmethod
    def resolve_bar(obj: SimplifiedExample) -> str:
        return obj["bar"]


class TestSimplifiedExampleSchema:
    def test_example(self):
        SimplifiedExampleSchema(foo="foo", bar="bar")

Expected Result
The SimplifiedExampleSchema instance is created without error

Actual Result
A ValidationError is raised & pytest produces output similar to this:

E       pydantic_core._pydantic_core.ValidationError: 2 validation errors for SimplifiedExampleSchema
E       foo
E         Error extracting attribute: KeyError: 'foo' [type=get_attribute_error, input_value=<DjangoGetter: {'foo...bar': 'bar'}>, input_type=DjangoGetter]
E           For further information visit https://errors.pydantic.dev/2.10/v/get_attribute_error
E       bar
E         Error extracting attribute: KeyError: 'bar' [type=get_attribute_error, input_value=<DjangoGetter: {'foo...bar': 'bar'}>, input_type=DjangoGetter]
E           For further information visit https://errors.pydantic.dev/2.10/v/get_attribute_error

Versions:

  • Python version: 3.10.16
  • Django version: 4.2.17
  • Django-Ninja version: 1.3.0
  • Pydantic version: 2.10.3
@crbunney crbunney changed the title [BUG] Resolvers called even when models are created via __init__ [BUG] Resolvers called even when schemas are created via __init__ Jan 23, 2025
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