swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

using `"$ref": "#/responses/<name>"` does not set request `format`

Open axel7083 opened this issue 1 week ago • 0 comments

Might be related to https://github.com/acacode/swagger-typescript-api/issues/977

Description

When using a reference to a response definition, the format is not set to json in RequestParams. Leading to null result when using the generated api.

Example

Let's say you define your swagger with the following, (Example describe in documentation https://swagger.io/docs/specification/v2_0/describing-responses/#reusing-responses)

{
  "swagger": "2.0",
  "paths": {
    "/pet": {
      "get": {
        "operationId": "getPet",
        "produces": ["application/json"],
        "parameters": [],
        "responses": {
          "200": {
            "$ref": "#/responses/GetPetResponse"
          }
        }
      }
    }
  },
  "definitions": {
    "Pet": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "id": {
          "type": "string"
        }
      }
    }
  },
  "responses": {
    "GetPetResponse": {
      "description": "successful operation",
      "schema": {
        "$ref": "#/definitions/Pet"
      }
    }
  },
}

Here is a comparison of the actual output vs the expected output.

Actual 🔴 Expected 🟢
getPet: (params: RequestParams = {}) =>
      this.request<PetTTT, any>({
        path: `/pet`,
        method: "GET",
        ...params,
      })
getPet: (params: RequestParams = {}) =>
      this.request<PetTTT, any>({
        path: `/pet`,
        method: "GET",
        format: "json", // <-- THIS
        ...params,
      })

Investigation

The format property seems to be conditionally defined here

https://github.com/acacode/swagger-typescript-api/blob/d987aee6a2f7dbf6edba73b58a2df2fceb32a876/templates/modular/procedure-call.ejs#L101

The responseFormatTmpl is calculated above

https://github.com/acacode/swagger-typescript-api/blob/13d26a6f3d5680d047d285db20bdb319f0f734d8/src/schema-routes/schema-routes.ts#L713

Running in debugger mode and breaking on the following function while generating from the example above

https://github.com/acacode/swagger-typescript-api/blob/d987aee6a2f7dbf6edba73b58a2df2fceb32a876/templates/modular/procedure-call.ejs#L66

Image

The responseBodyInfo seems to be computed in getResponseBodyInfo

https://github.com/acacode/swagger-typescript-api/blob/13d26a6f3d5680d047d285db20bdb319f0f734d8/src/schema-routes/schema-routes.ts#L909

The following line seems to be very problematic

https://github.com/acacode/swagger-typescript-api/blob/13d26a6f3d5680d047d285db20bdb319f0f734d8/src/schema-routes/schema-routes.ts#L419

I don't understand why the operationId is passed to getContentTypes. Looking at the implementation of getContentTypes this is not intended to process strings.

Checking the only caller of getRequestInfoTypes we can see it already compute the contentTypes

https://github.com/acacode/swagger-typescript-api/blob/13d26a6f3d5680d047d285db20bdb319f0f734d8/src/schema-routes/schema-routes.ts#L452-L455

Therefore we should simply pass it down, and it fixes the problem

axel7083 avatar Dec 10 '25 12:12 axel7083