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

Pydantic 1.8.0 breaks use of custom __root__ in models #381

Closed
butler54 opened this issue Mar 2, 2021 · 7 comments
Closed

Pydantic 1.8.0 breaks use of custom __root__ in models #381

butler54 opened this issue Mar 2, 2021 · 7 comments
Assignees

Comments

@butler54
Copy link
Collaborator

butler54 commented Mar 2, 2021

Describe the bug
Currently we use custom root models in situations where we have been unable to easily collapse the schema with fix_any.py

This tends to occur for us in situations where lists occur such as:

The result is a model such as this:

class LowerModel:
      __root__: str = Field(...,name='stuff')

class ATopLevelModel(OscalBaseModel):
      field: List[LowerModel]

Would result in an error when trying to create ATopLevelModel. See issue here:pydantic/pydantic#2449

It looks like this issue will be closed soon which will allow the removal of a hot fix.

However, a longer term question is why do we have the root models.

we should be able to fix this by removing our use of root. This would be a more appropriate long term solution.

To Reproduce
Force install pydantic==1.8.0
run make test
Observe errors

Expected behavior
No errors on make test.

@koxudaxi
Copy link

koxudaxi commented Mar 3, 2021

Hi, @butler54
I'm an author of datamodel-code-generator.
Thank you for using the code-generator in this project.
I have understood you use fix_any.py for fixing generated models.

Can I help you by fixing the code-generator?
I'm working on improving the code-generator.

However, a longer term question is why do we have the root models.

I guess LowerModel has some properties like name (usually maybe description or example )
So, the code-generator decide to need the __root__ model for the field.

I can implement an option to strip the un-required __root__ model on the code-generator.

Thank you

@fsuits
Copy link
Collaborator

fsuits commented Mar 4, 2021 via email

@koxudaxi
Copy link

koxudaxi commented Mar 4, 2021

@fsuits
Thank you for your response.

It would be great if the root classes could be absorbed into the
classes that reference them because they serve no purpose as standalone
classes.

I will add an option when I have the time.

There are still some issues I need to handle and I will aim to send a follow up note
shortly to summarize the remaining issues I deal with.

I look forward to the document.
Thank you for your cooperation.

Also, Would you please tell me how to reproduce your code generation?
I want to try it in my environment.

@fsuits
Copy link
Collaborator

fsuits commented Mar 5, 2021 via email

@koxudaxi
Copy link

koxudaxi commented Mar 5, 2021

Thank you for writing the document. I'm checking it.

I'm not sure if the issue related to Part and = None is due to pydantic or
datamodel-codegen - but I do need to make the change shown.

I deep-dived into pydantic source.
Pydantic blocks constraint conditions on self-reference class.
Because pydantic can't resolve infinite recursion.
Some day, the problem may be resolved.
But, the code-generator must not create an invalid model.
I will change that the code-generator ignores constraint conditions for the self-reference class.

  • Certain items in lists have 'Item' appended to the name, which can be confusing because some items in the schema do in fact end with 'Item'. As a result the script removes 'Item' in the name except when it is expected.

I will add an option that doesn't add an Item to a singular object.

  • Finally, in order to guarantee there are no induced forward references in the files, the classes are reordered to minimize the need for forwards, and any that can't be avoided are explicitly provided at the bottom of the file.

I think a good idea to sort the model. I will add an option for the feature too.

@fsuits
Copy link
Collaborator

fsuits commented Mar 6, 2021 via email

@koxudaxi
Copy link

koxudaxi commented Mar 8, 2021

@fsuits
Thank you for explaining the sorting.
I guess the feature is not required now. I don't implement it. because you have already the script.

I have released a new version 0.9.1.

I fixed the problem.

I will change that the code-generator ignores constraint conditions for the self-reference class.

And, I suggest two options '--disable-timestamp', '--disable-appending-item-suffix'
I tested these options with 0.9.1.
It works file.

Thank you.

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

3 participants