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 28 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 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));

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!

@kanadgupta kanadgupta added this to the v9 milestone Nov 22, 2024
@olehshh
Copy link
Author

olehshh commented Nov 25, 2024

A provisional approach to prevent modifications to files with unresolved references that cannot currently be addressed.

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