ts-spec / tspec

Type-driven API Documentation library. Auto-generating REST API document based on TypeScript types.
https://ts-spec.github.io/tspec/
MIT License
127 stars 5 forks source link

Using a Type/Interface in "body" resulted in new declaration instead of a $ref #58

Open programatix opened 3 months ago

programatix commented 3 months ago

Describe the bug Take the following example,

interface MyResquest {
   title: string;
}
interface MyResponse {...}

type ApiSpec = Tspec.DefineApiSpec<{
     paths: {
        "/endpoint": {
            post: {
                summary: "summary",
                description: "description",
                body: MyResquest ,
                responses: { 201: MyResponse },
            }
        }
    },
}>;

will result in the following,

  "paths": {
    "/tasks": {
      "post": {
        "operationId": "...",
        "summary": "summary",
        "description": "description,
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "title"
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TaskResponse"
                }
              }
            }
          }
        }
      }
   }
}

I'm expecting it to use $ref instead, as such,

  "paths": {
    "/tasks": {
      "post": {
        "operationId": "...",
        "summary": "summary",
        "description": "description,
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MyRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MyResponse"
                }
              }
            }
          }
        }
      }
   }
}

Desktop (please complete the following information):

Additional context