Skip to content

Commit eb20042

Browse files
committed
Simplify self mapped discriminator fix in parsers
1 parent 6c3b3e9 commit eb20042

2 files changed

Lines changed: 36 additions & 90 deletions

File tree

packages/shared/src/openApi/3.0.x/parser/schema.ts

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -150,46 +150,6 @@ const getAllDiscriminatorValues = ({
150150
return values;
151151
};
152152

153-
const getMappedDiscriminatorProperty = ({
154-
context,
155-
discriminator,
156-
schema,
157-
schemaRef,
158-
}: {
159-
context: Context;
160-
discriminator: NonNullable<SchemaObject['discriminator']>;
161-
schema: SchemaObject;
162-
schemaRef: string;
163-
}): IR.SchemaObject | undefined => {
164-
const values = getAllDiscriminatorValues({
165-
discriminator,
166-
schemaRef,
167-
});
168-
169-
if (!values.length) {
170-
return;
171-
}
172-
173-
const propertyType = findDiscriminatorPropertyType({
174-
context,
175-
propertyName: discriminator.propertyName,
176-
schemas: [schema],
177-
});
178-
179-
const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map((value) =>
180-
convertDiscriminatorValue(value, propertyType),
181-
);
182-
183-
if (valueSchemas.length === 1) {
184-
return valueSchemas[0];
185-
}
186-
187-
return {
188-
items: valueSchemas,
189-
logicalOperator: 'or',
190-
};
191-
};
192-
193153
const parseSchemaJsDoc = ({
194154
irSchema,
195155
schema,
@@ -418,19 +378,32 @@ const parseObject = ({
418378
}
419379

420380
if (schema.discriminator && state.$ref) {
421-
const discriminatorProperty = getMappedDiscriminatorProperty({
422-
context,
381+
const values = getAllDiscriminatorValues({
423382
discriminator: schema.discriminator,
424-
schema,
425383
schemaRef: state.$ref,
426384
});
427385

428-
if (discriminatorProperty) {
386+
if (values.length) {
387+
const propertyType = findDiscriminatorPropertyType({
388+
context,
389+
propertyName: schema.discriminator.propertyName,
390+
schemas: [schema],
391+
});
392+
const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map((value) =>
393+
convertDiscriminatorValue(value, propertyType),
394+
);
395+
429396
if (!irSchema.properties) {
430397
irSchema.properties = {};
431398
}
432399

433-
irSchema.properties[schema.discriminator.propertyName] = discriminatorProperty;
400+
irSchema.properties[schema.discriminator.propertyName] =
401+
valueSchemas.length > 1
402+
? {
403+
items: valueSchemas,
404+
logicalOperator: 'or',
405+
}
406+
: valueSchemas[0]!;
434407
}
435408
}
436409

packages/shared/src/openApi/3.1.x/parser/schema.ts

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -163,46 +163,6 @@ const getAllDiscriminatorValues = ({
163163
return values;
164164
};
165165

166-
const getMappedDiscriminatorProperty = ({
167-
context,
168-
discriminator,
169-
schema,
170-
schemaRef,
171-
}: {
172-
context: Context;
173-
discriminator: NonNullable<SchemaObject['discriminator']>;
174-
schema: SchemaObject;
175-
schemaRef: string;
176-
}): IR.SchemaObject | undefined => {
177-
const values = getAllDiscriminatorValues({
178-
discriminator,
179-
schemaRef,
180-
});
181-
182-
if (!values.length) {
183-
return;
184-
}
185-
186-
const propertyType = findDiscriminatorPropertyType({
187-
context,
188-
propertyName: discriminator.propertyName,
189-
schemas: [schema],
190-
});
191-
192-
const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map((value) =>
193-
convertDiscriminatorValue(value, propertyType),
194-
);
195-
196-
if (valueSchemas.length === 1) {
197-
return valueSchemas[0];
198-
}
199-
200-
return {
201-
items: valueSchemas,
202-
logicalOperator: 'or',
203-
};
204-
};
205-
206166
const parseSchemaJsDoc = ({
207167
irSchema,
208168
schema,
@@ -513,19 +473,32 @@ const parseObject = ({
513473
}
514474

515475
if (schema.discriminator && state.$ref) {
516-
const discriminatorProperty = getMappedDiscriminatorProperty({
517-
context,
476+
const values = getAllDiscriminatorValues({
518477
discriminator: schema.discriminator,
519-
schema,
520478
schemaRef: state.$ref,
521479
});
522480

523-
if (discriminatorProperty) {
481+
if (values.length) {
482+
const propertyType = findDiscriminatorPropertyType({
483+
context,
484+
propertyName: schema.discriminator.propertyName,
485+
schemas: [schema],
486+
});
487+
const valueSchemas: ReadonlyArray<IR.SchemaObject> = values.map((value) =>
488+
convertDiscriminatorValue(value, propertyType),
489+
);
490+
524491
if (!irSchema.properties) {
525492
irSchema.properties = {};
526493
}
527494

528-
irSchema.properties[schema.discriminator.propertyName] = discriminatorProperty;
495+
irSchema.properties[schema.discriminator.propertyName] =
496+
valueSchemas.length > 1
497+
? {
498+
items: valueSchemas,
499+
logicalOperator: 'or',
500+
}
501+
: valueSchemas[0]!;
529502
}
530503
}
531504

0 commit comments

Comments
 (0)