swagger-typescript-api 12.0.4 → 13.0.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.
- package/LICENSE +21 -21
- package/README.md +1 -1
- package/cli/constants.js +3 -3
- package/cli/execute.js +52 -31
- package/cli/index.d.ts +1 -2
- package/cli/index.js +18 -17
- package/cli/operations/display-help.js +51 -29
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +28 -20
- package/index.d.ts +99 -7
- package/index.js +158 -136
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +367 -264
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -2
- package/src/commands/generate-templates/templates-gen-process.js +62 -35
- package/src/component-type-name-resolver.js +44 -0
- package/src/configuration.js +166 -98
- package/src/constants.js +28 -22
- package/src/index.js +3 -4
- package/src/schema-components-map.js +39 -23
- package/src/schema-parser/base-schema-parsers/array.js +43 -0
- package/src/schema-parser/base-schema-parsers/complex.js +51 -0
- package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
- package/src/schema-parser/base-schema-parsers/enum.js +158 -0
- package/src/schema-parser/base-schema-parsers/object.js +105 -0
- package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
- package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
- package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
- package/src/schema-parser/complex-schema-parsers/not.js +9 -0
- package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
- package/src/schema-parser/mono-schema-parser.js +48 -0
- package/src/schema-parser/schema-formatters.js +58 -44
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +212 -361
- package/src/schema-parser/schema-utils.js +158 -33
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
- package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
- package/src/schema-walker.js +93 -0
- package/src/swagger-schema-resolver.js +61 -28
- package/src/templates-worker.js +240 -0
- package/src/translators/javascript.js +83 -0
- package/src/translators/translator.js +35 -0
- package/src/{type-name.js → type-name-formatter.js} +35 -20
- package/src/util/file-system.js +30 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +46 -20
- package/src/util/name-resolver.js +50 -58
- package/src/util/object-assign.js +7 -3
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +5 -5
- package/src/util/sort-by-property.js +17 -0
- package/templates/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -28
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +2 -2
- package/templates/base/http-clients/axios-http-client.ejs +139 -138
- package/templates/base/http-clients/fetch-http-client.ejs +224 -224
- package/templates/base/interface-data-contract.ejs +10 -10
- package/templates/base/object-field-jsdoc.ejs +28 -28
- package/templates/base/route-docs.ejs +30 -30
- package/templates/base/route-name.ejs +42 -42
- package/templates/base/route-type.ejs +22 -21
- package/templates/base/type-data-contract.ejs +15 -15
- package/templates/default/api.ejs +69 -65
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -28
- package/templates/modular/api.ejs +28 -28
- package/templates/modular/procedure-call.ejs +100 -100
- package/templates/modular/route-types.ejs +18 -18
- package/src/templates.js +0 -177
- package/src/translators/JavaScript.js +0 -60
|
@@ -1,421 +1,272 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const {
|
|
5
|
-
const { SchemaUtils } = require(
|
|
6
|
-
const {
|
|
7
|
-
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
const { SCHEMA_TYPES } = require('../constants.js');
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const { SchemaFormatters } = require('./schema-formatters');
|
|
5
|
+
const { SchemaUtils } = require('./schema-utils');
|
|
6
|
+
const {
|
|
7
|
+
DiscriminatorSchemaParser,
|
|
8
|
+
} = require('./base-schema-parsers/discriminator');
|
|
9
|
+
const { EnumSchemaParser } = require('./base-schema-parsers/enum');
|
|
10
|
+
const { ObjectSchemaParser } = require('./base-schema-parsers/object');
|
|
11
|
+
const { PrimitiveSchemaParser } = require('./base-schema-parsers/primitive');
|
|
12
|
+
const { ComplexSchemaParser } = require('./base-schema-parsers/complex');
|
|
13
|
+
const { OneOfSchemaParser } = require('./complex-schema-parsers/one-of');
|
|
14
|
+
const { AllOfSchemaParser } = require('./complex-schema-parsers/all-of');
|
|
15
|
+
const { AnyOfSchemaParser } = require('./complex-schema-parsers/any-of');
|
|
16
|
+
const { NotSchemaParser } = require('./complex-schema-parsers/not');
|
|
17
|
+
const { ArraySchemaParser } = require('./base-schema-parsers/array');
|
|
18
|
+
const { sortByProperty } = require('../util/sort-by-property');
|
|
8
19
|
|
|
9
20
|
class SchemaParser {
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
21
|
+
/** @type {SchemaParserFabric} */
|
|
22
|
+
schemaParserFabric;
|
|
23
|
+
/** @type {CodeGenConfig} */
|
|
13
24
|
config;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
25
|
+
/** @type {Logger} */
|
|
26
|
+
logger;
|
|
27
|
+
/** @type {SchemaComponentsMap} */
|
|
18
28
|
schemaComponentsMap;
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
typeName;
|
|
23
|
-
/**
|
|
24
|
-
* @type {SchemaFormatters}
|
|
25
|
-
*/
|
|
29
|
+
/** @type {TypeNameFormatter} */
|
|
30
|
+
typeNameFormatter;
|
|
31
|
+
/** @type {SchemaFormatters} */
|
|
26
32
|
schemaFormatters;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @type {SchemaUtils}
|
|
30
|
-
*/
|
|
33
|
+
/** @type {SchemaUtils} */
|
|
31
34
|
schemaUtils;
|
|
35
|
+
/** @type {TemplatesWorker} */
|
|
36
|
+
templatesWorker;
|
|
37
|
+
/** @type {SchemaWalker} */
|
|
38
|
+
schemaWalker;
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
40
|
+
typeName;
|
|
41
|
+
schema;
|
|
42
|
+
schemaPath = [];
|
|
43
|
+
|
|
44
|
+
constructor(schemaParserFabric, { typeName, schema, schemaPath } = {}) {
|
|
45
|
+
this.schemaParserFabric = schemaParserFabric;
|
|
46
|
+
this.config = schemaParserFabric.config;
|
|
47
|
+
this.logger = schemaParserFabric.logger;
|
|
48
|
+
this.templatesWorker = schemaParserFabric.templatesWorker;
|
|
49
|
+
this.schemaComponentsMap = schemaParserFabric.schemaComponentsMap;
|
|
50
|
+
this.typeNameFormatter = schemaParserFabric.typeNameFormatter;
|
|
51
|
+
this.schemaWalker = schemaParserFabric.schemaWalker;
|
|
52
|
+
this.schemaFormatters = schemaParserFabric.schemaFormatters;
|
|
53
|
+
this.schemaUtils = schemaParserFabric.schemaUtils;
|
|
54
|
+
|
|
55
|
+
this.typeName = typeName || null;
|
|
56
|
+
this.schema = schema;
|
|
57
|
+
this.schemaPath = [...(schemaPath || [])];
|
|
41
58
|
}
|
|
42
59
|
|
|
43
|
-
|
|
44
|
-
// T1 | T2
|
|
60
|
+
_complexSchemaParsers = {
|
|
45
61
|
[SCHEMA_TYPES.COMPLEX_ONE_OF]: (schema) => {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
const SchemaParser =
|
|
63
|
+
this.config.schemaParsers.complexOneOf || OneOfSchemaParser;
|
|
64
|
+
const schemaParser = new SchemaParser(
|
|
65
|
+
this,
|
|
66
|
+
schema,
|
|
67
|
+
null,
|
|
68
|
+
this.schemaPath,
|
|
49
69
|
);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const type = this.config.Ts.UnionType(filtered);
|
|
53
|
-
|
|
54
|
-
return this.schemaUtils.safeAddNullToType(schema, type);
|
|
70
|
+
return schemaParser.parse();
|
|
55
71
|
},
|
|
56
|
-
// T1 & T2
|
|
57
72
|
[SCHEMA_TYPES.COMPLEX_ALL_OF]: (schema) => {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
73
|
+
const SchemaParser =
|
|
74
|
+
this.config.schemaParsers.complexAllOf || AllOfSchemaParser;
|
|
75
|
+
const schemaParser = new SchemaParser(
|
|
76
|
+
this,
|
|
77
|
+
schema,
|
|
78
|
+
null,
|
|
79
|
+
this.schemaPath,
|
|
61
80
|
);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const type = this.config.Ts.IntersectionType(filtered);
|
|
65
|
-
|
|
66
|
-
return this.schemaUtils.safeAddNullToType(schema, type);
|
|
81
|
+
return schemaParser.parse();
|
|
67
82
|
},
|
|
68
|
-
// T1 | T2 | (T1 & T2)
|
|
69
83
|
[SCHEMA_TYPES.COMPLEX_ANY_OF]: (schema) => {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
_.compact([
|
|
78
|
-
...filtered,
|
|
79
|
-
filtered.length > 1 && this.config.Ts.ExpressionGroup(this.config.Ts.IntersectionType(filtered)),
|
|
80
|
-
]),
|
|
84
|
+
const SchemaParser =
|
|
85
|
+
this.config.schemaParsers.complexAnyOf || AnyOfSchemaParser;
|
|
86
|
+
const schemaParser = new SchemaParser(
|
|
87
|
+
this,
|
|
88
|
+
schema,
|
|
89
|
+
null,
|
|
90
|
+
this.schemaPath,
|
|
81
91
|
);
|
|
82
|
-
|
|
83
|
-
return this.schemaUtils.safeAddNullToType(schema, type);
|
|
92
|
+
return schemaParser.parse();
|
|
84
93
|
},
|
|
85
|
-
// TODO
|
|
86
94
|
[SCHEMA_TYPES.COMPLEX_NOT]: (schema) => {
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
const SchemaParser =
|
|
96
|
+
this.config.schemaParsers.complexNot || NotSchemaParser;
|
|
97
|
+
const schemaParser = new SchemaParser(
|
|
98
|
+
this,
|
|
99
|
+
schema,
|
|
100
|
+
null,
|
|
101
|
+
this.schemaPath,
|
|
102
|
+
);
|
|
103
|
+
return schemaParser.parse();
|
|
89
104
|
},
|
|
90
105
|
};
|
|
91
106
|
|
|
92
|
-
|
|
107
|
+
_baseSchemaParsers = {
|
|
93
108
|
[SCHEMA_TYPES.ENUM]: (schema, typeName) => {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (Array.isArray(schema.enum) && Array.isArray(schema.enum[0])) {
|
|
104
|
-
return this.parseSchema(
|
|
105
|
-
{
|
|
106
|
-
oneOf: schema.enum.map((enumNames) => ({
|
|
107
|
-
type: "array",
|
|
108
|
-
items: enumNames.map((enumName) => ({ type: "string", enum: [enumName] })),
|
|
109
|
-
})),
|
|
110
|
-
},
|
|
111
|
-
typeName,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const keyType = this.getSchemaType(schema);
|
|
116
|
-
const enumNames = this.schemaUtils.getEnumNames(schema);
|
|
117
|
-
let content = null;
|
|
118
|
-
|
|
119
|
-
const formatValue = (value) => {
|
|
120
|
-
if (value === null) {
|
|
121
|
-
return this.config.Ts.NullValue(value);
|
|
122
|
-
}
|
|
123
|
-
if (keyType === this.getSchemaType({ type: "number" })) {
|
|
124
|
-
return this.config.Ts.NumberValue(value);
|
|
125
|
-
}
|
|
126
|
-
if (keyType === this.getSchemaType({ type: "boolean" })) {
|
|
127
|
-
return this.config.Ts.BooleanValue(value);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return this.config.Ts.StringValue(value);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
if (_.isArray(enumNames) && _.size(enumNames)) {
|
|
134
|
-
content = _.map(enumNames, (enumName, index) => {
|
|
135
|
-
const enumValue = _.get(schema.enum, index);
|
|
136
|
-
const formattedKey =
|
|
137
|
-
(enumName &&
|
|
138
|
-
this.typeName.format(enumName, {
|
|
139
|
-
type: "enum-key",
|
|
140
|
-
})) ||
|
|
141
|
-
this.typeName.format(`${enumValue}`, {
|
|
142
|
-
type: "enum-key",
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
if (this.config.enumNamesAsValues || _.isUndefined(enumValue)) {
|
|
146
|
-
return {
|
|
147
|
-
key: formattedKey,
|
|
148
|
-
type: this.config.Ts.Keyword.String,
|
|
149
|
-
value: this.config.Ts.StringValue(enumName),
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
key: formattedKey,
|
|
155
|
-
type: keyType,
|
|
156
|
-
value: formatValue(enumValue),
|
|
157
|
-
};
|
|
158
|
-
});
|
|
159
|
-
} else {
|
|
160
|
-
content = _.map(schema.enum, (key) => {
|
|
161
|
-
return {
|
|
162
|
-
key: this.typeName.format(`${key}`, {
|
|
163
|
-
type: "enum-key",
|
|
164
|
-
}),
|
|
165
|
-
type: keyType,
|
|
166
|
-
value: formatValue(key),
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
...(_.isObject(schema) ? schema : {}),
|
|
173
|
-
$ref: $ref,
|
|
174
|
-
typeName: typeName || ($ref && refType.typeName) || null,
|
|
175
|
-
$parsedSchema: true,
|
|
176
|
-
schemaType: SCHEMA_TYPES.ENUM,
|
|
177
|
-
type: SCHEMA_TYPES.ENUM,
|
|
178
|
-
keyType: keyType,
|
|
179
|
-
typeIdentifier: this.config.generateUnionEnums ? this.config.Ts.Keyword.Type : this.config.Ts.Keyword.Enum,
|
|
180
|
-
name: typeName,
|
|
181
|
-
description: this.schemaFormatters.formatDescription(schema.description),
|
|
182
|
-
content,
|
|
183
|
-
};
|
|
109
|
+
const SchemaParser = this.config.schemaParsers.enum || EnumSchemaParser;
|
|
110
|
+
const schemaParser = new SchemaParser(
|
|
111
|
+
this,
|
|
112
|
+
schema,
|
|
113
|
+
typeName,
|
|
114
|
+
this.schemaPath,
|
|
115
|
+
);
|
|
116
|
+
return schemaParser.parse();
|
|
184
117
|
},
|
|
185
118
|
[SCHEMA_TYPES.OBJECT]: (schema, typeName) => {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
description: this.schemaFormatters.formatDescription(schema.description),
|
|
196
|
-
allFieldsAreOptional: !_.some(_.values(contentProperties), (part) => part.isRequired),
|
|
197
|
-
content: contentProperties,
|
|
198
|
-
};
|
|
119
|
+
const SchemaParser =
|
|
120
|
+
this.config.schemaParsers.object || ObjectSchemaParser;
|
|
121
|
+
const schemaParser = new SchemaParser(
|
|
122
|
+
this,
|
|
123
|
+
schema,
|
|
124
|
+
typeName,
|
|
125
|
+
this.schemaPath,
|
|
126
|
+
);
|
|
127
|
+
return schemaParser.parse();
|
|
199
128
|
},
|
|
200
129
|
[SCHEMA_TYPES.COMPLEX]: (schema, typeName) => {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
211
|
-
name: typeName,
|
|
212
|
-
description: this.schemaFormatters.formatDescription(
|
|
213
|
-
schema.description || _.compact(_.map(schema[complexType], "description"))[0] || "",
|
|
214
|
-
),
|
|
215
|
-
content:
|
|
216
|
-
this.config.Ts.IntersectionType(
|
|
217
|
-
_.compact([
|
|
218
|
-
this.config.Ts.ExpressionGroup(complexSchemaContent),
|
|
219
|
-
this.getInternalSchemaType(simpleSchema) === SCHEMA_TYPES.OBJECT &&
|
|
220
|
-
this.config.Ts.ExpressionGroup(this.getInlineParseContent(simpleSchema)),
|
|
221
|
-
]),
|
|
222
|
-
) || this.config.Ts.Keyword.Any,
|
|
223
|
-
};
|
|
130
|
+
const SchemaParser =
|
|
131
|
+
this.config.schemaParsers.complex || ComplexSchemaParser;
|
|
132
|
+
const schemaParser = new SchemaParser(
|
|
133
|
+
this,
|
|
134
|
+
schema,
|
|
135
|
+
typeName,
|
|
136
|
+
this.schemaPath,
|
|
137
|
+
);
|
|
138
|
+
return schemaParser.parse();
|
|
224
139
|
},
|
|
225
140
|
[SCHEMA_TYPES.PRIMITIVE]: (schema, typeName) => {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (_.isArray(type) && type.length) {
|
|
237
|
-
contentType = this.complexSchemaParsers.oneOf({
|
|
238
|
-
...(_.isObject(schema) ? schema : {}),
|
|
239
|
-
oneOf: type.map((type) => ({ type })),
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (_.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
|
|
244
|
-
contentType = this.config.Ts.Tuple(items.map((item) => this.getInlineParseContent(item)));
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return {
|
|
248
|
-
...(_.isObject(schema) ? schema : {}),
|
|
249
|
-
$parsedSchema: true,
|
|
250
|
-
schemaType: SCHEMA_TYPES.PRIMITIVE,
|
|
251
|
-
type: SCHEMA_TYPES.PRIMITIVE,
|
|
252
|
-
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
253
|
-
name: typeName,
|
|
254
|
-
description: this.schemaFormatters.formatDescription(description),
|
|
255
|
-
// TODO: probably it should be refactored. `type === 'null'` is not flexible
|
|
256
|
-
content: type === this.config.Ts.Keyword.Null ? type : contentType || this.getSchemaType(schema),
|
|
257
|
-
};
|
|
141
|
+
const SchemaParser =
|
|
142
|
+
this.config.schemaParsers.primitive || PrimitiveSchemaParser;
|
|
143
|
+
const schemaParser = new SchemaParser(
|
|
144
|
+
this,
|
|
145
|
+
schema,
|
|
146
|
+
typeName,
|
|
147
|
+
this.schemaPath,
|
|
148
|
+
);
|
|
149
|
+
return schemaParser.parse();
|
|
258
150
|
},
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (!_.isEmpty(schema.properties)) return SCHEMA_TYPES.OBJECT;
|
|
265
|
-
|
|
266
|
-
return SCHEMA_TYPES.PRIMITIVE;
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
getSchemaType = (schema) => {
|
|
270
|
-
if (!schema) return this.config.Ts.Keyword.Any;
|
|
271
|
-
|
|
272
|
-
const refTypeInfo = this.schemaUtils.getSchemaRefType(schema);
|
|
273
|
-
|
|
274
|
-
if (refTypeInfo) {
|
|
275
|
-
return this.schemaUtils.checkAndAddRequiredKeys(
|
|
151
|
+
[SCHEMA_TYPES.DISCRIMINATOR]: (schema, typeName) => {
|
|
152
|
+
const SchemaParser =
|
|
153
|
+
this.config.schemaParsers.discriminator || DiscriminatorSchemaParser;
|
|
154
|
+
const schemaParser = new SchemaParser(
|
|
155
|
+
this,
|
|
276
156
|
schema,
|
|
277
|
-
|
|
157
|
+
typeName,
|
|
158
|
+
this.schemaPath,
|
|
278
159
|
);
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (_.isFunction(typeAlias)) {
|
|
293
|
-
resultType = typeAlias(schema, this);
|
|
294
|
-
} else {
|
|
295
|
-
resultType = typeAlias || primitiveType;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (!resultType) return this.config.Ts.Keyword.Any;
|
|
299
|
-
|
|
300
|
-
return this.schemaUtils.checkAndAddRequiredKeys(schema, this.schemaUtils.safeAddNullToType(schema, resultType));
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
getObjectSchemaContent = (schema) => {
|
|
304
|
-
const { properties, additionalProperties } = schema || {};
|
|
305
|
-
|
|
306
|
-
const propertiesContent = _.map(properties, (property, name) => {
|
|
307
|
-
this.$processingSchemaPath.push(name);
|
|
308
|
-
const required = this.schemaUtils.isPropertyRequired(name, property, schema);
|
|
309
|
-
const rawTypeData = _.get(this.schemaUtils.getSchemaRefType(property), "rawTypeData", {});
|
|
310
|
-
const nullable = !!(rawTypeData.nullable || property.nullable);
|
|
311
|
-
const fieldName = this.typeName.isValidName(name) ? name : this.config.Ts.StringValue(name);
|
|
312
|
-
const fieldValue = this.getInlineParseContent(property);
|
|
313
|
-
const readOnly = property.readOnly;
|
|
314
|
-
|
|
315
|
-
this.$processingSchemaPath.pop();
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
...property,
|
|
319
|
-
$$raw: property,
|
|
320
|
-
title: property.title,
|
|
321
|
-
description:
|
|
322
|
-
property.description ||
|
|
323
|
-
_.compact(_.map(property[this.getComplexType(property)], "description"))[0] ||
|
|
324
|
-
rawTypeData.description ||
|
|
325
|
-
_.compact(_.map(rawTypeData[this.getComplexType(rawTypeData)], "description"))[0] ||
|
|
326
|
-
"",
|
|
327
|
-
isRequired: required,
|
|
328
|
-
isNullable: nullable,
|
|
329
|
-
name: fieldName,
|
|
330
|
-
value: fieldValue,
|
|
331
|
-
field: this.config.Ts.TypeField({
|
|
332
|
-
readonly: readOnly && this.config.addReadonly,
|
|
333
|
-
optional: !required,
|
|
334
|
-
key: fieldName,
|
|
335
|
-
value: fieldValue,
|
|
336
|
-
}),
|
|
337
|
-
};
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
if (additionalProperties) {
|
|
341
|
-
propertiesContent.push({
|
|
342
|
-
$$raw: { additionalProperties },
|
|
343
|
-
description: "",
|
|
344
|
-
isRequired: false,
|
|
345
|
-
field: this.config.Ts.InterfaceDynamicField(this.config.Ts.Keyword.String, this.config.Ts.Keyword.Any),
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
return propertiesContent;
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
getComplexType = (schema) => {
|
|
353
|
-
if (schema.oneOf) return SCHEMA_TYPES.COMPLEX_ONE_OF;
|
|
354
|
-
if (schema.allOf) return SCHEMA_TYPES.COMPLEX_ALL_OF;
|
|
355
|
-
if (schema.anyOf) return SCHEMA_TYPES.COMPLEX_ANY_OF;
|
|
356
|
-
// TODO :(
|
|
357
|
-
if (schema.not) return SCHEMA_TYPES.COMPLEX_NOT;
|
|
358
|
-
|
|
359
|
-
return SCHEMA_TYPES.COMPLEX_UNKNOWN;
|
|
160
|
+
return schemaParser.parse();
|
|
161
|
+
},
|
|
162
|
+
[SCHEMA_TYPES.ARRAY]: (schema, typeName) => {
|
|
163
|
+
const SchemaParser = this.config.schemaParsers.array || ArraySchemaParser;
|
|
164
|
+
const schemaParser = new SchemaParser(
|
|
165
|
+
this,
|
|
166
|
+
schema,
|
|
167
|
+
typeName,
|
|
168
|
+
this.schemaPath,
|
|
169
|
+
);
|
|
170
|
+
return schemaParser.parse();
|
|
171
|
+
},
|
|
360
172
|
};
|
|
361
173
|
|
|
362
174
|
/**
|
|
363
|
-
*
|
|
364
|
-
* @param schema {any}
|
|
365
|
-
* @param typeName {null | string}
|
|
366
|
-
* @param formatter {"inline" | "base"}
|
|
367
175
|
* @return {Record<string, any>}
|
|
368
176
|
*/
|
|
369
|
-
parseSchema = (
|
|
370
|
-
if (!
|
|
177
|
+
parseSchema = () => {
|
|
178
|
+
if (!this.schema)
|
|
179
|
+
return this._baseSchemaParsers[SCHEMA_TYPES.PRIMITIVE](
|
|
180
|
+
null,
|
|
181
|
+
this.typeName,
|
|
182
|
+
);
|
|
371
183
|
|
|
372
184
|
let schemaType = null;
|
|
373
185
|
let parsedSchema = null;
|
|
374
186
|
|
|
375
|
-
if (typeof schema ===
|
|
376
|
-
return schema;
|
|
187
|
+
if (typeof this.schema === 'string') {
|
|
188
|
+
return this.schema;
|
|
377
189
|
}
|
|
378
190
|
|
|
379
|
-
if (!schema.$parsed) {
|
|
380
|
-
if (!typeName && this.schemaUtils.isRefSchema(schema)) {
|
|
381
|
-
typeName = this.getSchemaType(schema);
|
|
191
|
+
if (!this.schema.$parsed) {
|
|
192
|
+
if (!this.typeName && this.schemaUtils.isRefSchema(this.schema)) {
|
|
193
|
+
this.typeName = this.schemaUtils.getSchemaType(this.schema);
|
|
382
194
|
}
|
|
383
195
|
|
|
384
|
-
|
|
385
|
-
|
|
196
|
+
/**
|
|
197
|
+
* swagger schemas fixes
|
|
198
|
+
* ---->
|
|
199
|
+
*/
|
|
200
|
+
if (
|
|
201
|
+
this.schema.items &&
|
|
202
|
+
!Array.isArray(this.schema.items) &&
|
|
203
|
+
!this.schema.type
|
|
204
|
+
) {
|
|
205
|
+
this.schema.type = SCHEMA_TYPES.ARRAY;
|
|
206
|
+
}
|
|
207
|
+
if (
|
|
208
|
+
Array.isArray(this.schema.enum) &&
|
|
209
|
+
this.schema.enum.length === 1 &&
|
|
210
|
+
this.schema.enum[0] == null
|
|
211
|
+
) {
|
|
212
|
+
this.logger.debug('invalid enum schema', this.schema);
|
|
213
|
+
this.schema = { type: this.config.Ts.Keyword.Null };
|
|
386
214
|
}
|
|
387
|
-
|
|
215
|
+
/**
|
|
216
|
+
* <----
|
|
217
|
+
*/
|
|
388
218
|
|
|
389
|
-
this
|
|
219
|
+
schemaType = this.schemaUtils.getInternalSchemaType(this.schema);
|
|
390
220
|
|
|
391
|
-
|
|
392
|
-
parsedSchema = this.baseSchemaParsers[schemaType](schema, typeName);
|
|
393
|
-
schema.$parsed = this.config.hooks.onParseSchema(schema, parsedSchema) || parsedSchema;
|
|
221
|
+
this.schemaPath.push(this.typeName);
|
|
394
222
|
|
|
395
|
-
|
|
223
|
+
_.merge(
|
|
224
|
+
this.schema,
|
|
225
|
+
this.config.hooks.onPreParseSchema(
|
|
226
|
+
this.schema,
|
|
227
|
+
this.typeName,
|
|
228
|
+
schemaType,
|
|
229
|
+
),
|
|
230
|
+
);
|
|
231
|
+
parsedSchema = this._baseSchemaParsers[schemaType](
|
|
232
|
+
this.schema,
|
|
233
|
+
this.typeName,
|
|
234
|
+
);
|
|
235
|
+
this.schema.$parsed =
|
|
236
|
+
this.config.hooks.onParseSchema(this.schema, parsedSchema) ||
|
|
237
|
+
parsedSchema;
|
|
238
|
+
|
|
239
|
+
if (
|
|
240
|
+
this.config.sortTypes &&
|
|
241
|
+
Array.isArray(this.schema.$parsed?.content)
|
|
242
|
+
) {
|
|
243
|
+
this.schema.$parsed.content = this.schema.$parsed.content.sort(
|
|
244
|
+
sortByProperty('name'),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
396
247
|
}
|
|
397
248
|
|
|
398
|
-
|
|
399
|
-
};
|
|
249
|
+
this.schemaPath.pop();
|
|
400
250
|
|
|
401
|
-
|
|
402
|
-
const parsedSchema = this.parseSchema(rawTypeData, typeName);
|
|
403
|
-
const formattedSchema = this.schemaFormatters.formatSchema(parsedSchema, "inline");
|
|
404
|
-
return formattedSchema.content;
|
|
251
|
+
return this.schema.$parsed;
|
|
405
252
|
};
|
|
406
253
|
|
|
407
|
-
|
|
408
|
-
const parsedSchema = this.parseSchema(
|
|
409
|
-
const formattedSchema = this.schemaFormatters.formatSchema(
|
|
254
|
+
getInlineParseContent = () => {
|
|
255
|
+
const parsedSchema = this.parseSchema();
|
|
256
|
+
const formattedSchema = this.schemaFormatters.formatSchema(
|
|
257
|
+
parsedSchema,
|
|
258
|
+
'inline',
|
|
259
|
+
);
|
|
410
260
|
return formattedSchema.content;
|
|
411
261
|
};
|
|
412
262
|
|
|
413
|
-
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
263
|
+
getParseContent = () => {
|
|
264
|
+
const parsedSchema = this.parseSchema();
|
|
265
|
+
const formattedSchema = this.schemaFormatters.formatSchema(
|
|
266
|
+
parsedSchema,
|
|
267
|
+
'base',
|
|
268
|
+
);
|
|
269
|
+
return formattedSchema.content;
|
|
419
270
|
};
|
|
420
271
|
}
|
|
421
272
|
|