-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-x-ms-enum-name.js
96 lines (81 loc) · 2.43 KB
/
add-x-ms-enum-name.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
/**
* @copyright Copyright 2019 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/add-x-ms-enum-name.js"
*/
import OpenApiTransformerBase from 'openapi-transformer-base';
/**
* Transformer to add x-ms-enum.name from schema name, if not present
* (so Autorest will generate enum types).
*
* https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-enum
*/
export default class AddXMsEnumNameTransformer extends OpenApiTransformerBase {
// eslint-disable-next-line class-methods-use-this
transformSchema(schema, schemaName) {
if (!schema.enum || (schema['x-ms-enum'] && schema['x-ms-enum'].name)) {
return schema;
}
return {
...schema,
'x-ms-enum': {
...schema['x-ms-enum'],
name: schemaName,
},
};
}
/** Transforms Map[string,Schema] with named schemas by passing the
* name as the second argument to transformSchema.
*
* @param {!Object<string,object>|*} schemas Schmea map to transform.
* @returns {!Object<string,object>|*} If obj is a Map, a plain object with
* the same own enumerable string-keyed properties as obj with values
* returned by transformSchema. Otherwise, obj is returned unchanged.
*/
transformSchemas(schemas) {
if (typeof schemas !== 'object'
|| schemas === null
|| Array.isArray(schemas)) {
return schemas;
}
const newSchemas = { ...schemas };
for (const [schemaName, schema] of Object.entries(schemas)) {
if (schema !== undefined) {
newSchemas[schemaName] = this.transformSchema(schema, schemaName);
}
}
return newSchemas;
}
// Optimization: Only transform schemas
transformComponents(components) {
if (!components) {
return components;
}
const { schemas } = components;
if (!schemas) {
return components;
}
return {
...components,
schemas: this.transformSchemas(schemas),
};
}
// Optimization: Only transform components/definitions
transformOpenApi(openApi) {
if (!openApi) {
return openApi;
}
const { components, definitions } = openApi;
if (!components && !definitions) {
return openApi;
}
const newOpenApi = { ...openApi };
if (components) {
newOpenApi.components = this.transformComponents(components);
}
if (definitions) {
newOpenApi.definitions = this.transformSchemas(definitions);
}
return newOpenApi;
}
}