lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.54k stars 499 forks source link

Problem Importing JSON with `@example` in TSOA #1480

Closed IamCocoDev closed 1 year ago

IamCocoDev commented 1 year ago

Description:

Context: I am using TSOA (TypeScript OpenAPI) to document and generate an API in TypeScript. Recently, I encountered an issue when trying to import JSON in examples defined with the @example decorator.

Problem: When I attempt to import JSON in an example using @example, TSOA does not recognize the value of the JSON and displays it as a string instead of a JSON object.

Steps to Reproduce the Issue:

  1. I defined an example using the @example decorator as follows:
@Example(importedExample)
public async someApiRoute() {
  // ...
}
  1. I imported a valid JSON called importedExample from another file, as shown below:
// examples.ts
export const importedExample = {
  // Valid JSON content here...
};
  1. When I consulted the documentation generated by TSOA, the example value was displayed as a string instead of a valid JSON object.

Expected Outcome: I expected TSOA to recognize the imported value of importedExample as a JSON object and display it correctly in the generated documentation.

Additional Information:

I appreciate any guidance or solutions to address this problem. Thank you!

IamCocoDev commented 1 year ago

Issue Resolution: TSOA Not Recognizing JSON Import in @example Decorator

Problem Description: I encountered an issue when trying to import JSON in examples defined with the @example decorator in TSOA (TypeScript OpenAPI). TSOA was not recognizing the imported JSON and was displaying it as a string instead of a JSON object in the generated documentation.

Steps to Reproduce the Issue:

  1. I defined an example using the @example decorator as follows:

    @Example(importedExample)
    public async someApiRoute() {
     // ...
    }
  2. I imported a valid JSON called importedExample from another file, as shown below:

    // examples.ts
    export const importedExample = {
     // Valid JSON content here...
    };
  3. When consulting the documentation generated by TSOA, the example value was displayed as a string instead of a valid JSON object.

Expected Outcome: I expected TSOA to recognize the imported value of importedExample as a JSON object and display it correctly in the generated documentation.

Resolution: To resolve this issue and make TSOA recognize the imported JSON, you can follow these steps:

  1. Ensure you have the latest version of TSOA installed in your project by running:

    npm install tsoa@latest
  2. To resolve the issue with alias paths or "barrels," use an absolute path to import the JSON file instead of an alias. Make sure the absolute path is correct. For example: Instead of:

    import { importedExample } from '@/examples';

    Use:

    import { importedExample } from './examples';
  3. Confirm that the JSON file (examples.ts in this case) is located at the correct path, and its content is a valid JSON object.

  4. Then, use the importedExample variable in the @Example decorator as follows:

    @Example(importedExample)
    public async someApiRoute() {
     // ...
    }

With these steps, you should be able to import and use the JSON correctly in examples defined with the @example decorator in TSOA, and it should be displayed correctly in the generated documentation. Ensure that the absolute path is correct, and the JSON file is properly formatted.

IamCocoDev commented 1 year ago

Have a nice day!