Skip to content

Commit 1366ece

Browse files
committed
fix: problem with incorrect resolving type name of discriminator mapping types data contracts
1 parent 0813fc4 commit 1366ece

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ feat: `const` keyword OpenAPI 3.0 draft
44
fix: problem with using `anyOf`
55
feat: `--extract-responses` (nodejs: `extractResponses`) option to extract all schemas from `/components/responses`
66
fix: discriminator and mapping with invalid discriminator property name (#551)
7+
fix: problem with incorrect resolving type name of discriminator mapping types data contracts
78

89
## 13.0.0
910

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
7474
});
7575

7676
if (ableToCreateMappingType) {
77-
mappingTypeName = this.schemaUtils.resolveTypeName(
78-
`${abstractSchemaStruct.typeName} ${discriminator.propertyName}`,
79-
{
80-
suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
81-
resolver:
82-
this.config.extractingOptions.discriminatorMappingNameResolver,
83-
},
84-
);
85-
this.schemaParserFabric.createSchema({
86-
linkedComponent: this.schemaComponentsMap.createComponent(
87-
this.schemaComponentsMap.createRef([
88-
'components',
89-
'schemas',
90-
mappingTypeName,
91-
]),
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+
},
9287
),
88+
]);
89+
const mappingTypeNameComponent =
90+
this.schemaComponentsMap.createComponent(mappingTypeNameRef);
91+
const mappingTypeNameSchema = this.schemaParserFabric.createSchema({
92+
linkedComponent: mappingTypeNameComponent,
9393
content: ts.IntersectionType([
9494
ts.ObjectWrapper(
9595
ts.TypeField({
@@ -102,6 +102,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
102102
genericArgs: [{ name: 'Key' }, { name: 'Type' }],
103103
internal: true,
104104
});
105+
106+
mappingTypeName = mappingTypeNameSchema.typeData.name;
105107
}
106108

107109
/** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */

src/type-name-formatter.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ class TypeNameFormatter {
4141
? this.config.enumKeySuffix
4242
: this.config.typeSuffix;
4343

44-
const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
44+
const existedFormattedEntry = Array.from(
45+
this.formattedModelNamesMap.entries(),
46+
).find((entry) => entry[1] === name);
47+
48+
const hashKey = existedFormattedEntry
49+
? existedFormattedEntry[0]
50+
: `${typePrefix}_${name}_${typeSuffix}`;
4551

4652
if (typeof name !== 'string') {
4753
this.logger.warn('wrong name of the model name', name);

0 commit comments

Comments
 (0)