@kevinoid/openapi-transformers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE.txt +19 -0
  2. package/README.md +134 -0
  3. package/add-tag-to-operation-ids.js +60 -0
  4. package/add-x-ms-enum-name.js +96 -0
  5. package/add-x-ms-enum-value-names.js +142 -0
  6. package/additional-properties-to-object.js +35 -0
  7. package/additional-properties-to-unconstrained.js +115 -0
  8. package/any-of-null-to-nullable.js +50 -0
  9. package/assert-properties.js +56 -0
  10. package/binary-string-to-file.js +54 -0
  11. package/bool-enum-to-bool.js +100 -0
  12. package/clear-html-response-schema.js +77 -0
  13. package/client-params-to-global.js +97 -0
  14. package/const-to-enum.js +49 -0
  15. package/escape-enum-values.js +211 -0
  16. package/exclusive-min-max-to-bool.js +61 -0
  17. package/format-to-type.js +54 -0
  18. package/index.js +94 -0
  19. package/inline-non-object-schemas.js +120 -0
  20. package/lib/component-manager.js +60 -0
  21. package/lib/matching-component-manager.js +74 -0
  22. package/lib/matching-parameter-manager.js +36 -0
  23. package/merge-all-of.js +60 -0
  24. package/merge-any-of.js +48 -0
  25. package/merge-one-of.js +48 -0
  26. package/nullable-not-required.js +240 -0
  27. package/nullable-to-type-null.js +46 -0
  28. package/openapi31to30.js +54 -0
  29. package/package.json +131 -0
  30. package/path-parameters-to-operations.js +63 -0
  31. package/pattern-properties-to-additional-properties.js +62 -0
  32. package/queries-to-x-ms-paths.js +63 -0
  33. package/read-only-not-required.js +111 -0
  34. package/ref-path-parameters.js +73 -0
  35. package/remove-default-only-response-produces.js +58 -0
  36. package/remove-paths-with-servers.js +34 -0
  37. package/remove-query-from-paths.js +526 -0
  38. package/remove-ref-siblings.js +78 -0
  39. package/remove-request-body.js +102 -0
  40. package/remove-response-headers.js +42 -0
  41. package/remove-security-scheme-if.js +166 -0
  42. package/remove-type-if.js +65 -0
  43. package/rename-components.js +285 -0
  44. package/replaced-by-to-description.js +50 -0
  45. package/server-vars-to-path-params.js +224 -0
  46. package/server-vars-to-x-ms-parameterized-host.js +247 -0
  47. package/type-null-to-enum.js +47 -0
  48. package/type-null-to-nullable.js +57 -0
  49. package/urlencoded-to-string.js +160 -0
  50. package/x-enum-to-ms.js +92 -0
