openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
558 stars 67 forks source link

Typegen: typegen can't assign parameter types to Operation methods if operationId includes non-alphanumeric characters #33

Closed jeongY-Cho closed 4 years ago

jeongY-Cho commented 4 years ago

I've been trying to work with the Riot API, (openapi schema here: http://www.mingweisamuel.com/riotapi-schema/openapi-3.0.0.json) and found that not only method names generated by typegen weren't escaped properly (ie #26) but also typegen could not assign proper types to each method.

I found that the dtsgenerator dependency sanitizes/normalizes operation id names when parsing schema to path names so path names for operations are not the same anymore, and thus cant be found by typegen.

Example: "tft-league-v1.getLeagueEntriesForSummoner" is sanitized as "tft_league_v1.getLeagueEntriesForSummoner"

So when typegen does a filter for the param paths on an operation, there are no results.

A fix would be to do the same transformation on operationId when searching for types.

Solution:

// on src/typegen/typegen.ts
import {normalizeTypeName} from "@anttiviljami/dtsgenerator/dist/core/typeNameConvertor"

  // normalized operationId
  const normalizedOpId = normalizeTypeName(operationId)
  // parameters arg
  const parameterTypePaths = _.chain([
    _.find(exportTypes, { schemaRef: `#/paths/${normalizedOpId}/pathParameters` }),
    _.find(exportTypes, { schemaRef: `#/paths/${normalizedOpId}/queryParameters` }),
    _.find(exportTypes, { schemaRef: `#/paths/${normalizedOpId}/headerParameters` }),
    _.find(exportTypes, { schemaRef: `#/paths/${normalizedOpId}/cookieParameters` }),
  ])
    .filter()
    .map('path')
    .value();
// and similar transformation for request body type and response type
anttiviljami commented 4 years ago

Fixed in release 1.1.2 of openapi-client-axios-typegen