From 1b5296b12aec07dd422abb77a55760b33e8ac18c Mon Sep 17 00:00:00 2001 From: Rifad Ainun Nazieb Date: Sat, 21 Apr 2018 22:32:54 +0700 Subject: [PATCH] feat(swagger): use response body as sample for definitions --- src/formatters/swagger.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/formatters/swagger.js b/src/formatters/swagger.js index 70ceb24..2752186 100644 --- a/src/formatters/swagger.js +++ b/src/formatters/swagger.js @@ -4,6 +4,7 @@ import Regex from "re.js" import moment from "moment" let securityDefinitions = {}; +let structureExamples = {}; /** * @param blueprint @@ -100,6 +101,17 @@ function getActions(actions) { pathResponse.schema["type"] = "string"; } else { pathResponse.schema = getResponseSchema(response.content); + + if (pathResponse.schema.hasOwnProperty("$ref")) { + const structureName = pathResponse.schema["$ref"].replace("#/definitions/", ""); + + try { + const responseBody = response.body.replace("\\n", ""); + structureExamples[structureName] = JSON.parse(responseBody); + } catch (e) { + // do nothing + } + } } path.responses[response.name] = pathResponse; @@ -429,6 +441,10 @@ function getDefinitions(dataStructures) { definition["properties"] = properties; } + if (structureExamples.hasOwnProperty(definitionName)) { + definition["example"] = structureExamples[definitionName]; + } + result[definitionName] = definition; }