Skip to content

Commit

Permalink
Handle multiple similar suffixes as a single one
Browse files Browse the repository at this point in the history
Fixes #64
  • Loading branch information
luisfpg committed Feb 19, 2020
1 parent 56c469f commit 5d1e4ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
49 changes: 27 additions & 22 deletions lib/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,36 @@ export class Operation {
});
}

private calculateVariants() {
const hasRequestBodyVariants = this.requestBody && this.requestBody.content.length > 1;
const hasResponseVariants = this.successResponse && this.successResponse.content.length > 1;
const contentOrNull = (hasContent?: { content?: Content[] }): (Content | null)[] => {
if (hasContent) {
const content = hasContent.content;
if (content && content.length > 0) {
return content;
}
}
return [null];
};
const requestBodyVariants = contentOrNull(this.requestBody);
const successResponseVariants = contentOrNull(this.successResponse);
for (const requestBodyVariant of requestBodyVariants) {
const methodPart = this.methodName + (hasRequestBodyVariants ? this.variantMethodPart(requestBodyVariant) : '');
for (const successResponseVariant of successResponseVariants) {
const methodName = methodPart + (hasResponseVariants ? this.variantMethodPart(successResponseVariant) : '');
if (!this.variants.find(v => v.methodName === methodName)) {
// It is possible to have multiple content types which end up in the same method.
// For example: application/json, application/foo-bar+json, text/json ...
this.variants.push(new OperationVariant(this, methodName, requestBodyVariant, successResponseVariant, this.options));
private contentsByMethodPart(hasContent?: { content?: Content[] }): Map<string, Content | null> {
const map = new Map<string, Content | null>();
if (hasContent) {
const content = hasContent.content;
if (content && content.length > 0) {
for (const type of content) {
if (type && type.mediaType) {
map.set(this.variantMethodPart(type), type);
}
}
}
}
if (map.size < 2) {
map.clear();
map.set('', null);
}
return map;
}

private calculateVariants() {
// It is possible to have multiple content types which end up in the same method.
// For example: application/json, application/foo-bar+json, text/json ...
const requestVariants = this.contentsByMethodPart(this.requestBody);
const responseVariants = this.contentsByMethodPart(this.successResponse);
requestVariants.forEach((requestContent, requestPart) => {
responseVariants.forEach((responseContent, responsePart) => {
const methodName = this.methodName + requestPart + responsePart;
this.variants.push(new OperationVariant(this, methodName, requestContent, responseContent, this.options));
});
});
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/all-operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
"$ref": "#/components/schemas/RefObject"
}
},
"application/dummy+json": {
"schema": {
"$ref": "#/components/schemas/RefObject"
}
},
"image/*": {
"schema": {
"type": "string",
Expand Down Expand Up @@ -197,6 +202,14 @@
"type": "string"
}
}
},
"application/*+json": {
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/all-operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('Generation tests using all-operations.json', () => {
expect(success).toBeDefined();
if (success) {
expect(success.statusCode).toBe('200');
expect(success.content.length).toBe(2);
expect(success.content.length).toBe(3);
const json = success.content.find(c => c.mediaType === 'application/json');
expect(json).toBeDefined();
if (json) {
Expand Down

0 comments on commit 5d1e4ba

Please sign in to comment.