-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathopenapi-parser.ts
More file actions
249 lines (202 loc) · 6.83 KB
/
openapi-parser.ts
File metadata and controls
249 lines (202 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as SwaggerParser from '@apidevtools/swagger-parser';
import { OpenAPIV3 } from 'openapi-types';
import { Oas } from './types';
export class OpenAPIParser {
/**
* The URL of the OpenAPI specification to be parsed.
* For a start, this only supports local file paths.
*/
private specURL: string;
private operations: Oas.PathsOperations = {};
constructor(specURL: string) {
this.specURL = specURL;
}
async parse() {
try {
const api = (await SwaggerParser.default.parse(
this.specURL
)) as OpenAPIV3.Document;
this.buildOperationsMap(api);
} catch (error) {
console.error('Error parsing OpenAPI spec:', error);
}
}
private parseModel(
name: string,
models: OpenAPIV3.ComponentsObject,
): Oas.Body {
const { schemas } = models!
const model = schemas![name] as OpenAPIV3.SchemaObject;
const body = {} as Oas.Body;
body.properties = []
if (model.properties === undefined) {
body.properties.push({ ...model as Oas.BodyParam })
return body
}
const { properties, required, example } = model;
Object.entries(properties).forEach(([key, property]) => {
let bodyParam = {} as Oas.BodyParam
if ("$ref" in property) {
const name = property.$ref.split("/").pop()!;
// TODO: Consider parsing inner examples if available
// Maybe just merge to the top example
const { properties: innerParams } = this.parseModel(name, models)
bodyParam = {
...innerParams[0]
}
bodyParam.name = key
} else {
const tempProperty = property as OpenAPIV3.SchemaObject
bodyParam = {
name: key,
type: tempProperty.type!,
description: tempProperty.description ?? "",
required:
required === undefined
? false
: required.includes(key)
? true
: false,
};
}
body.properties.push(bodyParam);
});
if (example !== undefined) {
body.examples = example as Map<string, any>
}
return body;
}
private parseRequestBody(
schema: Oas.PropertySchema,
models: OpenAPIV3.ComponentsObject,
): Oas.Body {
if ("$ref" in schema) {
const name = schema.$ref.split("/").pop()!;
const body = this.parseModel(name, models);
return body;
}
const body = {} as Oas.Body
body.properties = []
if ("type" in schema && schema.type === "array") {
const schemaPath = schema.items as OpenAPIV3.ReferenceObject;
const name = schemaPath.$ref.split("/").pop()
const { properties, examples } = this.parseModel(name!, models)
const parent = {
name: "",
type: 'array',
required: true,
description: schema.description ?? "",
children: properties
} as Oas.BodyParam
body.properties.push(parent)
body.examples = examples
return body;
}
if ("type" in schema && schema.type === "object") {
const [key, path] = Object.entries(schema.properties!)[0];
const schemaPath = path as OpenAPIV3.ReferenceObject
const name = schemaPath.$ref.split("/").pop()!;
const { properties, examples } = this.parseModel(name, models)
const parent = {
name: key,
type: 'object',
required: schema.required ?? false,
description: schema.description ?? "",
children: properties
} as Oas.BodyParam
body.properties.push(parent)
body.examples = examples
return body;
}
if ("allOf" in schema) {
// An object is being used here because the OAS examples are actually of
// the object type even though they're typed as Map<string, any>
// Also, map isn't being parsed properly when writing to file
let mergedExamples = {}
schema.allOf?.forEach((schema) => {
const { properties, examples } = this.parseRequestBody(schema, models)
body.properties.push(...properties)
if (examples !== undefined) {
mergedExamples = { ...mergedExamples, ...examples }
}
});
body.examples = mergedExamples as Map<string, any>
return body
}
return {} as Oas.Body;
}
private parseParameters(
parameters: OpenAPIV3.ParameterObject[]
): Oas.RequestParameter {
let result: Oas.RequestParameter = {
// header: [],
pathParameter: [],
queryParameter: [],
};
parameters.forEach((parameter) => {
const endpointParam: Oas.EndpointParam = {
name: parameter.name,
description: parameter.description ?? "",
required: parameter.required ?? false,
schema: parameter.schema as OpenAPIV3.SchemaObject,
example: parameter.example,
};
if (parameter.in === "query") {
result.queryParameter.push(endpointParam);
}
if (parameter.in === "path") {
result.pathParameter.push(endpointParam);
}
});
return result;
}
private buildOperationsMap(api: OpenAPIV3.Document) {
const { components, paths } = api;
Object.entries(paths).forEach(([route, path]) => {
if (path === undefined) {
return;
}
const { parameters: pathParameters, ...pathOperations } = path;
let parameters = pathParameters ?? [];
Object.entries(pathOperations).forEach(([method, operation]) => {
const {
summary,
description,
operationId,
requestBody,
parameters: innerParams,
} = operation as OpenAPIV3.OperationObject;
parameters = innerParams ? [...parameters, ...innerParams] : parameters;
const partialOperation: Partial<Oas.Operation> = {
name: summary,
path: route,
method: method as OpenAPIV3.HttpMethods,
description,
};
if (requestBody !== undefined) {
const { content } = requestBody as OpenAPIV3.RequestBodyObject;
const mediaType = content["application/json"];
const body =
mediaType.schema === undefined
? {} as Oas.Body
: this.parseRequestBody(mediaType.schema, components!);
partialOperation.requestBody = body
}
if (parameters && parameters !== null && parameters.length > 0) {
const { pathParameter, queryParameter } =
this.parseParameters(parameters as OpenAPIV3.ParameterObject[]);
// partialOperation.header = header;
partialOperation.pathParameter = pathParameter;
partialOperation.queryParameter = queryParameter;
}
this.operations[operationId!] = partialOperation
});
});
}
getOperationById(operationId: string): Partial<Oas.Operation> | undefined {
return this.operations[operationId];
}
getOperations(): Record<string, Partial<Oas.Operation>> {
return this.operations;
}
}