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

Implement getSbom using a generated api client. #121

Merged
merged 2 commits into from
Aug 6, 2024

Conversation

chirino
Copy link
Contributor

@chirino chirino commented Jul 22, 2024

The generated code lived in to the client/src/app/client. The goal should be to replace all the api calls with client calls.

The client/src/app/client-config deals with the axios integration similar to axios-config, right now we can't handle 401 retries as our existing client does it. Need to think harder about how to implement similar functionality.

@gildub
Copy link
Contributor

gildub commented Jul 23, 2024

@chirino, having the client code automatically generated could effectively replace the REST code which is quite repeatable is obviously a good idea.

I have the following reservations at this stage :

  • What's the benefice of adding 7,677 lines of extra code ?
  • The Hey Api (openapi-ts) framework seems to be a bit young at this stage, React Query support is announced but not yet ready
  • Experience from generated code tend to show that we need to have generated code clearly located in the source tree

Regarding the first point, I think we need to see the use of such approach as a replacement layer.
Basically to see the benefit between the two approaches as mentioned in #119, we need to be able to "swap" the existing code by replacing REST and Tanstack React query "layers" with the generated solution. Of course we'd have to take in consideration the future additions of REST queries made available by the back-end bending the advantage towards the generated approach (comparatively non linear).

@chirino
Copy link
Contributor Author

chirino commented Jul 23, 2024

The benefits of the extra lines of code are:

  • you get consistent API implementation.
  • you don't need to manually maintain your API clients, (less work!)
  • any new API endpoints developed on the server is automatically exposed to you with types on the client
  • you can help improve the openapi definition since you can provide the server team feedback if the API looks ugly, which helps improve the API for other end users that want to use an API generated from the openapi spec.

Wonder what additional React Query support they want to add as this PR shows that the two integrate fine.

@chirino
Copy link
Contributor Author

chirino commented Jul 24, 2024

Updated PR to use new axios client. Seems to integrate better with the existing axios-config logic.

@chirino
Copy link
Contributor Author

chirino commented Jul 24, 2024

this PR depends on hey-api/openapi-ts#811 getting merged.

@chirino chirino force-pushed the generated-api-client branch 2 times, most recently from 5ac1911 to 5b7ac42 Compare July 25, 2024 02:08
@chirino
Copy link
Contributor Author

chirino commented Jul 25, 2024

Update to new version of @hey-api/client-axios seems to work much better with our APIs.

@chirino chirino force-pushed the generated-api-client branch 5 times, most recently from b1e65ca to 83d26b6 Compare July 25, 2024 13:53
@chirino chirino marked this pull request as ready for review July 25, 2024 13:58
@carlosthe19916
Copy link
Member

Thanks @chirino for this PR, it helps a lot to see code and visualize code to have a better understanding.

Some general thoughts about "Api client" and "React Query" as it seems it was mentioned:

Client

React Query

  • ReactQuery is a state management tool (not a rest client), similar to redux but in my opinion better in many ways.
  • Regardless of whether or not we use the generated client or we keep the manually created client, ReactQuery can not be dropped as a rest client does not replace a state management tool; both have different responsibilities and serve different purposes in the UI.

In regards of the auto generated client. I think every solution should solve a problem. And we need to identify a problem.

My personal view about introducing @hey-api/openapi-ts :

At this point, I am afraid of introducing a hard dependency that rather than help would block us.

  • api client: It is actually easy a "no time" demanding task maintaining the API Client as the set of endpoints do not change. E.g. I expect to have /api/v1/advisory & /api/v1/advisory/{id} in the long term, not that it will change in a month; I would even dare to say that I wouldn't expect it to change in a year. So I do not see a problem to be solved here

  • Models: Here is where I think an auto-generated set of models could help. I do see the benefit of it. However:

In summary, although I appreciate the PR I think it should be carefully evaluated. I wouldn't like to accept a contribution and then leave the long term maintainers to deal with a decision not supported by them. I really do not want to sound not welcoming contributions, but we need to be aware that although this seems a simple things it has a significant impact on the daily work of whoever writes big pieces of code daily in this repository.

@chirino
Copy link
Contributor Author

chirino commented Jul 29, 2024

