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

feat(openapi): adding a command to resolve circular and recursive references #1063

Open
wants to merge 58 commits into
base: next
Choose a base branch
from

Conversation

olehshh
Copy link

@olehshh olehshh commented Nov 11, 2024

🚥 Resolves RM-10283

🧰 Changes

  1. Adding a script to automate the workaround for circular and recursive references.
  2. Adding this command to the list of existing ones.

🧬 QA & Testing

Take any file with circular references, for example

openapi: 3.0.0
info:
  title: Cyclic Reference Example
  version: 1.0.0
paths:
  /example:
    get:
      summary: Example endpoint with cyclic references
      responses:
        '200':
          description: Successful response
components:
  Schemas:
    ObjectC:
      type: object
      properties:
        test:
          type: string
    ObjectA:
      type: object
      properties:
        test:
          type: string
        relatedObject:
          $ref: '#/components/schemas/ObjectB'
    ObjectB:
      type: object
      properties:
        test:
          type: string
        relatedObject:
          type: array
          items:
            $ref: '#/components/schemas/ObjectA'
        test2:
          $ref: '#/components/schemas/ObjectC' 

Execute the rdme openapi:refs [path] command and use inspect to check for circular references after execution. The command also handles recursiveness.

@olehshh olehshh changed the title Oleh/openapi adding command to solve circularity and recursiveness docs(openapi): adding a command to resolve circular and recursive references Nov 11, 2024
@olehshh olehshh changed the title docs(openapi): adding a command to resolve circular and recursive references feat(openapi): adding a command to resolve circular and recursive references Nov 11, 2024
@olehshh olehshh requested a review from kanadgupta November 11, 2024 22:28
@olehshh olehshh added enhancement New feature or request command:openapi Issues pertaining to the `openapi`, `validate`, `reduce`, or `swagger` commands labels Nov 12, 2024
Copy link
Member

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great so far! haven't had a chance to properly test this yet, but some preliminary feedback below! also if you could add some docs in README.md that'd be great. thanks!

return 'File path is required.';
}

const openApiData = OpenAPISolvingCircularityAndRecursiveness.readOpenApiFile(spec);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than using this static method to read the file, can you use our existing prepareOas function? see an example here:

const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:convert', { convertToLatest: true });

}
}

OpenAPISolvingCircularityAndRecursiveness.writeOpenApiFile(spec, openApiData);
Copy link
Member

@kanadgupta kanadgupta Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than overwriting the existing file, can you add a prompt (and a corresponding --out flag) so users can specify what the updated file path should be? see here for an example:

prompts.override({
outputPath: opts.out,
});
const promptResults = await promptTerminal([
{
type: 'text',
name: 'outputPath',
message: 'Enter the path to save your converted/bundled API definition to:',
initial: () => {
const extension = path.extname(specPath);
return `${path.basename(specPath).split(extension)[0]}.openapi${extension}`;
},
validate: value => validateFilePath(value),
},
]);
Command.debug(`saving converted/bundled spec to ${promptResults.outputPath}`);
fs.writeFileSync(promptResults.outputPath, JSON.stringify(parsedPreparedSpec, null, 2));

@olehshh olehshh requested a review from kanadgupta November 14, 2024 13:37
Copy link
Member

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DM'd you about this but this has a few merge conflicts now with the changes that were shipped in #1068 — happy to help with these if you need a hand!

@olehshh olehshh requested a review from kanadgupta November 19, 2024 14:50
@olehshh olehshh requested a review from kanadgupta December 2, 2024 21:06
Copy link
Member

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more feedback! i'll try and open a PR this week based off of yours with copy edits

import { runCommandAndReturnResult } from '../../helpers/oclif.js';

describe('openapi:solving-circularity-recursiveness', () => {
let run: (args?: string[]) => Promise<unknown>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let run: (args?: string[]) => Promise<unknown>;
let run: (args?: string[]) => Promise<string>;

},
{
description: 'If you wish to automate this command, you can pass in CLI arguments to bypass the prompts:',
command: '<%= config.bin %> <%= command.id %> petstore.json -out petstore.openapi.json',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
command: '<%= config.bin %> <%= command.id %> petstore.json -out petstore.openapi.json',
command: '<%= config.bin %> <%= command.id %> petstore.json --out petstore.openapi.json',

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you revert this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I returned this file, there was some confusion with them(

Comment on lines 56 to 59
* @param {OASDocument} document - The OpenAPI document to analyze.
* @returns {Promise<string[]>} A list of circular reference paths.
*/
static async getCircularRefsFromOas(document: OASDocument): Promise<string[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you go through and remove all of the type definitions that you have in JSDocs (e.g., @param {OASDocument}) and move your parameter docs above the parameters themselves? they're superfluous since you're already are defining the types in your TS function parameter type annotations. see below for an example!

Suggested change
* @param {OASDocument} document - The OpenAPI document to analyze.
* @returns {Promise<string[]>} A list of circular reference paths.
*/
static async getCircularRefsFromOas(document: OASDocument): Promise<string[]> {
* @returns A list of circular reference paths.
*/
static async getCircularRefsFromOas(
/** The OpenAPI document to analyze. */
document: OASDocument,
): Promise<string[]> {


if (remainingCircularRefs.length > 0) {
// eslint-disable-next-line no-console
console.info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use the info logger from lib/logger instead?

function info(

@olehshh olehshh requested a review from kanadgupta December 2, 2024 21:38
@olehshh olehshh self-assigned this Dec 4, 2024
@kanadgupta kanadgupta mentioned this pull request Dec 5, 2024
4 tasks
@kanadgupta kanadgupta removed this from the v9 milestone Dec 5, 2024
kanadgupta and others added 8 commits December 5, 2024 18:33
## 🧰 Changes

Touched up a few things in #1063!

- [x] Documentation copy edits
- [x] Logging touch ups
- [x] Makes sure we're only allowing this to be run against OpenAPI
- [ ] Adds a stricter type in
81607ca
that ends up breaking the build — I believe this is a code smell.
@olehshh can you look into fixing the type errors here?

## 🧬 QA & Testing

Are my changes sound?
This reverts commit 9249d2a.
@olehshh olehshh marked this pull request as draft December 6, 2024 18:40
@olehshh olehshh marked this pull request as ready for review December 8, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
command:openapi Issues pertaining to the `openapi`, `validate`, `reduce`, or `swagger` commands enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants