Comments (3)
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.
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.
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)
- Property's model name prefix and duplication HOT 1
- Invalid definition of graphql requirement HOT 1
- Enum default value type HOT 1
- pydantic.errors.PydanticUserError: `unique_items` is removed, use `Set` instead HOT 1
- datamodel-codegen tries to download localy defined subschema from web
- generate request body HOT 2
- mixing additionalProperties and required properties doesn't work with pydantic HOT 1
- Option to include `$comment` fields HOT 1
- JSON Schema `const` value and type ignored when setting defaults for Pydantic V2
- typing.Literal import not generated for OpenAPI discriminator + enum
- Nullable fields from openapi not translated into optional fields for TypedDict HOT 3
- Two variations of syntaxes for defining dictionaries/free-form objects give different results HOT 1
- allOf ignores the $ref option if it contains enum HOT 3
- String enum with "YES/NO/NOT_APPLICABLE" being converted to "TRUE/FALSE/'NOT_APPLICABLE'" HOT 1
- Pydantic discriminator generation does not take alias mapping into account. HOT 1
- Allow discriminator field literals to be set to enum values HOT 1
- `--use-title-as-name` does not allow multi-module Python output.
- Dataclass generation does not include specified string format like uuid HOT 5
- Bug with oneOf and const HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datamodel-code-generator.