Skip to content

Better pydantic support in python fast api server#16321

Closed
wing328 wants to merge 2 commits intomasterfrom
python-fastapi-pydantic
Closed

Better pydantic support in python fast api server#16321
wing328 wants to merge 2 commits intomasterfrom
python-fastapi-pydantic

Conversation

@wing328
Copy link
Copy Markdown
Member

@wing328 wing328 commented Aug 14, 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/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.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.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.

@wing328
Copy link
Copy Markdown
Member Author

wing328 commented Aug 16, 2023

To test the branch, simply run the following:

git clone http://github.com/openapitools/openapi-generator -b python-fastapi-pydantic
cd openapi-generator
mvn clean package -DskipTests -Dmaven.javadoc.skip=true
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g python-fastapi -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/python-fastapi/

@wing328
Copy link
Copy Markdown
Member Author

wing328 commented Aug 25, 2023

We'll consider this PR as a bug fix (non breaking change) to the upcoming 7.x release.

@yahiaosama
Copy link
Copy Markdown

We'll consider this PR as a bug fix (non breaking change) to the upcoming 7.x release.

Is there a timeline of when this is planned to be released?

@yahiaosama
Copy link
Copy Markdown

@wing328 I have tried and tested if python-nextgen plays well with fastAPI and if it really fixes some of the issues that were never fixed for python-fastapi like the support for enum and oneOf schemas.

I can gladly say that it does fix the enum problem. However, for the oneOf schemas I had to implement a decorator so that the generated pydantic models would be compatible with how fastAPI serializes the request payload into pydantic models. I hope that this PR considers that. As from what I have seen is the template model_oneof.mustache is the same as the from python-nextgen which didn't work out of the box for fastAPI.

I haven't had the time to test this branch yet but I hope that things like that is under your scope during implmentation.

@wing328
Copy link
Copy Markdown
Member Author

wing328 commented Sep 27, 2023

@yahiaosama thanks for reviewing the testing this change.

Can you please PM me via Slack for a few quick questions if you've time?

https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g

@jonasheschl
Copy link
Copy Markdown
Contributor

jonasheschl commented Nov 14, 2023

These changes generate incorrect code for my usecase.

TextTopic:
  description: foobar
  enum:
  - foo
  - bar
  title: TextTopic
  type: string

Turns into:

class TextTopic(int, Enum):
    """
    foobar
    """

    """
    allowed enum values
    """
    NUMBER_'foo' = 'foo'
    NUMBER_'bar' = 'bar'

    @classmethod
    def from_json(cls, json_str: str) -> TextTopic:
        """Create an instance of TextTopic from a JSON string"""
        return TextTopic(json.loads(json_str))

The same code generates correctly using #14242.

@wing328
Copy link
Copy Markdown
Member Author

wing328 commented Dec 11, 2023

I've filed #17369 instead to support pydantic v2 in the python-fastapi generator.

please review and test that one instead.

closing this one.

@wing328 wing328 closed this Dec 11, 2023
@wing328 wing328 deleted the python-fastapi-pydantic branch December 11, 2023 04:48
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.

3 participants