Closed IamCocoDev closed 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:
I defined an example using the @example
decorator as follows:
@Example(importedExample)
public async someApiRoute() {
// ...
}
I imported a valid JSON called importedExample
from another file, as shown below:
// examples.ts
export const importedExample = {
// Valid JSON content here...
};
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:
Ensure you have the latest version of TSOA installed in your project by running:
npm install tsoa@latest
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';
Confirm that the JSON file (examples.ts
in this case) is located at the correct path, and its content is a valid JSON object.
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.
Have a nice day!
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:
@example
decorator as follows:importedExample
from another file, as shown below: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!