|
1
|
import { OpenAPIObject, SchemaObject } from "openapi3-ts" |
|
2
|
import OpenAPIParser from "@readme/openapi-parser" |
|
3
|
import { $Refs } from "@readme/json-schema-ref-parser" |
|
4
|
import { jsonObjectToYamlString } from "./yaml-utils" |
|
5
|
|
|
6
|
export const getCircularReferences = async ( |
|
7
|
srcFile: string |
|
8
|
): Promise<{ circularRefs: string[]; oas: OpenAPIObject }> => { |
|
9
|
const parser = new OpenAPIParser() |
|
10
|
const oas = (await parser.validate(srcFile, { |
|
11
|
dereference: { |
|
12
|
circular: "ignore", |
|
13
|
}, |
|
14
|
})) as OpenAPIObject |
|
15
|
if (parser.$refs.circular) { |
|
16
|
const $refs = parser.$refs as $Refs |
|
17
|
let circularRefs = $refs.circularRefs.map( |
|
18
|
(ref) => ref.match(/#\/components\/schemas\/.*/)![0] |
|
19
|
) |
|
20
|
circularRefs = [...new Set(circularRefs)] |
|
21
|
circularRefs.sort() |
|
22
|
return { circularRefs, oas } |
|
23
|
} |
|
24
|
return { circularRefs: [], oas } |
|
25
|
} |
|
26
|
|
|
27
|
export const getCircularPatchRecommendation = ( |
|
28
|
circularRefs: string[], |
|
29
|
oas: OpenAPIObject |
|
30
|
): CircularReferenceSchema => { |
|
31
|
type circularReferenceMatch = { |
|
32
|
schema: string |
|
33
|
property: string |
|
34
|
isArray: boolean |
|
35
|
referencedSchema: string |
|
36
|
} |
|
37
|
const matches: circularReferenceMatch[] = circularRefs |
|
38
|
.map((ref) => { |
|
39
|
let match = |
|
40
|
ref.match( |
|
41
|
/(?:.*)(?:#\/components\/schemas\/)(.*)(?:\/properties\/?)(.*)/ |
|
42
|
) ?? [] |
|
43
|
let schema = match[1] |
|
44
|
let property = match[2] |
|
45
|
let isArray = false |
|
46
|
if (property.endsWith("/items")) { |
|
47
|
property = property.replace("/items", "") |
|
48
|
isArray = true |
|
49
|
} |
|
50
|
return { schema, property, isArray } |
|
51
|
}) |
|
52
|
.filter((match) => match.property !== "") |
|
53
|
.map((match) => { |
|
54
|
const baseSchema = oas.components!.schemas![match.schema] as SchemaObject |
|
55
|
const propertySpec = match.isArray |
|
56
|
? (baseSchema.properties![match.property] as SchemaObject).items! |
|
57
|
: baseSchema.properties![match.property] |
|
58
|
const referencedSchema = propertySpec["$ref"].match( |
|
59
|
/(?:#\/components\/schemas\/)(.*)/ |
|
60
|
)![1] |
|
61
|
return { |
|
62
|
schema: match.schema, |
|
63
|
property: match.property, |
|
64
|
isArray: match.isArray, |
|
65
|
referencedSchema, |
|
66
|
} |
|
67
|
}) |
|
68
|
|
|
69
|
const schemas: CircularReferenceSchema = {} |
|
70
|
for (const match of matches) { |
|
71
|
if (!schemas.hasOwnProperty(match.schema)) { |
|
72
|
schemas[match.schema] = [] |
|
73
|
} |
|
74
|
schemas[match.schema].push(match.referencedSchema) |
|
75
|
} |
|
76
|
return schemas |
|
77
|
} |
|
78
|
|
|
79
|
export const formatHintRecommendation = ( |
|
80
|
recommendation: CircularReferenceSchema |
|
81
|
) => { |
|
82
|
return jsonObjectToYamlString({ |
|
83
|
decorators: { "medusa/circular-patch": { schemas: recommendation } }, |
|
84
|
}) |
|
85
|
} |