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-fastapi] Added a base class for the actual implementation #14470

Merged
merged 1 commit into from
May 26, 2023

Conversation

krjakbrjak
Copy link
Contributor

@krjakbrjak krjakbrjak commented Jan 16, 2023

It is very difficult to "merge" the changes, made by code generation, and the changes, made by developers. It would be very useful to separate the generated code and the code written by developers. In addition this would remove the necessity to track the generated code. Pyhton (since 3.6) has a hook, init_subclasses, that could be used to solve exactly this problem.
The classes from *_base.py should be implemented in an ns package that is specified by the additional parameter ("-p fastapiImplementationPackage=example_name").

Signed-off-by: Nikita Vakula programmistov.programmist@gmail.com

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.
  • 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.

@spacether

It is very difficult to "merge" the changes, made by code generation,
and the changes, made by developers. It would be very useful to
separate the generated code and the code written by developers. In
addition this would remove the necessity to track the generated code.
Pyhton (since 3.6) has a hook, __init_subclasses__, that could be used
to solve exactly this problem.
The classes from *_base.py should be implemented in an ns package that is specified
by the additional parameter ("-p fastapiImplementationPackage=example_name").

Signed-off-by: Nikita Vakula <programmistov.programmist@gmail.com>
@krjakbrjak
Copy link
Contributor Author

@spacether this PR is more about separating the generated code from the logic. I think it would be very convenient not to store the generated code in git.

@wing328 wing328 added this to the 7.0.0 milestone Feb 2, 2023
@wing328 wing328 changed the base branch from master to 7.0.x February 2, 2023 10:23
@wing328 wing328 changed the base branch from 7.0.x to master May 16, 2023 07:29
@wing328 wing328 merged commit 3db7169 into OpenAPITools:master May 26, 2023
@sefasenturk95
Copy link

@krjakbrjak Where can we find a good example on how this works in practice?

@sbarber-dsi
Copy link

sbarber-dsi commented Nov 10, 2023

Hello, sorry to say but these changes make it substantially harder to integrate not easier.

Please forgive me if this is over-bearing but these are the requirements of all generators for them to be usable (with details specific to the python-fastapi generator):

  1. Build a library
    Stop generating an entire project
    You may generate an example project but it shall not become part of the library; it may be 'next' to the library module and can include the generated library code directly.
    A build shall yield a whl file to provide the model classes and API skeleton usable by multiple end projects.

  2. The integrator (a person) shall import the generated library
    Do not use importlib
    Remove the architecture-breaking fastapiImplementationPackage variable.
    At the code level, this reaches-back and imports stuff above it. That's a black flag violation; you are ejected from the game.
    At the design level this injects a concrete implementation detail into the interface. This is unacceptable.
    When this is implemented appropriately you will be able to generate the SDK code once and create multiple projects from it.
    This is the power of OpenAPI as you can define APIs, bundle them together, and then have different parts running on different things then pull it all back together and present a single interface to the outside world. One component might be using a python-server and another might be using a C++ server et. al. One component might be a facade that pulls it all together.

  3. Verify it's a duck
    The generated stub implementations shall yield a "501 Not Implemented" error response.
    The generated test-cases shall verify every end-point returns a response (no 501s).
    i.e. Generated code as-is will fail all test-cases.
    The integrator shall add end-points to their @app. People write custom decorators to ensure consistency in responses so that functionality/methodology cannot be broken.
    While other generators/languages offer override and signals/slots this is python so duck-typing it is and we rely on test-cases to catch missing overrides.

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.

None yet

4 participants