OpenAPITools / OpenAPITools/openapi-generator

Add support for path prefixes e.g. single endpoint handler for all GET requests to /static/...

Open
#7,573 3 comments 0 reactions 1 assignee View on GitHub

@jimschubert is already working on this.

Since Oct 2, 2020.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Problem

I am generating server code in GoLang, and I'd like to use a single handler for the path prefix "/static/"-- i.e. all endpoints that start with "/static/" use the same handler (presumably, the generated code will use PathPrefix instead of Path as described at https://github.com/gorilla/mux).

To summarize:

  • we do not want to be required to specify static resources as a query parameter e.g. /static/?filePath=img/favicon.ico
  • we do want a single api definition for all static resources such as /static/img/favicon.ico or /static/img/myimage.png
  • we want the generated code to handle all /static/... endpoints in a single handler function

Proposed solution

The easiest and most intuitive input/output I can think of would be as follows.

Input: path variable that ends with /.../ e.g.:

paths:
  /static/.../
    get:
      summary: static resources
      responses:
        "200":
          description: OK

Output: uses PathPrefix instead of Path in routers.go

Here's a suggested patch (note: I haven't tested this, but it should be close enough to get the gist)

--- a/current.go
+++ b/proposed.go
@@ -5,11 +5,19 @@ func NewRouter() *mux.Router {
                handler = route.HandlerFunc
                handler = Logger(handler, route.Name)
-               router.
-                       Methods(route.Method).
-                       Path(route.Pattern).
-                       Name(route.Name).
-                       Handler(handler)
+               if route.Pattern[len(route.Pattern)-5:] == "/.../"  {
+                       router.
+                               Methods(route.Method).
+                               PathPrefix(route.Pattern[0:len(route.Pattern)-4]).
+                               Name(route.Name).
+                               Handler(handler)
+               } else {
+                       router.
+                               Methods(route.Method).
+                               Path(route.Pattern).
+                               Name(route.Name).
+                               Handler(handler)
+               }
        }
        return router

Other comments

I only need/want this for the golang generator, but obviously it would be ideal to implement this feature for other languages.

I am willing to consider a bounty if you would like to propose one

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.