URL encoding in path parameter with Chi

116 views Asked by At

I used oapi-codegen to generate go code from an api yaml specification. The generated code is compliant with the Chi router. The generated code for this path looks like

    r.Group(func(r chi.Router) {
        r.Get(options.BaseURL+"/endpoint/{full}", wrapper.Endpoint)
    })

One endpoint is a GET /{full}, an excerpt :

  /endpoint/{full}:
    get:
      parameters:
        - name: full
          in: path
          required: true
          schema:
            type: string
      responses:
        201:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Thing'

The path parameter full can contain specials characters such as slashes /.

I'd want something like curl localhost:8080/api/v1/endpoint/anExample/OfAFullParam/WithSlashes to route to the /endpoint/{full} with the string anExample/OfA/FullParam/WithSlashes as the full parameter, this is not the case, and instead I get a 404 not found. The only workaround I have now is to manually send the /s as %2Fs.. such as curl localhost:8080%2Fapi%2Fv1%2Fendpoint%2FanExample%2FOfAFullParam%2FWithSlashes. A simplified excerpt of the handler:

func (s *Server) Endpoint(w http.ResponseWriter, r *http.Request, full string) {
        ds := MakeDatastructure(full)
    _ = database.PersistStruct(&ds)
    w.WriteHeader(http.StatusCreated)
    _ = json.NewEncoder(w).Encode(url)
}

Is there any way this could be achieved?

0

There are 0 answers