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

[PYTHON] generate code based on pydantic v2 #16685

Merged
merged 14 commits into from
Sep 29, 2023

Conversation

fa0311
Copy link
Contributor

@fa0311 fa0311 commented Sep 29, 2023

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@fa0311 fa0311 marked this pull request as ready for review September 29, 2023 04:44
@wing328 wing328 added this to the 7.1.0 milestone Sep 29, 2023
Copy link
Contributor

@multani multani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the tests in samples/openapi3/client/petstore/python-aiohttp goes from:

148 passed, 2 skipped, 2100 warnings

to:

148 passed, 2 skipped, 60 warnings

and I don't see any Pydantic v2 warnings anymore 👏

@wing328
Copy link
Member

wing328 commented Sep 29, 2023

FYI @taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @arun-nalla (2019/11) @krjakbrjak (2023/02)

@wing328
Copy link
Member

wing328 commented Sep 29, 2023

tested locally and all those pydantic warnings are gone 👍

@wing328 wing328 merged commit e2f249b into OpenAPITools:master Sep 29, 2023
31 checks passed
@jakubno
Copy link

jakubno commented Sep 29, 2023

I am receiving TypeError: argument of type 'ModelPrivateAttr' is not iterable

It's caused by

        # raise errors for additional fields in the input
        for _key in obj.keys():
>           if _key not in cls.__properties:
E           TypeError: argument of type 'ModelPrivateAttr' is not iterable

Whole snippet

    __properties = ["sessionID"]

    @classmethod
    def from_dict(cls, obj: dict) -> Session:
        """Create an instance of Session from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return Session.model_validate(obj)

        # raise errors for additional fields in the input
        for _key in obj.keys():
            if _key not in cls.__properties:
                raise ValueError(
                    "Error due to additional fields (not defined in Session) in the input: "
                    + obj
                )

        _obj = Session.model_validate(
            {
                "sessionID": obj.get("sessionID"),
            }
        )
        return _obj

It worked in #16655

@multani
Copy link
Contributor

multani commented Sep 29, 2023

I am receiving TypeError: argument of type 'ModelPrivateAttr' is not iterable

It's caused by

        # raise errors for additional fields in the input
        for _key in obj.keys():
>           if _key not in cls.__properties:
E           TypeError: argument of type 'ModelPrivateAttr' is not iterable

@jakubno do you have an OpenAPI example where this happens?

This was changed in #16624 but I think we missed a branch that was not covered by the tests.

@jakubno
Copy link

jakubno commented Sep 29, 2023

I will create minimal example and create issue

@multani
Copy link
Contributor

multani commented Sep 29, 2023

I will create minimal example and create issue

Great, thanks 👍

If that helps, it seems it has something to do with "additional properties": this is the part in the code generator that may not be covered by the tests.

@fa0311
Copy link
Contributor Author

fa0311 commented Sep 29, 2023

Perhaps cls.__properties should be replaced with cls.__properties.default

@multani
Copy link
Contributor

multani commented Sep 29, 2023

Yep, that's what I did in the branch that was covered by the tests 👍

@fa0311
Copy link
Contributor Author

fa0311 commented Sep 29, 2023

If you test this, you need to set the disallowAdditionalPropertiesIfNotPresent to true.
https://github.com/fa0311/openapi-generator/blob/e2f249ba35e2da2f9936efaf5434068238cfc68c/bin/configs/python.yaml#L8

AlanCitrix pushed a commit to AlanCitrix/openapi-generator that referenced this pull request Oct 26, 2023
* [python] replace validator with field_validator

* [python] replace parse_obj with model_validate

* [python] replace dict with model_dump

* [python] replace construct with model_construct

* [python] replace __fields_set__ with model_fields_set

* [python] replace __fields_set__ in the test cases with model_fields_set

* [python] replace validate_arguments with validate_call

* [python] replace max_items, min_items with max_length, min_length

* [python] replace Config class with model_config

* [python] replace allow_population_by_field_name with populate_by_name

* [python] remove {{{classname}}}_ONE_OF_SCHEMAS

* [python] update test cases

* [python] update samples

* [python] fix typos in test cases
@wing328
Copy link
Member

wing328 commented Nov 13, 2023

NOTE: for users who still prefer using pydantic v1 in v7.1.0 release, they can use the python-pydantic-v1 client generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants