Skip to content

POC: Generating OpenAPI client #1213

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

winniehell
Copy link

@winniehell winniehell commented Jun 3, 2025

This is a proof of concept for replacing the existing request logic in the frontend with a client implementation that is automatically generated from the OpenAPI specification. It is not meant to be merged but only serves to demonstrate the concept.

Advantages

  • auto completion can be used for endpoints in the frontend
  • static code analysis can find typos in request URLs
  • typos in request parameters are prevented
  • static code analysis can find endpoints that are still used in the frontend but removed / replaced in the backend
  • naming consistency for endpoints is ensured between Python and JavaScript
  • stubs / interfaces for complex response data

Disadvantages

  • client code needs to added to the repository or always generated before starting the frontend
  • new Python dependency
  • manual modifications / overrides in the request logic is more difficult

@@ -173,6 +174,11 @@ async def update_user_setting(setting: UserSetting, user: User = Depends(authent
from ee.api import ai_optimisations
app.include_router(ai_optimisations.router)

for route in app.routes:
Copy link
Author

Choose a reason for hiding this comment

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

see c9e1aed

@@ -77,7 +79,8 @@ const fetchAndFillRunData = async (url_params) => {
let run = null;

try {
run = await makeAPICall('/v2/run/' + url_params['id'])
const api = new DefaultApi();
run = await api.getRun(url_params['id']);
Copy link
Author

Choose a reason for hiding this comment

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

this is the relevant improvement. when using TypeScript, there could even be validation for parameters and responses on the client side


openapi-generator-cli generate \
--generator-name javascript \
--input-spec http://127.0.0.1:8000/openapi.json \
Copy link
Author

Choose a reason for hiding this comment

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

instead of using the backend port, a local file can be used

@ArneTR
Copy link
Member

ArneTR commented Jun 4, 2025

This PR feels a bit chaotic and I am trying to understand what the purpose is:

  • Description missing
  • Is it a generator for OpenAI? Or is the generator bash script just linked for compeleteness and was initally used?
  • This seems like a lot of boilerplate just to query the simple API. Can it not be used with a classical external tool instead of carrying everything inside the repository?

@winniehell
Copy link
Author

Description missing

sorry about that! it was not meant to be a standalone changeset but only an additional proof of concept to highlight what we had talked about. I've added a proper description now.

Is it a generator for OpenAI? Or is the generator bash script just linked for compeleteness and was initally used?

it uses a library (https://openapi-generator.tech/) to generate code from the specification provided by FastAPI. the bash script is just for documenting the command line flags that I have used. most of them could be added to a config file. whether or not to keep a script as a shortcut for the generator command is a matter of taste.

the command (or script) needs to be run whenever the backend API changes.

This seems like a lot of boilerplate just to query the simple API.

that is true and most of it is due to the fact that OpenAPI is strictly typed while JavaScript is not. there is much less boilerplate when using TypeScript (but that would be a separate discussion). some of the boilerplate can be avoided by fine tuning the config and there is also an open feature request to skip client side validation and rely on server side validation instead: OpenAPITools/openapi-generator#4168

Can it not be used with a classical external tool instead of carrying everything inside the repository?

I think I have answered most of the question above. whether to add the (redundant) client code to the repository or not is a highly subjective matter. personally I prefer to do so because then you can search in it or run git blame without navigating to the backend (essentially avoiding context switches).

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

Successfully merging this pull request may close these issues.

2 participants