Skip to content

Commit

Permalink
Added tagging paths loaded from external files.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeLenny committed Apr 3, 2017
1 parent e708f55 commit 55f36ed
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ module.exports = function(options, specData) {

return copy;
}

module.exports.httpMethods = httpMethods;
2 changes: 1 addition & 1 deletion app/lib/reference-contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function definition(ref) {
*/
function path(ref) {
var parts = ref.split("/");
return parts.length === 2 && parts.lastIndexOf("paths") === parts.length - 2 && parts[1].length > 0;
return parts.length === 3 && parts.lastIndexOf("paths") === parts.length - 3 && parts[1].length > 0;
}

module.exports = {
Expand Down
29 changes: 29 additions & 0 deletions app/lib/resolve-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var fs = require("fs");
var path = require("path");
var yaml = require("js-yaml");
var request = require("request-sync");
var _ = require("lodash");
var pathUtils = require("./urls");
var contexts = require("./reference-contexts");
var resolveLocal = require("./json-reference").resolveLocal;
Expand All @@ -18,6 +19,11 @@ var TreeWalkError = require("./errors").TreeWalkError;
*/
var _cache = {};

/**
* The list of avalible HTTP methods.
*/
var httpMethods = require("./preprocessor").httpMethods;

/**
* Determines if a reference is relative to the current file.
* @param {string} ref The file path or URL referenced.
Expand Down Expand Up @@ -101,6 +107,29 @@ function replaceReference(cwd, top, obj, context) {
if(!top.definitions[external]) { top.definitions[external] = referenced; }
Object.assign(obj, { "$ref": "#/definitions/"+external.replace("/", "%2F") });
}
else if(contexts.path(context)) {
Object.keys(referenced).forEach(function(method) {
if(httpMethods.indexOf(method) < 0) {
delete path[method];
return;
}
var operation = referenced[method];
operation.method = method;
var operationTags = operation.tags || ["default"];
operationTags.forEach(function(tag) {
top.tags = top.tags || [];
var tagDef = _.find(top.tags, {name: tag});
if(!tagDef) {
tagDef = {name: tag, operations: []};
top.tags.push(tagDef);
}
tagDef.operations = tagDef.operations || [];
tagDef.operations.push(operation);
});
});
Object.assign(obj, referenced);
delete obj.$ref;
}
else {
Object.assign(obj, referenced);
delete obj.$ref;
Expand Down
7 changes: 4 additions & 3 deletions test/test-reference-contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ describe("reference-contexts.js", function() {
describe("path()", function() {

success("path", [
"paths/index.html",
"paths/{part}",
"paths/{a}-{b}-{c}",
"paths/index.html/",
"paths/{part}/",
"paths/{a}-{b}-{c}/",
]);

failure("path", [
"paths/index.html",
"paths/",
"paths",
"paths/index.html/responses/",
Expand Down
18 changes: 18 additions & 0 deletions test/test-resolve-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ describe("resolve-references.js", function() {
});

networkIt("should resolve deep references", function() {
this.timeout(4000);
top = Object.create(minimal);
top["x-spec-path"] = cwd + "/test.json";
top.info = {"$ref": rawgit+"deep-reference.json"};
Expand Down Expand Up @@ -232,6 +233,23 @@ describe("resolve-references.js", function() {
top.definitions.should.have.property("fixtures/User.yml");
});

it("adds tags when given a remote path", function() {
top = Object.create(minimal);
top.paths = {
"/": { "$ref": "fixtures/basic-path.yaml" },
};
res.replaceReference(cwd, top, top.paths["/"], "paths/%2F/");
top.should.have.property("tags");
top.tags.should.be.an.array;
top.tags.length.should.equal(1);
var tag = top.tags[0];
tag.should.have.property("name", "default");
tag.should.have.property("operations");
tag.operations.should.be.an.array;
tag.operations.should.include(top.paths["/"].get);
tag.operations.should.include(top.paths["/"].post);
});

});

});
Expand Down

0 comments on commit 55f36ed

Please sign in to comment.