Skip to content

Commit 5729dcf

Browse files
authored
Merge pull request #560 from acacode/next
Release 13.0.1
2 parents 2d4c74e + 1366ece commit 5729dcf

34 files changed

+581
-131
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
TEST_FILE_NAME=github-swagger.ts
2-
TEST_SCHEMA_VERSION=v3
2+
TEST_SCHEMA_VERSION=v3.0#v2.0|v3.0
33
TEST_WITH_DEBUG=true

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# next release
22

3+
feat: `const` keyword OpenAPI 3.0 draft
4+
fix: problem with using `anyOf`
5+
feat: `--extract-responses` (nodejs: `extractResponses`) option to extract all schemas from `/components/responses`
6+
fix: discriminator and mapping with invalid discriminator property name (#551)
7+
fix: problem with incorrect resolving type name of discriminator mapping types data contracts
8+
39
## 13.0.0
410

511
BREAKING_CHANGE: disable support NodeJS 14.x

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Options:
8181
--debug additional information about processes inside this tool (default: false)
8282
--another-array-type generate array types as Array<Type> (by default Type[]) (default: false)
8383
--sort-types sort fields and types (default: false)
84+
--sort-routes sort routes in alphabetical order (default: false)
85+
--custom-config <string> custom config: primitiveTypeConstructs, hooks, ... (default: "")
8486
--extract-enums extract all enums from inline interface\type content to typescript enum construction (default: false)
8587
-h, --help display help for command
8688
@@ -149,6 +151,8 @@ generateApi({
149151
enumKeyPrefix: '',
150152
enumKeySuffix: '',
151153
addReadonly: false,
154+
sortTypes: false,
155+
sortRouters: false,
152156
extractingOptions: {
153157
requestBodySuffix: ["Payload", "Body", "Input"],
154158
requestParamsSuffix: ["Params"],

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ const program = cli({
114114
default: codeGenBaseConfig.extractResponseError,
115115
internal: { formatter: Boolean },
116116
},
117+
{
118+
flags: '--extract-responses',
119+
description: 'extract all responses described in /components/responses',
120+
default: codeGenBaseConfig.extractResponses,
121+
internal: { formatter: Boolean },
122+
},
117123
{
118124
flags: '--modular',
119125
description:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"test:--extract-response-error": "node tests/spec/extractResponseError/test.js",
4141
"test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
4242
"test:--default-response": "node tests/spec/defaultResponse/test.js",
43+
"test:const-keyword": "node tests/spec/const-keyword/test.js",
4344
"test:--js": "node tests/spec/js/test.js",
4445
"test:jsSingleHttpClientModular": "node tests/spec/jsSingleHttpClientModular/test.js",
4546
"test:--js--axios": "node tests/spec/jsAxios/test.js",

src/code-gen-process.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ class CodeGenProcess {
116116
}),
117117
);
118118

119-
const schemaComponents = this.schemaComponentsMap.filter('schemas');
119+
/**
120+
* @type {SchemaComponent[]}
121+
*/
122+
const componentsToParse = this.schemaComponentsMap.filter(
123+
_.compact(['schemas', this.config.extractResponses && 'responses']),
124+
);
120125

121-
const parsedSchemas = schemaComponents.map((schemaComponent) => {
126+
const parsedSchemas = componentsToParse.map((schemaComponent) => {
122127
const parsed = this.schemaParserFabric.parseSchema(
123128
schemaComponent.rawTypeData,
124129
schemaComponent.typeName,
@@ -234,8 +239,13 @@ class CodeGenProcess {
234239
const components = this.schemaComponentsMap.getComponents();
235240
let modelTypes = [];
236241

242+
const modelTypeComponents = _.compact([
243+
'schemas',
244+
this.config.extractResponses && 'responses',
245+
]);
246+
237247
const getSchemaComponentsCount = () =>
238-
components.filter((c) => c.componentName === 'schemas').length;
248+
this.schemaComponentsMap.filter(...modelTypeComponents).length;
239249

240250
let schemaComponentsCount = getSchemaComponentsCount();
241251
let processedCount = 0;
@@ -244,7 +254,7 @@ class CodeGenProcess {
244254
modelTypes = [];
245255
processedCount = 0;
246256
for (const component of components) {
247-
if (component.componentName === 'schemas') {
257+
if (modelTypeComponents.includes(component.componentName)) {
248258
const modelType = this.prepareModelType(component);
249259
if (modelType) {
250260
modelTypes.push(modelType);

src/configuration.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CodeGenConfig {
7474
extractRequestBody = false;
7575
extractResponseBody = false;
7676
extractResponseError = false;
77+
extractResponses = false;
7778
extractEnums = false;
7879
fileNames = {
7980
dataContracts: 'data-contracts',
@@ -240,7 +241,7 @@ class CodeGenConfig {
240241
/**
241242
* $A
242243
*/
243-
NullValue: (content) => content,
244+
NullValue: (content) => `null`,
244245
/**
245246
* $A1 | $A2
246247
*/
@@ -276,6 +277,9 @@ class CodeGenConfig {
276277
*/
277278
InterfaceDynamicField: (key, value) => `[key: ${key}]: ${value}`,
278279

280+
/**
281+
* EnumName.EnumKey
282+
*/
279283
EnumUsageKey: (enumStruct, key) => `${enumStruct}.${key}`,
280284
/**
281285
* $A1 = $A2

src/schema-components-map.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ class SchemaComponentsMap {
5757
}
5858

5959
/**
60-
* @param componentName {string}
60+
* @params {...string[]} componentNames
6161
* @returns {SchemaComponent[]}
6262
*/
63-
filter(componentName) {
64-
return _.filter(this._data, (v) =>
65-
_.startsWith(v.$ref, `#/components/${componentName}`),
63+
filter(...componentNames) {
64+
return _.filter(this._data, (it) =>
65+
componentNames.some((componentName) =>
66+
_.startsWith(it.$ref, `#/components/${componentName}`),
67+
),
6668
);
6769
}
6870

src/schema-parser/base-schema-parsers/discriminator.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { MonoSchemaParser } = require('../mono-schema-parser');
44

55
class DiscriminatorSchemaParser extends MonoSchemaParser {
66
parse() {
7+
const ts = this.config.Ts;
78
const { discriminator, ...noDiscriminatorSchema } = this.schema;
89

910
if (!discriminator.mapping) {
@@ -27,7 +28,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
2728
abstractSchemaStruct,
2829
});
2930

30-
const schemaContent = this.config.Ts.IntersectionType(
31+
const schemaContent = ts.IntersectionType(
3132
[
3233
abstractSchemaStruct?.content,
3334
discriminatorSchemaStruct?.content,
@@ -40,7 +41,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
4041
$parsedSchema: true,
4142
schemaType: SCHEMA_TYPES.COMPLEX,
4243
type: SCHEMA_TYPES.PRIMITIVE,
43-
typeIdentifier: this.config.Ts.Keyword.Type,
44+
typeIdentifier: ts.Keyword.Type,
4445
name: this.typeName,
4546
description: this.schemaFormatters.formatDescription(
4647
this.schema.description,
@@ -50,6 +51,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
5051
}
5152

5253
createDiscriminatorSchema = ({ skipMappingType, abstractSchemaStruct }) => {
54+
const ts = this.config.Ts;
55+
5356
const refPath = this.schemaComponentsMap.createRef([
5457
'components',
5558
'schemas',
@@ -71,26 +74,26 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
7174
});
7275

7376
if (ableToCreateMappingType) {
74-
mappingTypeName = this.schemaUtils.resolveTypeName(
75-
`${abstractSchemaStruct.typeName} ${discriminator.propertyName}`,
76-
{
77-
suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
78-
resolver:
79-
this.config.extractingOptions.discriminatorMappingNameResolver,
80-
},
81-
);
82-
this.schemaParserFabric.createSchema({
83-
linkedComponent: this.schemaComponentsMap.createComponent(
84-
this.schemaComponentsMap.createRef([
85-
'components',
86-
'schemas',
87-
mappingTypeName,
88-
]),
77+
const mappingTypeNameRef = this.schemaComponentsMap.createRef([
78+
'components',
79+
'schemas',
80+
this.schemaUtils.resolveTypeName(
81+
`${abstractSchemaStruct.typeName} ${discriminator.propertyName}`,
82+
{
83+
suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
84+
resolver:
85+
this.config.extractingOptions.discriminatorMappingNameResolver,
86+
},
8987
),
90-
content: this.config.Ts.IntersectionType([
91-
this.config.Ts.ObjectWrapper(
92-
this.config.Ts.TypeField({
93-
key: discriminator.propertyName,
88+
]);
89+
const mappingTypeNameComponent =
90+
this.schemaComponentsMap.createComponent(mappingTypeNameRef);
91+
const mappingTypeNameSchema = this.schemaParserFabric.createSchema({
92+
linkedComponent: mappingTypeNameComponent,
93+
content: ts.IntersectionType([
94+
ts.ObjectWrapper(
95+
ts.TypeField({
96+
key: ts.StringValue(discriminator.propertyName),
9497
value: 'Key',
9598
}),
9699
),
@@ -99,6 +102,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
99102
genericArgs: [{ name: 'Key' }, { name: 'Type' }],
100103
internal: true,
101104
});
105+
106+
mappingTypeName = mappingTypeNameSchema.typeData.name;
102107
}
103108

104109
/** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */
@@ -112,18 +117,15 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
112117

113118
const mappingUsageKey =
114119
mappingPropertySchemaEnumKeysMap[mappingKey] ||
115-
this.config.Ts.StringValue(mappingKey);
120+
ts.StringValue(mappingKey);
116121

117122
if (ableToCreateMappingType) {
118-
return this.config.Ts.TypeWithGeneric(mappingTypeName, [
119-
mappingUsageKey,
120-
content,
121-
]);
123+
return ts.TypeWithGeneric(mappingTypeName, [mappingUsageKey, content]);
122124
} else {
123-
return this.config.Ts.ExpressionGroup(
124-
this.config.Ts.IntersectionType([
125-
this.config.Ts.ObjectWrapper(
126-
this.config.Ts.TypeField({
125+
return ts.ExpressionGroup(
126+
ts.IntersectionType([
127+
ts.ObjectWrapper(
128+
ts.TypeField({
127129
key: discriminator.propertyName,
128130
value: mappingUsageKey,
129131
}),
@@ -151,9 +153,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
151153

152154
if (skipMappingType) return null;
153155

154-
const content = this.config.Ts.ExpressionGroup(
155-
this.config.Ts.UnionType(mappingContents),
156-
);
156+
const content = ts.ExpressionGroup(ts.UnionType(mappingContents));
157157

158158
return {
159159
content,
@@ -164,6 +164,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
164164
abstractSchemaStruct,
165165
discPropertyName,
166166
}) => {
167+
const ts = this.config.Ts;
168+
167169
let mappingPropertySchemaEnumKeysMap = {};
168170
let mappingPropertySchema = _.get(
169171
abstractSchemaStruct?.component?.rawTypeData,
@@ -183,7 +185,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
183185
(acc, key, index) => {
184186
const enumKey =
185187
mappingPropertySchema.rawTypeData.$parsed.content[index].key;
186-
acc[key] = this.config.Ts.EnumUsageKey(
188+
acc[key] = ts.EnumUsageKey(
187189
mappingPropertySchema.rawTypeData.$parsed.typeName,
188190
enumKey,
189191
);
@@ -284,12 +286,13 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
284286
};
285287

286288
createComplexSchemaStruct = () => {
289+
const ts = this.config.Ts;
287290
const complexType = this.schemaUtils.getComplexType(this.schema);
288291

289292
if (complexType === SCHEMA_TYPES.COMPLEX_UNKNOWN) return null;
290293

291294
return {
292-
content: this.config.Ts.ExpressionGroup(
295+
content: ts.ExpressionGroup(
293296
this.schemaParser._complexSchemaParsers[complexType](this.schema),
294297
),
295298
};

src/schema-parser/complex-schema-parsers/any-of.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { MonoSchemaParser } = require('../mono-schema-parser');
22
const _ = require('lodash');
33

4-
// T1 | T2 | (T1 & T2)
4+
// T1 | T2
55
class AnyOfSchemaParser extends MonoSchemaParser {
66
parse() {
77
const ignoreTypes = [this.config.Ts.Keyword.Any];
@@ -12,20 +12,13 @@ class AnyOfSchemaParser extends MonoSchemaParser {
1212
this.schemaPath,
1313
),
1414
);
15+
1516
const filtered = this.schemaUtils.filterSchemaContents(
1617
combined,
1718
(content) => !ignoreTypes.includes(content),
1819
);
1920

20-
const type = this.config.Ts.UnionType(
21-
_.compact([
22-
...filtered,
23-
filtered.length > 1 &&
24-
this.config.Ts.ExpressionGroup(
25-
this.config.Ts.IntersectionType(filtered),
26-
),
27-
]),
28-
);
21+
const type = this.config.Ts.UnionType(filtered);
2922

3023
return this.schemaUtils.safeAddNullToType(this.schema, type);
3124
}

0 commit comments

Comments
 (0)