Skip to content

Path parameters with dots generates nonfunctional typescript #413

Closed
@ianrose14

Description

@ianrose14

Summary

If a path parameter has a dot in it (e.g. item.id) then swagger-typescript-api will rewrite this to camelCase for the parameter name but not in the path string interpolation, which leads in nonfunctional code.

Repro

Here's a minimal (or at least small) input to reproduce this:

{
  "swagger": "2.0",
  "info": {
    "title": "unset",
    "version": "unset"
  },
  "consumes": ["application/json"],
  "produces": ["application/json"],
  "paths": {
    "/foobar/{truck.id}/item": {
      "post": {
        "summary": "Repros an issue",
        "operationId": "ReproFunc",
        "responses": {
          "default": {
            "description": "A response.",
            "schema": {
              "$ref": "#/definitions/myResponse"
            }
          }
        },
        "parameters": [
          {
            "name": "truck.id",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ]
      }
    }
  },
  "definitions": {
    "myResponse": {
      "type": "object"
    }
  }
}

Expected output:

  foobar = {
    /**
     * No description
     *
     * @name ReproFunc
     * @summary Repros an issue
     * @request POST:/foobar/{truck.id}/item
     */
    reproFunc: (truckId: string, params: RequestParams = {}) =>
      this.request<any, MyResponse>({
        path: `/foobar/{truckId}/item`,
        method: "POST",
        ...params,
      }),
  };

Actual output:

  foobar = {
    /**
     * No description
     *
     * @name ReproFunc
     * @summary Repros an issue
     * @request POST:/foobar/{truck.id}/item
     */
    reproFunc: (truckId: string, params: RequestParams = {}) =>
      this.request<any, MyResponse>({
        path: `/foobar/{truck.id}/item`,
        method: "POST",
        ...params,
      }),
  };

Note that the path (/foobar/{truck.id}/item) references a variable ("truck.id") that does not exist, since the function parameter was rewritten to be truckId.

p.s. The obvious solution here is "don't use parameters with dots in them - that's weird!". Unfortunately the swagger we are using is itself generated by grpc-gateway which supports nested fields via dot notation (example) so there's no easy way to avoid this...

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions