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
108 stars 5 forks source link

[bug-fix] Fix bugs related to mediaType #35

Closed skgndi12 closed 10 months ago

skgndi12 commented 11 months ago

Describe bug

Issues can arise in the following two scenarios

  1. When there is no request body in the API Spec, it leads to failed OAS generation.
  2. When the mediaType is specified above the request body or response body, it results in the generation of an invalid OAS.

Reproduction

Version

0.1.105

Case 1: If API spec does not have a body

Schema

type TestApiSpec = Tspec.DefineApiSpec<{
  basePath: "/test";
  tags: ["Test"];
  paths: {
    "/": {
      get: {
        summary: "Simple test";
        responses: {
          200: {};
        };
      };
    };
  };
}>;

Result

Failed to generate OAS

/Users/gimseong-won/coding/tspec/packages/tspec/src/generator/openapiGenerator.ts:112
    const { mediaType = '', ...bodyParams } = getObjectPropertyByPath(spec, 'body', openapiSchemas) as any;
            ^

TypeError: Cannot read properties of undefined (reading 'mediaType')
    at <anonymous> (/Users/gimseong-won/coding/tspec/packages/tspec/src/generator/openapiGenerator.ts:112:13)
    at Array.forEach (<anonymous>)
    at getOpenapiPaths (/Users/gimseong-won/coding/tspec/packages/tspec/src/generator/openapiGenerator.ts:90:9)
    at generateTspec (/Users/gimseong-won/coding/tspec/packages/tspec/src/generator/index.ts:177:17)
    at <anonymous> (/Users/gimseong-won/coding/tspec/packages/tspec/schema.ts:45:1)

Case 2: If mediaType are specified above the request or response

Schema

/** @mediaType application/json */
interface TestRequest {
  message?: string;
}

/** @mediaType application/json */
interface TestResponse {
    message: string;
}

type TestApiSpec = Tspec.DefineApiSpec<{
  basePath: "/test";
  tags: ["Test"];
  paths: {
    "/": {
      get: {
        summary: "Simple test";
        body: TestRequest;
        responses: {
          200: TestResponse;
        };
      };
    };
  };
}>;

Result

An invalid OAS is generated, which includes mediaType property

{
  "info": {
    "title": "Test for Tspec",
    "version": "1.0.0"
  },
  "openapi": "3.0.3",
  "paths": {
    "/test/": {
      "get": {
        "operationId": "TestApiSpec_get_/",
        "tags": [
          "Test"
        ],
        "summary": "Simple test",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "message": {
                    "type": "string"
                  }
                },
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TestResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TestRequest": {
        "mediaType": "application/json",
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "TestResponse": {
        "mediaType": "application/json",
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        },
        "additionalProperties": false,
        "required": [
          "message"
        ]
      }
    }
  }
}

Changes

  1. Handle cases where the body is undefined when the schema lacks body data.
  2. Exclude the mediaType property when obtaining data of schema type.

The code in change 2 were made solely to fix a bug, without considering the package architecture. As a result, they might need to be refactored to align with the architecture (TODO).

Suggestion

I believe your team needs test code. When developing a new feature, it's also crucial to ensure that it doesn't have any adverse effects on the existing features.

hyeonss0417 commented 10 months ago

I've released v0.1.106 with your fixes🎉 https://github.com/ts-spec/tspec/releases/tag/v0.1.106