discord-gophers / goapi-gen

This package contains a set of utilities for generating Go boilerplate code for services based on OpenAPI 3.0 API definitions
Apache License 2.0
132 stars 12 forks source link

Generated render/bind implementations #68

Open zacharyburkett opened 2 years ago

zacharyburkett commented 2 years ago

Currently we use https://github.com/go-chi/render interfaces for our generated types, simplifying request/response management.

I propose we implement our own alternative that is generated alongside server/type code.

A generated implementation removes the need for a dependency and allows us to easily modify it to suit any future needs (ie more content types).

dustin-decker commented 2 years ago

Closed by #47 ?

Karitham commented 2 years ago

Not exactly,

This issue is about generating go-chi/render's functions directly alongside the generated code, avoiding the dependency.

Since we already generate the implementation of interfaces, we just need the actual render implementation, which is quite small

zacharyburkett commented 2 years ago

Correct, the goal of this issue is to simplify our dependencies.

diamondburned commented 2 years ago

As a sidenote, I think we're better off changing the generated method signatures to return a *Response or (*Response, error) pair directly instead of having the user call the Render function, since we're already breaking the http.Handler signature. For example,

func (h handler) Login(w http.ResponseWriter, r *http.Request, params openapi.LoginParams) *openapi.Response {
    s, err := h.server.LoginServer().Login(
        r.Context(),
        params.Username, params.Password,
        foodtinder.LoginMetadata{
            UserAgent: r.Header.Get("User-Agent"),
        },
    )
    if err != nil {
        return openapi.LoginJSON400Response(openapi.Error{Error: err})
    }

    return openapi.LoginJSON200Response(openapi.Session{
        UserID: openapi.ID(s.UserID),
        Token:  s.Token,
        Expiry: s.Expiry,
        Metadata: openapi.LoginMetadata{
            UserAgent: optstr(s.Metadata.UserAgent),
        },
    })
}

(package openapi is autogenerated.)

Created PR #80 to implement this.