-
Notifications
You must be signed in to change notification settings - Fork 12
/
params.ts
223 lines (203 loc) · 6.13 KB
/
params.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
import {
HttpParamStyles,
IHttpEncoding,
IHttpHeaderParam,
IHttpOperationRequestBody,
IHttpPathParam,
IHttpQueryParam,
} from '@stoplight/types';
import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';
import { get, map, pick, pickBy, set } from 'lodash';
import {
BodyParameter,
FormDataParameter,
Header,
HeaderParameter,
PathParameter,
QueryParameter,
Schema,
} from 'swagger-schema-official';
import { getExamplesFromSchema } from './getExamplesFromSchema';
function chooseQueryParameterStyle(
parameter: QueryParameter,
):
| HttpParamStyles.PipeDelimited
| HttpParamStyles.SpaceDelimited
| HttpParamStyles.Form
| HttpParamStyles.CommaDelimited {
/** Must cast to 'any' because this field is missing from the types but it's defined in the spec
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
*/
switch (parameter.collectionFormat) {
case 'pipes':
return HttpParamStyles.PipeDelimited;
case 'ssv':
return HttpParamStyles.SpaceDelimited;
case 'csv':
return HttpParamStyles.CommaDelimited;
case 'multi':
default:
/**
* This implementation is, in fact, not fully compliant with oas3.
* As per oas3 spec: "Form style parameters defined by RFC6570.
* This option replaces collectionFormat with a csv
* (when explode is false) or multi (when explode is true)
* value from OpenAPI 2.0."
* But since there is no such property like 'explode' in oas2 we are defaulting to 'form'.
*/
return HttpParamStyles.Form;
}
}
export function translateToHeaderParam(parameter: HeaderParameter): IHttpHeaderParam {
return (pickBy({
...buildSchemaForParameter(parameter),
name: parameter.name,
style: HttpParamStyles.Simple,
required: parameter.required,
}) as unknown) as IHttpHeaderParam;
}
export function translateToHeaderParams(headers: { [headerName: string]: Header }): IHttpHeaderParam[] {
return map(headers, (header, name) => {
const { schema, description } = buildSchemaForParameter(Object.assign({ name }, header));
const param: IHttpHeaderParam = {
name,
style: HttpParamStyles.Simple,
schema: schema as JSONSchema4 | JSONSchema6 | JSONSchema7,
description,
};
return param;
});
}
export function translateToBodyParameter(body: BodyParameter, consumes: string[]): IHttpOperationRequestBody {
const examples = map(
get(body, 'x-examples') || (body.schema ? getExamplesFromSchema(body.schema) : void 0),
(value, key) => ({ key, value }),
);
return pickBy({
description: body.description,
required: body.required,
contents: consumes.map(mediaType => {
return {
mediaType,
schema: body.schema,
examples,
};
}),
});
}
export function translateFromFormDataParameters(
parameters: FormDataParameter[],
consumes: string[],
): IHttpOperationRequestBody {
const finalBody: IHttpOperationRequestBody = {
contents: consumes.map(mediaType => ({
mediaType,
schema: {
type: 'object',
},
})),
};
return parameters.reduce((body, parameter) => {
const { schema, description } = buildSchemaForParameter(parameter);
(body.contents || []).forEach(content => {
// workaround... JSONSchema4 does not support `allowEmptyValue`
if ('allowEmptyValue' in parameter) {
Object.assign(schema, { allowEmptyValue: parameter.allowEmptyValue });
}
if (description) {
schema.description = description;
}
set(content, `schema.properties.${parameter.name}`, schema);
if (parameter.required) {
const requiredIndex = get(content, 'schema.required.length', 0);
set(content, `schema.required.${requiredIndex}`, parameter.name);
}
if (parameter.collectionFormat) {
content.encodings = content.encodings || [];
const encoding = buildEncoding(parameter);
if (encoding) {
content.encodings.push(encoding);
}
}
});
return body;
}, finalBody);
}
function buildEncoding(parameter: FormDataParameter): IHttpEncoding | null {
switch (parameter.collectionFormat) {
case 'csv':
return {
property: parameter.name,
style: HttpParamStyles.CommaDelimited,
explode: false,
};
case 'pipes':
return {
property: parameter.name,
style: HttpParamStyles.PipeDelimited,
explode: false,
};
case 'multi':
return {
property: parameter.name,
style: HttpParamStyles.Form,
explode: true,
};
case 'ssv':
return {
property: parameter.name,
style: HttpParamStyles.SpaceDelimited,
explode: false,
};
}
return null;
}
export function translateToQueryParameter(query: QueryParameter): IHttpQueryParam {
return (pickBy({
...buildSchemaForParameter(query),
allowEmptyValue: query.allowEmptyValue,
name: query.name,
style: chooseQueryParameterStyle(query),
required: query.required,
}) as unknown) as IHttpQueryParam;
}
export function translateToPathParameter(parameter: PathParameter): IHttpPathParam {
return (pickBy({
...buildSchemaForParameter(parameter),
name: parameter.name,
style: HttpParamStyles.Simple,
required: parameter.required,
}) as unknown) as IHttpPathParam;
}
function buildSchemaForParameter(
param: QueryParameter | PathParameter | HeaderParameter | FormDataParameter | Header,
): { schema: Schema | JSONSchema4 | JSONSchema6 | JSONSchema7; description?: string } {
const schema = pick(
param,
'type',
'format',
'default',
'enum',
'exclusiveMaximum',
'exclusiveMinimum',
'maxItems',
'maxLength',
'maximum',
'minItems',
'minimum',
'minLength',
'title',
'items',
'pattern',
'uniqueItems',
'multipleOf',
);
if ('allowEmptyValue' in param && param.allowEmptyValue === false) {
schema.minLength = 1;
}
return {
schema,
description: param.description,
...('x-deprecated' in param && { deprecated: param['x-deprecated'] }),
};
}