hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
639 stars 45 forks source link

Certain Path Styles break "Type Generation" #655

Closed adawalli closed 3 weeks ago

adawalli commented 3 weeks ago

Description

I've noticed that certain paths formatted with underscores seem to break type generation (but no errors are actually given). There ends up being a mismatch in the "options" generated that doesn't match the search path

The generated types.gen.ts from my provided config below yields the following

export type GetProgramSearchData = {
  path: {
    searchText: string;
  };
};

export type GetProgramSearchResponse = unknown;

export type GetProgramSearchError = unknown;

export type $OpenApiTs = {
  '/programs/search/{search_text}': {
    get: {
      req: GetProgramSearchData;
      res: {
        /**
         * Success
         */
        200: unknown;
      };
    };
  };
};

I've noticed that the path ends up being /programs/search/{search_text}, but in the "path options", we get path.searchText (not path.search_text)

I then see the fetch reaching out to /programs/search/%7Bsearch_text%7D

search_text is being hard coded (well url encoded) instead of passing in the actual variable. I'm able to fix this by manually patching '/programs/search/{search_text} to '/programs/search/{searchText}

OpenAPI specification (optional)

{
  "swagger": "2.0",
  "basePath": "/api/v1.0",
  "paths": {
    "/programs/search/{search_text}": {
      "parameters": [
        {
          "name": "search_text",
          "in": "path",
          "required": true,
          "type": "string"
        }
      ],
      "get": {
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "operationId": "get_program_search",
        "tags": [
          "programs"
        ]
      }
    }
  },
  "info": {
    "title": "API",
    "version": "1.0"
  },
  "produces": [
    "application/json"
  ],
  "consumes": [
    "application/json"
  ]
}

Configuration

import { defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
  client: '@hey-api/client-fetch',
  input: './sample_api.json',
  output: {
    format: 'prettier',
    lint: 'eslint',
    path: './src/client',
  },
  types: {
    enums: 'javascript',
  },
});

System information (optional)

No response

adawalli commented 3 weeks ago

Should have said that I am using@hey-api/openapi-ts@0.45.1 to generate the client and services and "@hey-api/client-fetch@0.1.3"

mrlubos commented 3 weeks ago

Hey @adawalli, the addendum is the key part. Pretty sure this is fixed in the latest version of both. Please upgrade and let me know