Skip to content

Commit

Permalink
fix(generatePaths): custom swagger.actions.action2Name.responses type…
Browse files Browse the repository at this point in the history
…s should override action2.exits types
  • Loading branch information
theoomoregbee committed May 18, 2020
1 parent d0fd537 commit 365d934
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
2 changes: 0 additions & 2 deletions api/controllers/subdir/actions2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ module.exports = {
},

exits: {
success: {
},
notFound: {
description: 'No user with the specified ID was found in the database',
responseType: 'notFound',
Expand Down
36 changes: 9 additions & 27 deletions lib/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,42 +477,24 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
if (action2.exits) {
const exitResponses: NameKeyMap<OpenApi.Response> = {};

// actions2 may specify more than one 'exit' per 'statusCode' --> use oneOf
forEach(action2.exits, (exit, exitName) => {
const hasDefaults = actions2Responses[exitName as keyof Action2Response];
if (!hasDefaults) {
return
}
const { statusCode = hasDefaults.statusCode, description = hasDefaults.description } = exit

if (exitResponses[statusCode]) {
const arr = get(pathEntry, ['responses', statusCode, 'content', 'application/json', 'schema', 'oneOf'], []);
arr.push({ type: 'json', description: description });
} else {
exitResponses[statusCode] = {
description: description,
content: {
'application/json': {
schema: { oneOf: [{ type: 'object', description: description }] }
}
}
};
}
});

// remove oneOf for single entries, otherwise summarise
forEach(exitResponses, resp => {
const arr = get(resp, ['content', 'application/json', 'schema', 'oneOf'], []);
if (arr.length === 1) {
set(resp, ['content', 'application/json', 'schema'], arr[0]);
} else {
resp.description = `${arr.length} alternative responses`;
}
const { statusCode = hasDefaults.statusCode, description = hasDefaults.description, headers, content, links } = exit

exitResponses[statusCode] = {
description: description,
content,
headers,
links
};
});

pathEntry.responses = {
...exitResponses,
...pathEntry.responses,
...exitResponses
}
forEach(pathEntry.responses, (v, k) => { // ensure description
if (!v.description) v.description = get(exitResponses, [k, 'description'], '-');
Expand Down
28 changes: 6 additions & 22 deletions test/fixtures/generatedSwagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -612,37 +612,21 @@
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "Success"
"type": "number",
"default": 123
}
}
}
},
"description": "Done."
},
"404": {
"description": "No user with the specified ID was found in the database",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "No user with the specified ID was found in the database"
}
}
}
"description": "No user with the specified ID was found in the database"
},
"500": {
"description": "An unexpected error occurred.",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "An unexpected error occurred."
}
}
}
"description": "An unexpected error occurred."
}
},
"tags": [
Expand Down

0 comments on commit 365d934

Please sign in to comment.