Skip to content

Commit

Permalink
fix: cover more one-of transformation cases (#42)
Browse files Browse the repository at this point in the history
PR: #42
  • Loading branch information
osama-salman99 authored Aug 8, 2023
1 parent 582fa4d commit 33b78fb
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/model/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,23 @@ 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,
content: {
...operationFieldValue.content,
'application/json': {
...operationFields.requestBody.content['application/json'],
schema: {
oneOf: this.buildOneOfList(mappings.get(name))
}
schema: schema
}
}
};
Expand All @@ -199,31 +200,43 @@ 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,
content: {
...responseFields.content,
'application/json': {
...responseFields.content['application/json'],
schema: {
oneOf: this.buildOneOfList(mappings.get(name))
}
schema: schema
}
}
}];
});
}

private buildOneOfSchema(schemaValue: Value, mappings: Record<Key, Value>): Record<Key, Value> {
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<Key, Value>): Record<Key, Value> {
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);
Expand Down Expand Up @@ -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<Key, Value>) => parameter.in !== HEADER || !this.includesIgnoreCase(headersToRemove, parameter.name)
Expand Down
153 changes: 153 additions & 0 deletions test/transformer/OneOfSettingTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -305,6 +369,19 @@ describe(' test OneOfSettingTransformer', () => {
}
}
}
},
'404': {
description: 'Not Found',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/PaymentMethod'
}
}
}
}
}
}
}
Expand Down Expand Up @@ -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'
}
]
}
}
}
}
}
}
}
Expand Down Expand Up @@ -415,6 +518,19 @@ describe(' test OneOfSettingTransformer', () => {
}
}
}
},
'201': {
description: 'Created',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Dummy'
}
}
}
}
}
}
}
Expand Down Expand Up @@ -458,6 +574,12 @@ describe(' test OneOfSettingTransformer', () => {
},
paymentMethod: {
$ref: '#/components/schemas/PaymentMethod'
},
paymentMethods: {
type: 'array',
items: {
$ref: '#/components/schemas/PaymentMethod'
}
}
}
}
Expand Down Expand Up @@ -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'
}
]
}
}
}
}
Expand Down Expand Up @@ -551,6 +692,12 @@ describe(' test OneOfSettingTransformer', () => {
},
paymentMethod: {
$ref: '#/components/schemas/Dummy'
},
dummies: {
type: 'array',
items: {
$ref: '#/components/schemas/Dummy'
}
}
}
}
Expand Down Expand Up @@ -589,6 +736,12 @@ describe(' test OneOfSettingTransformer', () => {
},
paymentMethod: {
$ref: '#/components/schemas/Dummy'
},
dummies: {
type: 'array',
items: {
$ref: '#/components/schemas/Dummy'
}
}
}
}
Expand Down

0 comments on commit 33b78fb

Please sign in to comment.