Skip to content

Commit

Permalink
fix: Cover more oneOf-transformation cases (#44)
Browse files Browse the repository at this point in the history
PR: #44
  • Loading branch information
osama-salman99 authored Aug 14, 2023
1 parent f49ba3f commit 78c58b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
27 changes: 27 additions & 0 deletions src/model/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,39 @@ export default class Spec {
for (const cmp in allOfComponents) {
const refs = allOfComponents[cmp].allOf.filter((item: any) => item.$ref).map((item: any) => this.getRefComponentName(item));
refs.forEach((ref: string) => {
if (!this.isDefinedInParent(cmp, ref)) return;
/* istanbul ignore next */
children.has(ref) ? children.set(ref, [...(children.get(ref) || []), cmp]) : children.set(ref, [cmp]);
});
}
}

private isDefinedInParent = (child: string, parent: string): boolean => {
const extractComponentName = (ref: string): string => ref.slice(ref.lastIndexOf('/') + 1)

const discriminatorMappings: string[] = this.getDiscriminatorMappings(parent);
return !!discriminatorMappings.map((item: string) => extractComponentName(item))
.find((value: string): boolean => value === child);
}

private getDiscriminatorMappings = (componentName: string): string[] => {
const schema = this.specs.components.schemas[componentName];
let mappings: string[] = [];
const discriminatorMapping = schema.discriminator?.mapping;
if (discriminatorMapping) {
mappings = mappings.concat(Object.values(discriminatorMapping));
} else if (schema.allOf) {
mappings = mappings.concat(
schema.allOf.filter(
(item: Value) => item.discriminator?.mapping
).map(
(item: Value) => Object.values(item.discriminator.mapping)
).flat()
);
}
return mappings;
}

private getRefComponentName = (item: any) => item.$ref.slice(item.$ref.lastIndexOf('/') + 1);

private mapToLeaves = (children: Map<string, string[]>) => {
Expand Down
29 changes: 22 additions & 7 deletions test/transformer/OneOfSettingTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ describe(' test OneOfSettingTransformer', () => {
},
Visa: {
title: 'Visa',
discriminator: {
propertyName: 'type',
mapping: {
DEBIT: 'Debit',
CREDIT: 'Credit'
}
},
allOf: [
{
$ref: '#/components/schemas/CreditCard'
},
{
discriminator: {
propertyName: 'type',
mapping: {
DEBIT: 'Debit',
CREDIT: 'Credit'
}
},
properties: {
type: {
enum: ['VISA']
Expand Down Expand Up @@ -144,6 +144,21 @@ describe(' test OneOfSettingTransformer', () => {
}
]
},
SomeCreditCard: {
title: 'SomeCreditCard',
allOf: [
{
$ref: '#/components/schemas/CreditCard'
},
{
properties: {
name: {
type: 'string'
}
}
}
]
},
Dummy: {
title: 'Dummy',
type: 'object',
Expand Down

0 comments on commit 78c58b2

Please sign in to comment.