-
Notifications
You must be signed in to change notification settings - Fork 34
/
openApiDiff.ts
311 lines (270 loc) · 10.4 KB
/
openApiDiff.ts
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import * as asyncFs from "@ts-common/fs"
import * as jsonParser from "@ts-common/json-parser"
import { getFilePosition } from "@ts-common/source-map"
import * as child_process from "child_process"
import * as fs from "fs"
import JSON_Pointer from "json-pointer"
import * as jsonRefs from "json-refs"
import * as os from "os"
import * as path from "path"
import * as sourceMap from "source-map"
import * as util from "util"
import { log } from "../util/logging"
import { ResolveSwagger } from "../util/resolveSwagger"
import { pathToJsonPointer } from "../util/utils"
const _ = require("lodash")
const exec = util.promisify(child_process.exec)
export type Options = {
readonly consoleLogLevel?: unknown
readonly logFilepath?: unknown
}
export type ProcessedFile = {
readonly fileName: string
readonly map: sourceMap.BasicSourceMapConsumer | sourceMap.IndexedSourceMapConsumer
readonly resolvedFileName: string
readonly resolvedJson: any
}
export type ChangeProperties = {
readonly location?: string
readonly path?: string
readonly ref?: string
}
export type Message = {
readonly id: string
readonly code: string
readonly docUrl: string
readonly message: string
readonly mode: string
readonly type: string
readonly new: ChangeProperties
readonly old: ChangeProperties
}
export type Messages = ReadonlyArray<Message>
const updateChangeProperties = (change: ChangeProperties, pf: ProcessedFile): ChangeProperties => {
if (change.location) {
let position
let jsonPointer
if (change.path != undefined) {
try {
jsonPointer = pathToJsonPointer(change.path)
const jsonValue = JSON_Pointer.get(pf.resolvedJson, jsonPointer)
position = getFilePosition(jsonValue)
} catch (e) {
console.log(e.message)
}
}
if (!position || !Object.keys(position).length) {
return { ...change, ref: "", location: "" }
}
const originalPosition = pf.map.originalPositionFor(position)
if (!originalPosition || !Object.keys(originalPosition).length) {
return { ...change, ref: "", location: "" }
}
const name = originalPosition.name as string
const namePath = name ? name.split("\n")[0] : ""
const parsedPath = namePath ? (JSON.parse(namePath) as string[]) : ""
const ref = parsedPath ? `${originalPosition.source}${jsonRefs.pathToPtr(parsedPath, true)}` : ""
const location = `${originalPosition.source}:${originalPosition.line}:${(originalPosition.column as number) + 1}`
return { ...change, ref, location }
} else {
return {}
}
}
/**
* @class
* Open API Diff class.
*/
export class OpenApiDiff {
/**
* Constructs OpenApiDiff based on provided options.
*
* @param {object} options The configuration options.
*
* @param {boolean} [options.json] A boolean flag indicating whether output format of the messages is json.
*
* @param {boolean} [options.matchApiVersion] A boolean flag indicating whether to consider api-version while comparing.
*/
constructor(private options: Options) {
log.silly(`Initializing OpenApiDiff class`)
if (this.options === null || this.options === undefined) {
this.options = {}
}
if (typeof this.options !== "object") {
throw new Error('options must be of type "object".')
}
log.debug(`Initialized OpenApiDiff class with options = ${util.inspect(this.options, { depth: null })}`)
}
/**
* Compares old and new specifications.
*
* @param {string} oldSwagger Path to the old specification file.
*
* @param {string} newSwagger Path to the new specification file.
*
* @param {string} oldTag Tag name used for AutoRest with the old specification file.
*
* @param {string} newTag Tag name used for AutoRest with the new specification file.
*
*/
public async compare(oldSwagger: string, newSwagger: string, oldTag?: string, newTag?: string) {
log.silly(`compare is being called`)
const results = []
results[0] = await this.processViaAutoRest(oldSwagger, "old", oldTag)
results[1] = await this.processViaAutoRest(newSwagger, "new", newTag)
return this.processViaOpenApiDiff(results[0], results[1])
}
/**
* Gets path to the dotnet executable.
*
* @returns {string} Path to the dotnet executable.
*/
public dotNetPath(): string {
log.silly(`dotNetPath is being called`)
// Assume that dotnet is in the PATH
return "dotnet"
}
/**
* Gets path to the autorest application.
*
* @returns {string} Path to the autorest app.js file.
*/
public autoRestPath(): string {
log.silly(`autoRestPath is being called`)
// When oad is installed globally
{
const result = path.join(__dirname, "..", "..", "..", "node_modules", "autorest", "dist", "app.js")
if (fs.existsSync(result)) {
log.silly(`Found autoRest:${result} `)
return `node ${result}`
}
}
// When oad is installed locally
{
const result = path.join(__dirname, "..", "..", "..", "..", "..", "autorest", "dist", "app.js")
if (fs.existsSync(result)) {
log.silly(`Found autoRest:${result} `)
return `node ${result}`
}
}
// Try to find autorest in `node-modules`
{
const result = path.resolve("node_modules/.bin/autorest")
if (fs.existsSync(result)) {
log.silly(`Found autoRest:${result} `)
return result
}
}
// Assume that autorest is in the path
return "autorest"
}
/**
* Gets path to the OpenApiDiff.dll.
*
* @returns {string} Path to the OpenApiDiff.dll.
*/
public openApiDiffDllPath(): string {
log.silly(`openApiDiffDllPath is being called`)
return path.join(__dirname, "..", "..", "..", "dlls", "OpenApiDiff.dll")
}
/**
* Processes the provided specification via autorest.
*
* @param {string} swaggerPath Path to the specification file.
*
* @param {string} outputFileName Name of the output file to which autorest outputs swagger-doc.
*
* @param {string} tagName Name of the tag in the specification file.
*
*/
public async processViaAutoRest(swaggerPath: string, outputFileName: string, tagName?: string): Promise<ProcessedFile> {
log.silly(`processViaAutoRest is being called`)
if (swaggerPath === null || swaggerPath === undefined || typeof swaggerPath.valueOf() !== "string" || !swaggerPath.trim().length) {
throw new Error('swaggerPath is a required parameter of type "string" and it cannot be an empty string.')
}
if (
outputFileName === null ||
outputFileName === undefined ||
typeof outputFileName.valueOf() !== "string" ||
!outputFileName.trim().length
) {
throw new Error('outputFile is a required parameter of type "string" and it cannot be an empty string.')
}
log.debug(`swaggerPath = "${swaggerPath}"`)
log.debug(`outputFileName = "${outputFileName}"`)
if (!fs.existsSync(swaggerPath)) {
throw new Error(`File "${swaggerPath}" not found.`)
}
const outputFolder = os.tmpdir()
const outputFilePath = path.join(outputFolder, `${outputFileName}.json`)
const outputMapFilePath = path.join(outputFolder, `${outputFileName}.map`)
const autoRestCmd = tagName
? `${this.autoRestPath()} ${swaggerPath} --v2 --tag=${tagName} --output-artifact=swagger-document.json` +
` --output-artifact=swagger-document.map --output-file=${outputFileName} --output-folder=${outputFolder}`
: `${this.autoRestPath()} --v2 --input-file=${swaggerPath} --output-artifact=swagger-document.json` +
` --output-artifact=swagger-document.map --output-file=${outputFileName} --output-folder=${outputFolder}`
log.debug(`Executing: "${autoRestCmd}"`)
const { stderr } = await exec(autoRestCmd, {
encoding: "utf8",
maxBuffer: 1024 * 1024 * 64,
env: { ...process.env, NODE_OPTIONS: "--max-old-space-size=8192" }
})
if (stderr) {
throw new Error(stderr)
}
const resolveSwagger = new ResolveSwagger(outputFilePath)
const resolvedJson = resolveSwagger.resolve()
const resolvedPath: string = resolveSwagger.getResolvedPath()
if (!resolvedJson) {
throw new Error("resolve failed!")
}
const buffer = await asyncFs.readFile(outputMapFilePath)
const map = await new sourceMap.SourceMapConsumer(buffer.toString())
log.debug(`outputFilePath: "${outputFilePath}"`)
return {
fileName: outputFilePath,
map,
resolvedFileName: resolvedPath,
resolvedJson
}
}
/**
* Processes the provided specifications via OpenApiDiff tool.
*
* @param {string} oldSwagger Path to the old specification file.
*
* @param {string} newSwagger Path to the new specification file.
*
*/
public async processViaOpenApiDiff(oldSwaggerFile: ProcessedFile, newSwaggerFile: ProcessedFile) {
log.silly(`processViaOpenApiDiff is being called`)
const oldSwagger = oldSwaggerFile.resolvedFileName
const newSwagger = newSwaggerFile.resolvedFileName
if (oldSwagger === null || oldSwagger === undefined || typeof oldSwagger.valueOf() !== "string" || !oldSwagger.trim().length) {
throw new Error('oldSwagger is a required parameter of type "string" and it cannot be an empty string.')
}
if (newSwagger === null || newSwagger === undefined || typeof newSwagger.valueOf() !== "string" || !newSwagger.trim().length) {
throw new Error('newSwagger is a required parameter of type "string" and it cannot be an empty string.')
}
log.debug(`oldSwagger = "${oldSwagger}"`)
log.debug(`newSwagger = "${newSwagger}"`)
if (!fs.existsSync(oldSwagger)) {
throw new Error(`File "${oldSwagger}" not found.`)
}
if (!fs.existsSync(newSwagger)) {
throw new Error(`File "${newSwagger}" not found.`)
}
const cmd = `${this.dotNetPath()} ${this.openApiDiffDllPath()} -o ${oldSwagger} -n ${newSwagger}`
log.debug(`Executing: "${cmd}"`)
const { stdout } = await exec(cmd, { encoding: "utf8", maxBuffer: 1024 * 1024 * 64 })
const resultJson = JSON.parse(stdout) as Messages
const updatedJson = resultJson.map(message => ({
...message,
new: updateChangeProperties(message.new, newSwaggerFile),
old: updateChangeProperties(message.old, oldSwaggerFile)
}))
const uniqueJson = _.uniqWith(updatedJson, _.isEqual)
return JSON.stringify(uniqueJson)
}
}