I think you are missing the point... the Trustify server SHOULD have a stable and easy to use openapi definition. .. We are asking the UI team to help us make sure it becomes that and STAYS that way. Yes this might help the UI by not needing to write as much boiler plate code, but the real value is that you guys become our acceptance testers for the APIs.

@bobmcwhirter
Copy link
Contributor

I think 2 problems it'd help solve would be:

  1. Inadvertent bugs created between eye-balling the OpenAPI and implementing the client.
  2. Making a lot of noise when the server breaks the API contract (as per @chirino)

@gildub
Copy link
Contributor

gildub commented Jul 30, 2024

I totally agree the UI cannot and shouldn't be a dependency to any back-end. Quite the contrary, to be able to fulfill its main purpose of offering the best and most efficient UXD, the Web development' stack requires flexibility and we must keep a vision of independent layers.

Of course the UI will catch bugs when the API is broken but it's not its role. No matter what the UI is about UXD.
If the latter case, it just shows non-regression tests are missing on the back-end.

Also we need to keep in mind the GraphQL API support which adds another "layer" where we need to stay flexible.

@carlosthe19916
Copy link
Member

It seems the problem we are trying to address is "how to ensure the API contract is correctly defined".

  • Currently the server is the one who defines the contract. There should be no way for the server to break its OWN contract. If there are tests to be done to avoid discrepancies between the contract and the actual implementation it should be done in the server side.
  • The UI developers are already actively reporting issues related to the REST definitions through gh issues and the internal chat if needed.

The kind of things I continuously raise against the API contract are things related to design not to implementation, like the ones below I created today:

trustify/issues/624 - Filter products by org does not work
trustify/issues/625 - Discrepancies in the abstraction of SBOM-version
trustify/issues/626 - Vulnerabilities without titles
trustify/issues/628 - Request flattening packages within SBOMs

We will always gladly contribute as Acceptance testers to the Contract as we also benefit from it. But steeping our feet on reality, I would say we must focus on API Design (which won't be solved, neither make noise, by pushing an auto-generated API on any client)

* Initial step in implementing trustification#119

The generated code lived in to the `client/src/app/client`. 
The goal should be to replace all the api calls with client calls.

Added `npm run generate` and `npm run prettier` sub commands.

Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
@chirino
Copy link
Contributor Author

chirino commented Aug 5, 2024

Adding this increases the client/dist/js dir by 15k.

@chirino
Copy link
Contributor Author

chirino commented Aug 5, 2024

Reformated PR with prettier

@chirino chirino closed this Aug 5, 2024
@chirino chirino reopened this Aug 5, 2024
Copy link
Member

@carlosthe19916 carlosthe19916 left a comment

Choose a reason for hiding this comment

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

@chirino I discovered a thing that need to be addressed. See my comment in the code itself please.

Comment on lines 42 to 43
queryFn: async () =>
id === undefined ? undefined : (await getSbom({ path: { id } })).data,
Copy link
Member

Choose a reason for hiding this comment

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

@chirino I've been doing further tests. I found a problem I would really like to be addressed:

  • The Axios errors are gone.

How to reproduce:

  • Open the file client/src/app/pages/sbom-details and add this code to print the error in the code so you can it what I mean:
useEffect(() => {
  if (fetchError) {
    console.log(fetchError);
  }
}, [fetchError]);
  • Start the server using npm run start:dev
  • Open your browser in a non existent SBOM. E.g. open your browser at http://localhost:3000/sboms/anything
  • Open your browser dev tools and you will see something like the image below:

image

So what is the problem?

  • Due to this change, the auto retry of ReactQuery stopped working
  • The Actual error returned from the server is gone.

This is how it looks with the original code and without the autogenerated code:

image

  • Notice that the feature of auto retry of ReactQuery works, you see 3 GET errors in the console
  • The original Axios error generated by the server is still there

Keeping the errors is crucial and I would be against losing them. Is there some fix you could apply please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will look into this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be fixed now. The error was not getting thrown. Had to enable an option to get the desired behavior.

…deal with them as expected.

Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
Copy link
Member

@carlosthe19916 carlosthe19916 left a comment

Choose a reason for hiding this comment

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

Thanks for the latest fix @chirino . Let's see how we can incrementally use these endpoints/models and what we find on that journey.

@carlosthe19916 carlosthe19916 merged commit 13a3e2e into trustification:main Aug 6, 2024
1 check passed
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.

None yet

4 participants