Giter Club home page Giter Club logo

Comments (3)

koxudaxi avatar koxudaxi commented on June 4, 2024

@GC-Elia

Being able to access pydantic v2 model properties directly such as model.item_id and not model.root.item_id.

How do we access to root attribute without root attribute?
I think RootModel provides a root attribute to access the content.

RootModel is the feature of pydantic. datamodel-code-generator is not owened.
https://docs.pydantic.dev/latest/concepts/models/#rootmodel-and-custom-root-types

You may want Type Adapter to parse the object.
https://docs.pydantic.dev/latest/concepts/type_adapter/

...

InventoryAdapter = TypeAdapter(Union[InventoryItem1, InventoryItem2])
print(InventoryAdapter.validate_python({'item-id': '123'}).item_id)
# 123

from datamodel-code-generator.

GC-Elia avatar GC-Elia commented on June 4, 2024

Hi @koxudaxi,
This change seem to have been introduced in #1682 as previously oneOf generated both fields as Optional, albeit it was not enforcing the oneOf requirement, direct field access was supported (model.item_id).
Other than breaking behavior, I believe it also introduces inconsistency in the generated models where some support direct field access and others not.

Meanwhile I've modified RootModel.jinja2 (simple addition as seen below) to support direct field access, and I wonder if this is something that may be added by default to retain previous behavior, or if you see any downside with this approach.

    def __getattr__(self, attr: str):
        return getattr(self.root, attr)

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on June 4, 2024

@GC-Elia

This change seem to have been introduced in #1682 as previously oneOf generated both fields as Optional,

I'm sorry. The implementation at this time was completely wrong. It does not follow the correct oneof definition because all the attributes of multiple models are implemented as optional.

Other than breaking behavior, I believe it also introduces inconsistency in the generated models where some support direct field access and others not.

I agree with you. It is true that breaking change should be avoided as much as possible, but due to Python and Pydantic restrictions on rootModel and .root attributes, it would be difficult to reproduce the exact same structure as OpenAPI.

Meanwhile I've modified RootModel.jinja2 (simple addition as seen below) to support direct field access, and I wonder if this is something that may be added by default to retain previous behavior, or if you see any downside with this approach.

Doesn't this hack completely destroy the type-checking mechanism?

I believe it also introduces inconsistency in the generated models where some support direct field access and others not.

It is indeed inconsistent from this point of view, but from the type-checking point of view, it can check if the root model definitely exists, and type checking also shows that there is a union set up within it.

Long story short, it means that you should run the IDE type-chekcing or mypy before you run the code.

from datamodel-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.