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

added function in openapi3.operation #370

Merged
merged 20 commits into from
Jan 29, 2025

Conversation

TheRanomial
Copy link
Contributor

@TheRanomial TheRanomial commented Jan 22, 2025

Let me explain the purpose of the parameter registration code I added. It handles automatic OpenAPI3 documentation for strongly-typed request parameters.

Consider this example:
type ListBooksParams struct {
APIKey string header:"API-Key"
PageSize *int query:"page_size"
SessionID string cookie:"my-cookie"
}

The parameter registration code I added will:
*Read the struct tags from ListBooksParams
*Create OpenAPI parameter definitions for each tagged field:

This means that when someone looks at your API documentation, they'll see exactly what parameters are expected, where they should be placed (header/query/cookie), and their types - all generated automatically from your Go code's type definitions.
Without this code, you'd have to manually document these parameters in your OpenAPI spec, which could get out of sync with your actual code.

Copy link
Member

@EwenQuim EwenQuim 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 PR! 🥳

Please fix formatting and add some tests to check that it works as expected.

documentation/docs/guides/controllers.md Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

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

We still need some tests

documentation/docs/guides/controllers.md Outdated Show resolved Hide resolved
documentation/docs/guides/controllers.md Outdated Show resolved Hide resolved
Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

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

Better, thank you!

But you've made weird choices for testing. Please test your feature, not an helper function! This does not make sense, is there any reason?

openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
register_parameter_test.go Outdated Show resolved Hide resolved
@TheRanomial
Copy link
Contributor Author

Hii @EwenQuim sorry for the mistakes i did previously. I have made some changes,Can you have a look at them.

Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

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

Please, when submitting a PR, re-read yourself. There are a lot of things you could have seen yourself.

When submitting a PR, you can click on the "Files changed" tab and review your changes. There, you'll see that there are a lot of changes that are useless, typos or mistakes. Reviewing yourself before asking for a maintainer to review is something usual in CS, don't hesitate to get used to it :)

image

I hope you'll understand!

ctx.go Outdated Show resolved Hide resolved
examples/petstore/lib/server_test.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
@TheRanomial
Copy link
Contributor Author

Hii @EwenQuim I am extremely sorry because make lint is not working in in laptop so i am not aware of the linting errors if they are present.That is why i reduced the newlines as they were showing me lint error in the actions.

@EwenQuim
Copy link
Member

No problem! What is not working when you run make ? Can you send us a screenshot so we can debug it for you (and maybe others)?

Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

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

Looks very nice! Just a few comments but we're almost there! Thank you for your great work! 🥳

examples/petstore/lib/testdata/doc/openapi.golden.json Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated
Comment on lines 231 to 234
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists != nil {
continue
}
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
if strings.HasSuffix(pathParam, "...") {
parameter.Description += " (might contain slashes)"
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists == nil {
if strings.Contains(route.Path, "{"+pathParam+"}") {
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
route.Operation.AddParameter(parameter)
}
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change it? Did you spot any bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One of my test was failing so i had to modify it.

Copy link
Member

Choose a reason for hiding this comment

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

Which one?

Copy link
Member

Choose a reason for hiding this comment

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

Why resolved???

@EwenQuim
Copy link
Member

I've removed some useless changes and used OptionXxx instead of redefining the OpenAPI logic (so we have a unified parameter declaration).

Thanks a lot @TheRanomial for setting up the structure of all this, this was very useful! Don't hesitate to look at my commits and the current PR.

@dylanhitt what do you think about it?

Copy link
Collaborator

@dylanhitt dylanhitt left a comment

Choose a reason for hiding this comment

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

Yes this looks great. Not sure what you want to do about that linting issue though.

examples/petstore/lib/testdata/doc/openapi.golden.json Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
openapi.go Outdated
Comment on lines 231 to 234
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists != nil {
continue
}
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
if strings.HasSuffix(pathParam, "...") {
parameter.Description += " (might contain slashes)"
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists == nil {
if strings.Contains(route.Path, "{"+pathParam+"}") {
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
route.Operation.AddParameter(parameter)
}
Copy link
Member

Choose a reason for hiding this comment

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

Which one?

openapi.go Outdated
Comment on lines 231 to 234
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists != nil {
continue
}
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
if strings.HasSuffix(pathParam, "...") {
parameter.Description += " (might contain slashes)"
if exists := route.Operation.Parameters.GetByInAndName("path", pathParam); exists == nil {
if strings.Contains(route.Path, "{"+pathParam+"}") {
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
route.Operation.AddParameter(parameter)
}
Copy link
Member

Choose a reason for hiding this comment

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

Why resolved???

@EwenQuim EwenQuim merged commit 59cbb1a into go-fuego:main Jan 29, 2025
4 of 5 checks passed
@EwenQuim
Copy link
Member

Thanks @TheRanomial for this PR! 🎉

Happy to count you in our contributors

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.

3 participants