Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem Importing JSON with @example in TSOA #1480

Closed
IamCocoDev opened this issue Sep 7, 2023 · 2 comments
Closed

Problem Importing JSON with @example in TSOA #1480

IamCocoDev opened this issue Sep 7, 2023 · 2 comments

Comments

@IamCocoDev
Copy link

IamCocoDev commented Sep 7, 2023

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:

  • TSOA Version: [Specify the version of TSOA you are using]
  • Operating System: [Specify your operating system]

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

@IamCocoDev
Copy link
Author

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
Copy link
Author

Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant