Skip to content

Commit

Permalink
feat(medusa-oas-cli): automatically fix missing circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Aug 15, 2024
1 parent 4cb2853 commit 47e52ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
26 changes: 18 additions & 8 deletions packages/cli/oas/medusa-oas-cli/src/command-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "./utils/circular-patch-utils"
import { getTmpDirectory, isFile } from "./utils/fs-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"

/**
Expand Down Expand Up @@ -135,7 +135,7 @@ export async function execute(cliParams: OptionValues): Promise<void> {

const srcFileSanitized = path.resolve(tmpDir, "tmp.oas.yaml")
await sanitizeOAS(srcFile, srcFileSanitized, configTmpFile)
await circularReferenceCheck(srcFileSanitized)
await fixCirclularReferences(srcFileSanitized, dryRun)

if (dryRun) {
console.log(`⚫️ Dry run - no files generated`)
Expand Down Expand Up @@ -223,22 +223,32 @@ const sanitizeOAS = async (
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)
if (circularRefs.length) {
console.log(circularRefs)
let errorMessage = `🔴 Unhandled circular references - Please manually patch using --config ./redocly-config.yaml`
const recommendation = getCircularPatchRecommendation(circularRefs, oas)
if (Object.keys(recommendation).length) {
const hint = formatHintRecommendation(recommendation)
errorMessage += `
Within redocly-config.yaml, try adding the following:
const hintMessage = `
###
${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`)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/oas/medusa-oas-cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
type ApiType = "store" | "admin" | "combined"

type CircularReferenceSchema = Record<string, string[]>

type CircularReferenceConfig = {
decorators: {
"medusa/circular-patch": {
schemas: CircularReferenceSchema
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getCircularReferences = async (
export const getCircularPatchRecommendation = (
circularRefs: string[],
oas: OpenAPIObject
): Record<string, string[]> => {
): CircularReferenceSchema => {
type circularReferenceMatch = {
schema: string
property: string
Expand Down Expand Up @@ -66,7 +66,7 @@ export const getCircularPatchRecommendation = (
}
})

const schemas = {}
const schemas: CircularReferenceSchema = {}
for (const match of matches) {
if (!schemas.hasOwnProperty(match.schema)) {
schemas[match.schema] = []
Expand All @@ -77,7 +77,7 @@ export const getCircularPatchRecommendation = (
}

export const formatHintRecommendation = (
recommendation: Record<string, string[]>
recommendation: CircularReferenceSchema
) => {
return jsonObjectToYamlString({
decorators: { "medusa/circular-patch": { schemas: recommendation } },
Expand Down

0 comments on commit 47e52ba

Please sign in to comment.