-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-vars-to-path-params.js
225 lines (196 loc) · 6.5 KB
/
server-vars-to-path-params.js
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
/**
* @copyright Copyright 2019-2020 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/server-vars-to-path-params.js"
*/
import assert from 'node:assert';
// Note: Undocumented function which is part of the public API
// https://github.com/flitbit/json-ptr/issues/29
import { encodeUriFragmentIdentifier } from 'json-ptr';
import OpenApiTransformerBase from 'openapi-transformer-base';
import MatchingParameterManager from './lib/matching-parameter-manager.js';
/** Gets the longest common suffix of two strings.
*
* @private
*/
function commonSuffix(s1, s2) {
let i = s1.length - 1;
for (let j = s2.length - 1; i >= 0 && j >= 0; i -= 1, j -= 1) {
if (s1[i] !== s2[j]) {
break;
}
}
return s1.slice(i + 1);
}
/** Get the path portion of a templated URL.
*
* @private
*/
function getPath(url) {
// Note: url may be relative and lack scheme+authority
// Note: Can't use new URL() due to [/?#] in variable names
return url
// Remove scheme+authority, if present
.replace(/^(?:\{[^{}]*\}|[a-zA-Z_+.-])+:\/\/(?:\{[^{}]*\}|[^/{}])+/, '')
// Remove query or fragment, if present
.replace(/^((?:\{[^{}]*\}|[^{}?#])*)[?#].*$/, '$1');
}
/** Gets the common path segment suffix of the path part of all server URLs,
* containing at least one template expression.
*
* @private
*/
function getCommonPathSuffix(servers) {
// Common suffix of path part of URLs (may be in middle of template expr)
const commonUrlSuffix = servers
.map((server) => getPath(server.url))
.reduce(commonSuffix);
// Smallest suffix of commonUrlSuffix which begins on a path segment
// (i.e. at beginning or after a slash) and includes all template exprs.
const match = /(?:^|\/)[^/]*(?:\{[^{}]*\}[^{}]*)+$/.exec(commonUrlSuffix);
return match ? match[0] : '';
}
function transformPaths(paths, serverPath, serverParams) {
return Object.keys(paths)
.reduce((newPaths, path) => {
const pathItem = paths[path];
const { parameters } = pathItem;
newPaths[serverPath + path] = {
parameters:
parameters ? [...serverParams, ...parameters] : serverParams,
...pathItem,
};
return newPaths;
}, Object.create(Object.getPrototypeOf(paths)));
}
/**
* Transformer to convert Server Variables in path portion to Parameters on
* Path Item Objects for use with OpenAPI 2.
*
* TODO: Roll this into OpenAPI 3 -> 2 conversion?
*/
export default class ServerVarsToPathParamsTransformer
extends OpenApiTransformerBase {
constructor({ omitDefault } = {}) {
super();
if (omitDefault !== undefined && !Array.isArray(omitDefault)) {
throw new TypeError('options.omitDefault must be an Array');
}
this.options = {
omitDefault: omitDefault || [],
};
}
transformOpenApi(spec) {
if (!spec || !spec.servers || spec.servers.length === 0) {
return spec;
}
const pathSuffix = getCommonPathSuffix(spec.servers);
if (!pathSuffix) {
return spec;
}
const pathSuffixVarNames = pathSuffix
.match(/\{[^{}]*\}/g)
.map((templateExpr) => templateExpr.slice(1, -1));
// Transform servers and get variables for common path suffix
let pathSuffixVars;
const newServers = spec.servers.map((server) => {
const newUrl = server.url.replace(pathSuffix, '');
// Separate path suffix variables from remaining server variables
const variables = server.variables || {};
let haveNewVars = false;
const newVars = {};
const suffixVars = {};
for (const varName of Object.keys(variables)) {
const variable = variables[varName];
if (pathSuffixVarNames.includes(varName)) {
// Variable in path suffix
if (this.options.omitDefault.includes(varName)
&& Object.hasOwn(variable, 'default')) {
const newVariable = { ...variable };
delete newVariable.default;
suffixVars[varName] = newVariable;
} else {
suffixVars[varName] = variable;
}
// Check if variable is also in remaining server URL
if (newUrl.includes(`{${varName}}`)) {
haveNewVars = true;
newVars[varName] = variable;
}
} else {
haveNewVars = true;
newVars[varName] = variable;
}
}
if (pathSuffixVars === undefined) {
pathSuffixVars = suffixVars;
} else {
assert.deepStrictEqual(
pathSuffixVars,
suffixVars,
'shared path variables must be the same in all servers',
);
}
const newServer = { ...server };
newServer.url = newUrl;
if (haveNewVars) {
newServer.variables = newVars;
} else {
delete newServer.variables;
}
return newServer;
});
// Note: Autorest treats parameters defined globally as properties of the
// client, rather than the method, by default (which is what we want here).
// https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-parameter-location
let parameters, paramRefPrefix;
if (spec.openapi) {
parameters = { ...spec.components && spec.components.parameters };
paramRefPrefix = ['components', 'parameters'];
} else {
parameters = { ...spec.parameters };
paramRefPrefix = ['parameters'];
}
const paramManager = new MatchingParameterManager(parameters);
const serverParamRefs = Object.keys(pathSuffixVars).map((name) => {
const serverVar = pathSuffixVars[name];
const serverParam = {
name,
in: 'path',
required: true,
};
if (spec.openapi) {
serverParam.schema = {
type: 'string',
...serverVar,
};
if (Object.hasOwn(serverVar, 'description')) {
serverParam.description = serverVar.description;
delete serverParam.schema.description;
}
} else {
serverParam.type = 'string';
Object.assign(serverParam, serverVar);
}
const defName = paramManager.add(serverParam, name);
return {
$ref: encodeUriFragmentIdentifier([...paramRefPrefix, defName]),
};
});
const newPathPrefix = pathSuffix.replace(/\/$/, '');
const newSpec = {
...spec,
servers: newServers,
paths: transformPaths(spec.paths, newPathPrefix, serverParamRefs),
};
if (spec.openapi) {
newSpec.components = {
...spec.components,
parameters,
};
} else {
newSpec.parameters = parameters;
}
return newSpec;
}
}