-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-to-type.js
54 lines (48 loc) · 1.36 KB
/
format-to-type.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
/**
* @copyright Copyright 2019 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/format-to-type.js"
*/
import OpenApiTransformerBase from 'openapi-transformer-base';
function transformSchemaType(schema) {
if (schema.type === 'string') {
const formatToType = {
decimal: 'number',
double: 'number',
float: 'number',
integer: 'integer',
int32: 'integer',
int64: 'integer',
};
const newType = formatToType[schema.format];
if (newType) {
const newSchema = {
...schema,
type: newType,
};
if (newSchema.format === 'integer') {
// format: integer is redundant with type: integer
delete newSchema.format;
}
return newSchema;
}
}
return schema;
}
/**
* Transformer to convert known formats in an OAS3 doc to types (for Autorest).
*
* Autorest doesn't generate int/decimal/double properties for type: string.
* Change the type to generate as desired.
*/
export default class FormatToTypeTransformer extends OpenApiTransformerBase {
transformSchema(schema) {
return transformSchemaType(super.transformSchema(schema));
}
transformParameter(parameter) {
return transformSchemaType(super.transformParameter(parameter));
}
transformHeader(header) {
return transformSchemaType(super.transformHeader(header));
}
}