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

Add support for Gorilla generation #585

Merged
merged 2 commits into from May 26, 2022

Conversation

jamietanna
Copy link
Collaborator

@jamietanna jamietanna commented May 20, 2022

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.

@jamietanna jamietanna marked this pull request as ready for review May 20, 2022 16:06
As part of deepmap#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.
As part of deepmap#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.
@jamietanna
Copy link
Collaborator Author

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? 🙏

@deepmap-marcinr
Copy link
Contributor

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.

@jamietanna
Copy link
Collaborator Author

Thanks! I think that's right, but maybe I should double check. This was mostly followed by using Chi as a base 🤔

@jamietanna
Copy link
Collaborator Author

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 net/http handles it, so happy for this to be merged when you're happy with it 🙌

@deepmap-marcinr deepmap-marcinr merged commit b3eedde into deepmap:master May 26, 2022
@jamietanna jamietanna deleted the feature/gorilla branch May 27, 2022 08:54
@micklove
Copy link

micklove commented Jul 1, 2022

@jamietanna , nice work, this is great, have been testing for the last few days, works well 👏
Will your gorilla changes be in the next release?

FYI - I used an earlier commit, on the master branch , to test your stuff, works well.
However, today, when I used the latest master (my build pipeline is just pulling the latest master, till the next release), it appears the mux import is no longer being written out)... example below...

image

@jamietanna
Copy link
Collaborator Author

jamietanna commented Jul 1, 2022

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 ☺

@micklove
Copy link

micklove commented Jul 1, 2022

Cheers Jamie, it appears to be something going on in the GA pipeline (latest HEAD working locally for me too), I'll sort it :)
(I'll dump the details here if they are relevant, and not just PEBKAC)

V Nice blog post BTW, will have a look at some of those tools you mentioned too, your hard work is much appreciated

@corani
Copy link
Contributor

corani commented Jul 5, 2022

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:

  1. You're generating type aliases (e.g. type Status = string) while I was generating new types (e.g. type Status string).
  2. For enum values you're generating constants named directly after the string value. To avoid collisions I was prefixing them by the type.

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"
)
  1. Using new types allows you to define methods on them, so for enum types I was generating a custom UnmarshalJSON method that validated that the provided enum value was actually allowed by the spec (not sure if this is too much "magic").
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)
    }
}
  1. It appears some useless code is being generated (or some code is missing) for optional query parameters:
// ------------- Optional query parameter "name" -------------
if paramValue := r.URL.Query().Get("name"); paramValue != "" {
    // this is empty
}

// BindQueryParameter
  1. When setting the Methods for the Gorilla HandleFunc you're using strings (e.g. "GET") while I used the constants in the http package (e.g. http.MethodGet).

@jamietanna
Copy link
Collaborator Author

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.

adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
* Add support for Gorilla generation

As part of deepmap#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 deepmap#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.
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.

Add support for gorilla/mux
4 participants