Skip to content

[GO] Go Server: Adds ordered routes to go-server router #21280

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,33 @@ func (c *{{classname}}Controller) Routes() Routes {
{{#operations}}
{{#operation}}
"{{operationId}}": Route{
"{{{operationId}}}",
strings.ToUpper("{{httpMethod}}"),
"{{{basePathWithoutHost}}}{{{path}}}",
c.{{operationId}},
},
{{/operation}}
{{/operations}}
}
}{{#operations}}{{#operation}}
}

// OrderedRoutes returns all the api routes in a deterministic order for the {{classname}}Controller
func (c *{{classname}}Controller) OrderedRoutes() []Route {
return []Route{
{{#operations}}
{{#operation}}
Route{
"{{{operationId}}}",
strings.ToUpper("{{httpMethod}}"),
"{{{basePathWithoutHost}}}{{{path}}}",
c.{{operationId}},
},
{{/operation}}
{{/operations}}
}
}

{{#operations}}{{#operation}}

// {{nickname}} - {{{summary}}}
{{#isDeprecated}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (

// A Route defines the parameters for an api endpoint
type Route struct {
Method string
Pattern string
Name string
Copy link
Member

Choose a reason for hiding this comment

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

thanks for the PR

is adding Name a breaking change to the existing users using the auto-generated go server code?

Copy link
Author

Choose a reason for hiding this comment

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

From my understanding, this would only be a breaking change if someone constructed one using an unkeyed literal, so like Route{"pattern","etc"}, which in my experience is pretty rare to see in Go as the vast majority of folks used keyed literals (like Route{ Pattern: "pattern", ...})

In fact, even the Go language backwards compatibility documentation specifically calls this out and seems to indicate that only keyed structs will be ensured to be backwards compatible:

Struct literals. For the addition of features in later point releases, it may be necessary to add fields to exported structs in the API. Code that uses unkeyed struct literals (such as pkg.T{3, "x"}) to create values of these types would fail to compile after such a change. However, code that uses keyed literals (pkg.T{A: 3, B: "x"}) will continue to compile after such a change. We will update such data structures in a way that allows keyed struct literals to remain compatible, although unkeyed literals may fail to compile. (There are also more intricate cases involving nested data structures or interfaces, but they have the same resolution.) We therefore recommend that composite literals whose type is defined in a separate package should use the keyed notation.

Copy link
Author

Choose a reason for hiding this comment

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

There are a few situations where if people are using bits and pieces of generated code, but not all of it, it could be considered a breaking change, but I am not sure where y'all draw the official line for this repo (e.g. any mustache template change could be considered a breaking change if users are overriding one with their own version)

Method string
Pattern string
HandlerFunc http.HandlerFunc
}

Expand All @@ -32,6 +33,7 @@ type Routes map[string]Route
// Router defines the required methods for retrieving api routes
type Router interface {
Routes() Routes
OrderedRoutes() []Route
}

// NewRouter creates a new router for any number of api routers
Expand All @@ -49,19 +51,19 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
{{/chi}}
{{/routers}}
for _, api := range routers {
for {{#routers}}{{#mux}}name{{/mux}}{{#chi}}_{{/chi}}{{/routers}}, route := range api.Routes() {
for _, route := range api.OrderedRoutes() {
var handler http.Handler = route.HandlerFunc
{{#routers}}
{{#mux}}
handler = Logger(handler, name)
handler = Logger(handler, route.Name)
{{#featureCORS}}
handler = handlers.CORS()(handler)
{{/featureCORS}}

router.
Methods(route.Method).
Path(route.Pattern).
Name(name).
Name(route.Name).
Handler(handler)
{{/mux}}
{{#chi}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void verifyOrder() throws IOException {
// verify /getPath/latest is first route
Assert.assertEquals(Files.readAllLines(Paths.get(output + "/go/api_dev.go")).get(52), "\t\t\"GetLatest\": Route{");
// verify /getPath/{id} is second route
Assert.assertEquals(Files.readAllLines(Paths.get(output + "/go/api_dev.go")).get(57), "\t\t\"GetById\": Route{");
Assert.assertEquals(Files.readAllLines(Paths.get(output + "/go/api_dev.go")).get(58), "\t\t\"GetById\": Route{");

}

Expand Down
64 changes: 64 additions & 0 deletions samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions samples/openapi3/server/petstore/go/go-petstore/go/api_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions samples/openapi3/server/petstore/go/go-petstore/go/api_user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading