feat(medusa-oas-cli): automatically fix missing circular references (#8600)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-08-15 13:31:47 +03:00
1 parent 3ea467e3e8
commit b7bb793f03
3 files changed
+31 -11

No files matched your search

@@ -12,7 +12,7 @@ import {
} from "./utils/circular-patch-utils" } from "./utils/circular-patch-utils"
import { getTmpDirectory, isFile } from "./utils/fs-utils" import { getTmpDirectory, isFile } from "./utils/fs-utils"
import { readJson } from "./utils/json-utils" import { readJson } from "./utils/json-utils"
import { readYaml, writeYaml, writeYamlFromJson } from "./utils/yaml-utils" import { jsonObjectToYamlString, readYaml, writeYaml, writeYamlFromJson } from "./utils/yaml-utils"
import yargs from "yargs" import yargs from "yargs"
/** /**
@@ -135,7 +135,7 @@ export async function execute(cliParams: OptionValues): Promise<void> {
const srcFileSanitized = path.resolve(tmpDir, "tmp.oas.yaml") const srcFileSanitized = path.resolve(tmpDir, "tmp.oas.yaml")
await sanitizeOAS(srcFile, srcFileSanitized, configTmpFile) await sanitizeOAS(srcFile, srcFileSanitized, configTmpFile)
await circularReferenceCheck(srcFileSanitized) await fixCirclularReferences(srcFileSanitized, dryRun)
if (dryRun) { if (dryRun) {
console.log(`⚫️ Dry run - no files generated`) console.log(`⚫️ Dry run - no files generated`)
@@ -223,22 +223,32 @@ const sanitizeOAS = async (
console.log(logs) console.log(logs)
} }
const circularReferenceCheck = async (srcFile: string): Promise<void> => { const fixCirclularReferences = async (srcFile: string, dryRun = false): Promise<void> => {
const { circularRefs, oas } = await getCircularReferences(srcFile) const { circularRefs, oas } = await getCircularReferences(srcFile)
if (circularRefs.length) { if (circularRefs.length) {
console.log(circularRefs)
let errorMessage = `🔴 Unhandled circular references - Please manually patch using --config ./redocly-config.yaml`
const recommendation = getCircularPatchRecommendation(circularRefs, oas) const recommendation = getCircularPatchRecommendation(circularRefs, oas)
if (Object.keys(recommendation).length) { if (Object.keys(recommendation).length) {
const hint = formatHintRecommendation(recommendation) const hint = formatHintRecommendation(recommendation)
errorMessage += ` const hintMessage = `
Within redocly-config.yaml, try adding the following:
### ###
${hint} ${hint}
### ###
` `
if (dryRun) {
throw new Error(
`🔴 Unhandled circular references - Please manually patch using --config ./redocly-config.yaml
Within redocly-config.yaml, try adding the following:` + hintMessage
)
}
const redoclyConfigPath = path.join(basePath, "redocly", "redocly-config.yaml")
const originalContent = await readYaml(redoclyConfigPath) as CircularReferenceConfig
originalContent.decorators["medusa/circular-patch"].schemas = Object.assign(
originalContent.decorators["medusa/circular-patch"].schemas,
recommendation
)
await writeYaml(redoclyConfigPath, jsonObjectToYamlString(originalContent))
console.log(`🟡 Added the following unhandled circular references to redocly-config.ts:` + hintMessage)
} }
throw new Error(errorMessage)
} }
console.log(`🟢 All circular references are handled`) console.log(`🟢 All circular references are handled`)
} }
@@ -1 +1,11 @@
type ApiType = "store" | "admin" | "combined" type ApiType = "store" | "admin" | "combined"
type CircularReferenceSchema = Record<string, string[]>
type CircularReferenceConfig = {
decorators: {
"medusa/circular-patch": {
schemas: CircularReferenceSchema
}
}
}
@@ -27,7 +27,7 @@ export const getCircularReferences = async (
export const getCircularPatchRecommendation = ( export const getCircularPatchRecommendation = (
circularRefs: string[], circularRefs: string[],
oas: OpenAPIObject oas: OpenAPIObject
): Record<string, string[]> => { ): CircularReferenceSchema => {
type circularReferenceMatch = { type circularReferenceMatch = {
schema: string schema: string
property: string property: string
@@ -66,7 +66,7 @@ export const getCircularPatchRecommendation = (
} }
}) })
const schemas = {} const schemas: CircularReferenceSchema = {}
for (const match of matches) { for (const match of matches) {
if (!schemas.hasOwnProperty(match.schema)) { if (!schemas.hasOwnProperty(match.schema)) {
schemas[match.schema] = [] schemas[match.schema] = []
@@ -77,7 +77,7 @@ export const getCircularPatchRecommendation = (
} }
export const formatHintRecommendation = ( export const formatHintRecommendation = (
recommendation: Record<string, string[]> recommendation: CircularReferenceSchema
) => { ) => {
return jsonObjectToYamlString({ return jsonObjectToYamlString({
decorators: { "medusa/circular-patch": { schemas: recommendation } }, decorators: { "medusa/circular-patch": { schemas: recommendation } },