hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.04k stars 87 forks source link

OpenAPI parameter name of only an underscore results in missing parameter name in services.gen.ts and malformed type definition #726

Open Beauchaj opened 2 months ago

Beauchaj commented 2 months ago

Description

An OpenAPI Query Parameter with the name _ (or any _ within the parameter name) fails to generate the corresponding property name under services.gen.ts (Fetch client). Any instance of _ is being stripped from the resulting value (leading, trailing, or otherwise) in the translation of the snake_case OpenAPI parameter key to the camelCase property name, but if the parameter name is only _ the generated ts is broken.

Noting: If the parameter was _ the query parameter generated under services.gen.ts is:

      query: {
        _: data.,
      },

instead of

      query: {
        _: data._,
      },

Also noting: Using _ as a name or character parameter in OpenAPI queries does not break the OpenAPI specification.

Expected output

types.gen.ts

export type ReachableData = {
    /**
     * Optional parameter for avoiding caching
     */
    _?: unknown;
};

services.gen.ts

/**
 * Reachable?
 * Does nothing and returns No Content
 * @param data The data for the request.
 * @param data._ Optional parameter for avoiding caching
 * @returns void Success
 * @throws ApiError
 */
export const reachable = (data: ReachableData = {}): CancelablePromise<ReachableResponse> => { return __request(OpenAPI, {
    method: 'HEAD',
    url: '/reachable',
    query: {
        _: data._
    }
}); };

Instead got output

types.gen.ts

export type ReachableData = {
    /**
     * Optional parameter for avoiding caching
     */
    ?: unknown;
};

services.gen.ts

/**
 * Reachable?
 * Does nothing and returns No Content
 * @param data The data for the request.
 * @param data. Optional parameter for avoiding caching
 * @returns void Success
 * @throws ApiError
 */
export const reachable = (data: ReachableData = {}): CancelablePromise<ReachableResponse> => { return __request(OpenAPI, {
    method: 'HEAD',
    url: '/reachable',
    query: {
        _: data.
    }
}); };

OpenAPI specification (optional)

openapi: 3.1.0
info:
  title: Test API
  description: API for Test
  version: development
security:
  - test_auth:
      - default
paths:
  /reachable:
    head:
      summary: Reachable?
      description: Does nothing and returns No Content
      operationId: reachable
      security: []
      parameters:
        - name: _
          in: query
          description: Optional parameter for avoiding caching
          schema: {}
      responses:
        '204':
          description: Success

Configuration

Just defaults, so standard fetch client generation

npx @hey-api/openapi-ts -i path/to/openapi.yaml -o src/client

System information (optional)

mrlubos commented 2 months ago

Hey @Beauchaj, I had a look at this and it seems to be affecting only the legacy clients. Both generated types and services look okay with only underscore in query parameter when using the new Fetch API client. If this issue is urgent, I'd be open to a pull request, but it sounds like an edge case?