@@ -0,0 +1,160 @@
1
+ /**
2
+ * @copyright Copyright 2020 Kevin Locke <kevin@kevinlocke.name>
3
+ * @license MIT
4
+ * @module "openapi-transformers/urlencoded-to-string.js"
5
+ */
6
+
7
+ import OpenApiTransformerBase from 'openapi-transformer-base';
8
+
9
+ const consumesSymbol = Symbol('consumes');
10
+
11
+ function isFormData(mediaType) {
12
+ return /^multipart\/form-data\s*(?:;.*)?$/i.test(mediaType);
13
+ }
14
+
15
+ function isUrlencoded(mediaType) {
16
+ return /^application\/x-www-form-urlencoded\s*(?:;.*)?$/i.test(mediaType);
17
+ }
18
+
19
+ /**
20
+ * Transformer to change request parameter types to string for operations which
21
+ * only consume application/x-www-form-urlencoded, to work around
22
+ * https://github.com/Azure/autorest/issues/3449
23
+ */
24
+ export default class UrlencodedToStringTransformer
25
+ extends OpenApiTransformerBase {
26
+ constructor() {
27
+ super();
28
+ this[consumesSymbol] = undefined;
29
+ }
30
+
31
+ transformSchema(schema) {
32
+ if (typeof schema !== 'object' || schema === null) {
33
+ return schema;
34
+ }
35
+
36
+ let newSchema = super.transformSchema(schema);
37
+
38
+ // Change primitive types to string
39
+ if (['number', 'integer', 'boolean'].includes(schema.type)) {
40
+ newSchema = {
41
+ ...newSchema,
42
+ type: 'string',
43
+ };
44
+
45
+ // TODO: Optionally remove constraints which don't apply to string type?
46
+ // Older JSON Schema drafts (e.g. JSON Schema Draft 4 ref'd by OAS2)
47
+ // specify the instance is only valid if it satisfies the constraint.
48
+ // Newer JSON Schema drafts (e.g. Wright 00 ref'd by OAS3.1) specify
49
+ // the constraint is ignored for non-numbers.
50
+ // Autorest ignores these constraints on string properties.
51
+ // They may be useful as documentation for API developers.
52
+ // Is there a reason to remove them?
53
+ /*
54
+ delete newSchema.multipleOf;
55
+ delete newSchema.minimum;
56
+ delete newSchema.exclusiveMaximum;
57
+ delete newSchema.maximum;
58
+ delete newSchema.exclusiveMinimum;
59
+ */
60
+ }
61
+
62
+ // Ensure enums are modeled as string
63
+ const { type, 'x-ms-enum': xMsEnum } = newSchema;
64
+ if (type === 'string'
65
+ && xMsEnum
66
+ && xMsEnum.modelAsString !== true) {
67
+ newSchema = { ...newSchema };
68
+ // Note: Could set modelAsString: true, but would cause conflict if any
69
+ // other params/schemas have same name
70
+ delete newSchema['x-ms-enum'];
71
+ }
72
+
73
+ // Note: There does not appear to be a need to remove validation properties
74
+ // (e.g. minimum, maximum, etc.) since Autorest ignores them for string
75
+ // properties.
76
+
77
+ return newSchema;
78
+ }
79
+
80
+ transformParameter(parameter) {
81
+ if (!parameter) {
82
+ return parameter;
83
+ }
84
+
85
+ if (parameter.in === 'formData') {
86
+ return this.transformSchema(parameter);
87
+ }
88
+
89
+ if (parameter.in === 'body' && parameter.schema) {
90
+ return {
91
+ ...parameter,
92
+ schema: this.transformSchema(parameter.schema),
93
+ };
94
+ }
95
+
96
+ return parameter;
97
+ }
98
+
99
+ transformRequestBody(requestBody) {
100
+ if (!requestBody || !requestBody.content) {
101
+ return requestBody;
102
+ }
103
+
104
+ const { content } = requestBody;
105
+ const consumes = Object.keys(content);
106
+ const urlencoded = consumes.find(isUrlencoded);
107
+ if (!urlencoded) {
108
+ return requestBody;
109
+ }
110
+
111
+ return {
112
+ ...requestBody,
113
+ content: {
114
+ ...content,
115
+ [urlencoded]: this.transformMediaType(content[urlencoded]),
116
+ },
117
+ };
118
+ }
119
+
120
+ transformOperation(operation) {
121
+ if (!operation) {
122
+ return super.transformOperation(operation);
123
+ }
124
+
125
+ const { parameters, requestBody } = operation;
126
+ if (requestBody === undefined && !Array.isArray(parameters)) {
127
+ return operation;
128
+ }
129
+
130
+ const newOperation = { ...operation };
131
+
132
+ const consumes = operation.consumes || this[consumesSymbol];
133
+ if (parameters !== undefined
134
+ && Array.isArray(consumes)
135
+ && consumes.some(isUrlencoded)
136
+ && !consumes.some(isFormData)) {
137
+ newOperation.parameters =
138
+ parameters.map(this.transformParameter.bind(this));
139
+ }
140
+
141
+ if (requestBody !== undefined) {
142
+ newOperation.requestBody =
143
+ this.transformRequestBody(newOperation.requestBody);
144
+ }
145
+
146
+ return newOperation;
147
+ }
148
+
149
+ transformOpenApi(openApi) {
150
+ this[consumesSymbol] = openApi.consumes;
151
+ try {
152
+ return {
153
+ ...openApi,
154
+ paths: this.transformPaths(openApi.paths),
155
+ };
156
+ } finally {
157
+ this[consumesSymbol] = undefined;
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * @copyright Copyright 2019 Kevin Locke <kevin@kevinlocke.name>
3
+ * @license MIT
4
+ * @module "openapi-transformers/x-enum-to-ms.js"
5
+ */
6
+
7
+ import { debuglog } from 'node:util';
8
+
9
+ import OpenApiTransformerBase from 'openapi-transformer-base';
10
+
11
+ const debug = debuglog('x-enum-to-ms');
12
+
13
+ function transformSchemaXEnumToXMsEnum(schema, schemaName) {
14
+ if ((!schema['x-enum-descriptions'] && !schema['x-enum-varnames'])
15
+ || (schema['x-ms-enum'] && schema['x-ms-enum'].values)) {
16
+ // Schema doesn't have enum varnames/descriptions to convert,
17
+ // or already has x-ms-enum.values
18
+ return schema;
19
+ }
20
+
21
+ const {
22
+ 'x-enum-descriptions': descriptions,
23
+ 'x-enum-varnames': varnames,
24
+ ...newSchema
25
+ } = schema;
26
+
27
+ const xMsEnum = {
28
+ // Make name first in output for aesthetics
29
+ name: undefined,
30
+ ...newSchema['x-ms-enum'],
31
+ values: newSchema.enum.map((value, i) => ({
32
+ value,
33
+ description: descriptions ? descriptions[i] : undefined,
34
+ name: varnames ? varnames[i] : undefined,
35
+ })),
36
+ };
37
+
38
+ if (!xMsEnum.name) {
39
+ if (!schemaName) {
40
+ debug('Unable to determine required x-ms-enum.name for %O', schema);
41
+ return schema;
42
+ }
43
+
44
+ xMsEnum.name = schemaName;
45
+ }
46
+
47
+ newSchema['x-ms-enum'] = xMsEnum;
48
+ return newSchema;
49
+ }
50
+
51
+ /**
52
+ * Transformer to convert x-enum-descriptions and x-enum-varnames to x-ms-enum
53
+ * for Autorest.
54
+ *
55
+ * https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md#enum
56
+ * https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-enum
57
+ */
58
+ export default class XEnumToXMsEnumTransformer extends OpenApiTransformerBase {
59
+ transformSchema(schema, schemaName) {
60
+ return transformSchemaXEnumToXMsEnum(
61
+ super.transformSchema(schema),
62
+ schemaName,
63
+ );
64
+ }
65
+
66
+ // Override to pass schemaName as second argument to transformSchema
67
+ transformMap(obj, transform) {
68
+ if (transform !== this.transformSchema) {
69
+ return super.transformMap(obj, transform);
70
+ }
71
+
72
+ if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
73
+ return obj;
74
+ }
75
+
76
+ const newObj = { ...obj };
77
+ for (const [propName, propValue] of Object.entries(obj)) {
78
+ if (propValue !== undefined) {
79
+ newObj[propName] = transform.call(this, propValue, propName);
80
+ }
81
+ }
82
+
83
+ return newObj;
84
+ }
85
+
86
+ transformParameter(parameter) {
87
+ return transformSchemaXEnumToXMsEnum(
88
+ super.transformParameter(parameter),
89
+ undefined,
90
+ );
91
+ }
92
+ }