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

orm.factory _get_schema_v2 always returns None in my env #13

Open
sebcampos opened this issue Oct 26, 2024 · 1 comment
Open

orm.factory _get_schema_v2 always returns None in my env #13

sebcampos opened this issue Oct 26, 2024 · 1 comment

Comments

@sebcampos
Copy link
Contributor

sebcampos commented Oct 26, 2024

running Debian GNU/Linux on a google cloud instance.
Python version: Python 3.13.0+

I was using django-ninja-extra which allows to create a model schema by leveraging ninja-schema in the following way:

@api_controller("/pypi")
class PythonPackageModelController(ModelControllerBase):
    model_config = ModelConfig(
            model=PythonPackage,
            schema_config=ModelSchemaConfig(read_only_fields=["id"]),
            async_routes=True,
            pagination=ModelPagination(
                klass=LimitOffsetPagination,
                pagination_schema=NinjaPaginationResponseSchema
            )

But ninja schema kept throwing and error in which it was trying to get the name attribute from a NoneType.

Upon looking into the form/factory.py file I saw that the code was eventually calling this line:

        new_schema = (
            cls._get_schema_v1(name, model_config_kwargs, ModelSchema)
            if IS_PYDANTIC_V1
            else cls._get_schema_v2(name, model_config_kwargs, ModelSchema)
        )

In my case cls._get_schema_v2 was returning None.

I added a breakpoint at cls._get_schema_v2:

    def _get_schema_v2(
        cls, name: str, model_config_kwargs: typing.Dict, model_type: typing.Type
    ) -> Union[Type["ModelSchema"], Type["Schema"]]:
        model_config = cls.get_model_config(**model_config_kwargs)
        new_schema_string = f"""class {name}(model_type):
            class Config(model_config):
                pass """
        exec(new_schema_string, locals())
        breakpoint()
        return locals().get(name)

I noticed that the exec statement was not storing the new object it create in locals, therefore locals().get(name)
always returned None.

But with this change:

    def _get_schema_v2(
       cls, name: str, model_config_kwargs: typing.Dict, model_type: typing.Type
   ) -> Union[Type["ModelSchema"], Type["Schema"]]:
       model_config = cls.get_model_config(**model_config_kwargs)
       res = {}
       new_schema_string = f"""class {name}(model_type):
           class Config(model_config):
               pass """
       exec(new_schema_string, locals(), res)
       return res.get(name)  # type:ignore[return-value]

I am getting the expected result
Screenshot 2024-10-26 at 2 27 29 PM

Original code with breakpoint:
Screenshot 2024-10-26 at 2 30 12 PM

after change:
Screenshot 2024-10-26 at 2 32 35 PM

@eadwinCode
Copy link
Owner

@sebcampos I have created a new release, can you verify if this is resolved now?

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

2 participants