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

feat: gRPC request stubs #6196

Merged
merged 16 commits into from
Aug 1, 2023
Merged

feat: gRPC request stubs #6196

merged 16 commits into from
Aug 1, 2023

Conversation

nbgraham
Copy link
Contributor

@nbgraham nbgraham commented Jul 25, 2023

changelog(New Features): Added gRPC request body Mock/stub generation for servers with reflection enabled

From discussion: #3475

I need input on the things in bold

Copies code from bloomrpc (is this okay?) to create gRPC method request mocks.

Adds a button to replace the request body with a method mock/stub if available.
I initially tried to set it only when the method changed, if the body was empty, but that was hard to predict as a user and seemed unreliable. Another option is to add a new tab with the stub as static content that the user could manually copy.
Thoughts on which approach to use?

Only works with server reflection!
Current the proto file loader uses @grpc/proto-loader (instead of protobufjs directly) which returns a simple JSON object and so doesn't have access to the protobuf.Service type. I'm not quite sure how to convert to protobuf JS and preserve the options. Or if we should.
I think we want this but I don't think it's exported. Maybe I should just copy it in?
Does this need to support file imports to be merged?

Comment on lines +241 to +260
class StackDepth {
private readonly depths: { [type: string]: number };
readonly maxStackSize: number;

constructor(maxStackSize = 3) {
this.depths = {};
this.maxStackSize = maxStackSize;
}

incrementAndCheckIfOverMax(key: string): boolean {
if (this.depths[key] > this.maxStackSize) {
return true;
}
if (!this.depths[key]) {
this.depths[key] = 0;
}
this.depths[key]++;
return false;
}
}
Copy link
Contributor Author

@nbgraham nbgraham Jul 25, 2023

Choose a reason for hiding this comment

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

I wrapped this in a class and made sure it was passed in everywhere. The original implementation didn't always pass it down.

Comment on lines +171 to +179
function isProtoType(resolvedType: Enum | Type | null): resolvedType is Type {
if (!resolvedType) {
return false;
}
const fieldsArray: keyof Type = 'fieldsArray';
return resolvedType instanceof Type || (
fieldsArray in resolvedType && Array.isArray(resolvedType[fieldsArray])
);
}
Copy link
Contributor Author

@nbgraham nbgraham Jul 25, 2023

Choose a reason for hiding this comment

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

I added this
Not sure why this is needed, but my example proto file did not work with resolvedType instanceof Type even though during debug it definitely seemed to be a Type

@nbgraham
Copy link
Contributor Author

I'm not sure why the build is failing.
It seems like a timeout issue on Smoke tests unrelated to gRPC, so I don't think my PR would affect it.
First the Mac one was failing, and now the Windows one.
I was able to successfully run tests on both my personal Mac and Windows machine.

@nbgraham nbgraham marked this pull request as ready for review July 28, 2023 14:17
@filfreire
Copy link
Member

@nbgraham Mac and Windows are sometimes flaky on GitHub due to the GitHub runners performance being slow. If Ubuntu passes should be fine 👍

Copy link
Contributor

@jackkav jackkav left a comment

Choose a reason for hiding this comment

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

🎸

Copy link
Contributor

@gatzjames gatzjames left a comment

Choose a reason for hiding this comment

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

Awesome! Tested and works great!

Some future work we might want to revisit in the future:

  • Replace the custom auto-mocking class with faker or similar logic which we can use for autogenerating requests for open-api, graphql variables etc

@gatzjames gatzjames merged commit 9776034 into Kong:develop Aug 1, 2023
@nbgraham nbgraham deleted the feat/grpc-stubs branch August 1, 2023 12:05
jackkav added a commit to jackkav/insomnia that referenced this pull request Nov 24, 2023
* feat: grpc request stubs

* mark changed lines

* add tests

* fix infinite recursion

* replace empty body

* catch Type objects

* button to replace stubs

* flatten grpc editor

* remove grpc-editor

* trigger setvalue

* rename mocks to example

---------

Co-authored-by: jackkav <[email protected]>
Co-authored-by: James Gatz <[email protected]>
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.

4 participants