-
-
Notifications
You must be signed in to change notification settings - Fork 926
Add support for Gorilla generation #585
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
Add support for Gorilla generation #585
Conversation
a7334af
to
9a95192
Compare
As part of oapi-codegen#465, it'd be handy to have gorilla/mux, a commonly used HTTP server as a generated server. This is very similar to Chi, as it is also `net/http` compliant, and allows us to mostly copy-paste the code, with very minor tweaks for Gorilla-specific routing needs.
9a95192
to
a13f9aa
Compare
As part of oapi-codegen#465, we should produce an example version of the Petstore API using Gorilla, to validate that this works, as well as showing sample usage to consumers.
Hey @deepmap-marcinr sorry for the nudge, but wondering if there's any chance you'd be able to have a review of this, to see if it fits in with how we'd want to do this? 🙏 |
Oh wow, you did a lot of work here. I'm happy to merge this shortly. Looking the generated code looks pretty reasonable - it's interesting how middleware is done by making a chain of functions that return handlers. |
Thanks! I think that's right, but maybe I should double check. This was mostly followed by using Chi as a base 🤔 |
Having a look through Gorilla's middleware handling code it's similar to Chi's middleware handling code, which both appear to be based on how |
@jamietanna , nice work, this is great, have been testing for the last few days, works well 👏 FYI - I used an earlier commit, on the master branch , to test your stuff, works well. |
Thanks Mick, yep I'm currently using it in production (https://deliveroo.engineering/2022/06/27/openapi-design-first.html) pinned to a commit off HEAD. That's odd, mind sharing the config file you're using to generate with? I pulled HEAD yesterday and it still seemed to generate OK for me 🤔 And yes, they'll be in for the next release ☺ |
Cheers Jamie, it appears to be something going on in the GA pipeline (latest HEAD working locally for me too), I'll sort it :) V Nice blog post BTW, will have a look at some of those tools you mentioned too, your hard work is much appreciated |
Hi @jamietanna, thanks for the great work! I had hacked together something similar but would like to switch to this implementation. A few differences I noticed that may be worth to discuss:
Example of both: status:
type: string
enum: [ success, failed ] // Yours:
type Status = string
const (
Success Status = "success"
Failed Status = "failed"
)
// Mine:
type Status string
const (
StatusSuccess Status = "success"
StatusFailed Status = "failed"
)
func (v *Status) UnmarshalJSON(bs []byte) error {
var s string
if err := json.Unmarshal(bs, &s); err != nil {
return err
}
switch Status(s) {
case StatusSuccess, StatusFailed:
*v = Status(s)
return nil
default:
return fmt.Errorf("invalid enum value %q", s)
}
}
// ------------- Optional query parameter "name" -------------
if paramValue := r.URL.Query().Get("name"); paramValue != "" {
// this is empty
}
// BindQueryParameter
|
Hey, so generally this PR echoes how we've done it in the other servers, and these pieces of feedback may be worth adding as separate issues to get general feedback that we can align all servers with? For 2 - #625 may be worth looking at and raising a PR if possible - it was originally added in #549 to only selectively do it, but makes sense to be all the time. |
* Add support for Gorilla generation As part of oapi-codegen#465, it'd be handy to have gorilla/mux, a commonly used HTTP server as a generated server. This is very similar to Chi, as it is also `net/http` compliant, and allows us to mostly copy-paste the code, with very minor tweaks for Gorilla-specific routing needs. * Add example project for Gorilla As part of oapi-codegen#465, we should produce an example version of the Petstore API using Gorilla, to validate that this works, as well as showing sample usage to consumers.
* Add support for Gorilla generation As part of oapi-codegen#465, it'd be handy to have gorilla/mux, a commonly used HTTP server as a generated server. This is very similar to Chi, as it is also `net/http` compliant, and allows us to mostly copy-paste the code, with very minor tweaks for Gorilla-specific routing needs. * Add example project for Gorilla As part of oapi-codegen#465, we should produce an example version of the Petstore API using Gorilla, to validate that this works, as well as showing sample usage to consumers.
So far this seems to have worked for one of my services, but would be good to hear if others have success.
I'll also be adding the
examples/
version so folks can see how that works too.Closes #465.