Skip to content

Path Parameters Are Not Encoded #679

@lew

Description

@lew

What are the steps to reproduce this issue?

  1. Create a minimal OpenAPI example like
{
  "info": {
    "title": "My API",
    "version": "1.0.0"
  },
  "openapi": "3.0.0",
  "paths": {
    "/test/{parameter}": {
      "get": {
        "parameters": [
          {
            "in": "path",
            "name": "parameter",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  }
}
  1. Generate the functions.
  2. Use a parameter with special characters in above example.

What happens?

The parameter parameter is just string concatenated into the URL:

export const getTestParameter = <TData = AxiosResponse<void>>(
  parameter: string,
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.get(`/test/${parameter}`, options)
}

As a result, requests fail.

What were you expecting to happen?

Strings in the path fragment must be encoded

export const getTestParameter = <TData = AxiosResponse<void>>(
  parameter: string,
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.get(`/test/${encodeURIComponent(parameter)}`, options)
}

Any other comments?

URL encoding should take place here because neither the rest of the app should care about encoding nor is it possible to later encode a fully constructed URL.

A programmer would typically prefer to use a method like this to avoid having to worry about internals:

const someParam = 'special[s]/?test'
const results = useGetTestParameter(someParam)

This bug happens at different places in the generated code.

What versions are you using?

"orval": "6.11.0-alpha.6"

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions