diff --git a/src/model/Spec.ts b/src/model/Spec.ts index 864557a..95501f8 100644 --- a/src/model/Spec.ts +++ b/src/model/Spec.ts @@ -169,12 +169,15 @@ export default class Spec { const isDirectRef = this.isRef(operationFieldValue); const schemaValue = isDirectRef ? operationFieldValue : operationFieldValue.content['application/json'].schema; - if (!this.isRef(schemaValue)) return operationFieldValue; - - const name = this.getRefComponentName(schemaValue); - const hasChildren = mappings.has(name); - - if (!hasChildren) return operationFieldValue; + let schema; + if (this.isRef(schemaValue)) { + schema = this.buildOneOfSchema(schemaValue, mappings) + } else if (this.containsItems(schemaValue)) { + schema = { + ...schemaValue, + items: this.buildOneOfSchema(schemaValue.items, mappings) + } + } else return operationFieldValue; return { ...operationFieldValue, @@ -182,9 +185,7 @@ export default class Spec { ...operationFieldValue.content, 'application/json': { ...operationFields.requestBody.content['application/json'], - schema: { - oneOf: this.buildOneOfList(mappings.get(name)) - } + schema: schema } } }; @@ -199,12 +200,15 @@ export default class Spec { const schemaValue = isDirectRef ? responseFields : responseFields.content['application/json'].schema; - if (!this.isRef(schemaValue)) return [response, responseFields]; - - const name = this.getRefComponentName(schemaValue); - const hasChildren = mappings.has(name); - - if (!hasChildren) return [response, responseFields]; + let schema; + if (this.isRef(schemaValue)) { + schema = this.buildOneOfSchema(schemaValue, mappings) + } else if (this.containsItems(schemaValue)) { + schema = { + ...schemaValue, + items: this.buildOneOfSchema(schemaValue.items, mappings) + } + } else return [response, responseFields]; return [response, { ...responseFields, @@ -212,18 +216,27 @@ export default class Spec { ...responseFields.content, 'application/json': { ...responseFields.content['application/json'], - schema: { - oneOf: this.buildOneOfList(mappings.get(name)) - } + schema: schema } } }]; }); } + private buildOneOfSchema(schemaValue: Value, mappings: Record): Record { + const name = this.getRefComponentName(schemaValue); + const hasChildren = mappings.has(name); + + if (!hasChildren) return schemaValue; + + return { + oneOf: this.buildOneOfList(mappings.get(name)) + } + } + private buildOneOfProperty(properties: Value, mappings: Record): Record { const updateProperty = (value: Value) => { - if (!this.isRef(value)) return value; + if (!this.isRef(value)) return this.containsItems(value) ? this.buildOneOfProperty(value, mappings) : value; const name = this.getRefComponentName(value); const hasChildren = mappings.has(name); @@ -289,6 +302,8 @@ export default class Spec { private isRef = (value: Value): boolean => value && value.hasOwnProperty('$ref'); + private containsItems = (value: Value): boolean => value && value.hasOwnProperty('items'); + private filterHeaderParameters = (parameters: Value, headersToRemove: string[]) => { return parameters.filter( (parameter: Record) => parameter.in !== HEADER || !this.includesIgnoreCase(headersToRemove, parameter.name) diff --git a/test/transformer/OneOfSettingTransformer.test.ts b/test/transformer/OneOfSettingTransformer.test.ts index 6fa5c43..ec459ba 100644 --- a/test/transformer/OneOfSettingTransformer.test.ts +++ b/test/transformer/OneOfSettingTransformer.test.ts @@ -177,6 +177,23 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + '/another/test': { + post: { + requestBody: { + required: true, + content: { + 'application/json': { + schema: { + type: 'array', + items: { + $ref: '#/components/schemas/PaymentMethod' + } + } + } + } + } + } } }, ...components @@ -212,6 +229,36 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + '/another/test': { + post: { + requestBody: { + required: true, + content: { + 'application/json': { + schema: { + type: 'array', + items: { + oneOf: [ + { + $ref: '#/components/schemas/PayPal' + }, + { + $ref: '#/components/schemas/Mastercard' + }, + { + $ref: '#/components/schemas/Debit' + }, + { + $ref: '#/components/schemas/Credit' + } + ] + } + } + } + } + } + } } }, ...components @@ -270,6 +317,23 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + '/another/test': { + post: { + requestBody: { + required: true, + content: { + 'application/json': { + schema: { + type: 'array', + items: { + $ref: '#/components/schemas/Dummy' + } + } + } + } + } + } } }, ...components @@ -305,6 +369,19 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + '404': { + description: 'Not Found', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + $ref: '#/components/schemas/PaymentMethod' + } + } + } + } } } } @@ -352,6 +429,32 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + 404: { + description: 'Not Found', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + oneOf: [ + { + $ref: '#/components/schemas/PayPal' + }, + { + $ref: '#/components/schemas/Mastercard' + }, + { + $ref: '#/components/schemas/Debit' + }, + { + $ref: '#/components/schemas/Credit' + } + ] + } + } + } + } } } } @@ -415,6 +518,19 @@ describe(' test OneOfSettingTransformer', () => { } } } + }, + '201': { + description: 'Created', + content: { + 'application/json': { + schema: { + type: 'array', + items: { + $ref: '#/components/schemas/Dummy' + } + } + } + } } } } @@ -458,6 +574,12 @@ describe(' test OneOfSettingTransformer', () => { }, paymentMethod: { $ref: '#/components/schemas/PaymentMethod' + }, + paymentMethods: { + type: 'array', + items: { + $ref: '#/components/schemas/PaymentMethod' + } } } } @@ -509,6 +631,25 @@ describe(' test OneOfSettingTransformer', () => { '$ref': '#/components/schemas/Credit' } ] + }, + paymentMethods: { + type: 'array', + items: { + 'oneOf': [ + { + '$ref': '#/components/schemas/PayPal' + }, + { + '$ref': '#/components/schemas/Mastercard' + }, + { + '$ref': '#/components/schemas/Debit' + }, + { + '$ref': '#/components/schemas/Credit' + } + ] + } } } } @@ -551,6 +692,12 @@ describe(' test OneOfSettingTransformer', () => { }, paymentMethod: { $ref: '#/components/schemas/Dummy' + }, + dummies: { + type: 'array', + items: { + $ref: '#/components/schemas/Dummy' + } } } } @@ -589,6 +736,12 @@ describe(' test OneOfSettingTransformer', () => { }, paymentMethod: { $ref: '#/components/schemas/Dummy' + }, + dummies: { + type: 'array', + items: { + $ref: '#/components/schemas/Dummy' + } } } }