Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(anchors): applied a fix for anchors living in seperate files with… #256

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion examples/yaml-anchors-aliases/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,46 @@ module.exports = (app) => {
* /aws:
* get:
* summary: sample aws-specific route
* description: contains a reference outside this file
* description: contains a reference outside this file, and pulls response params from multiple files.
* security: []
* responses:
* 200:
* description: OK
* schema:
* type: object
* properties:
* id: *id
* username: *username
*
* x-amazon-apigateway-integration: *default-integration
* x-second-integration: *second-integration
*/
app.get('/aws', () => {});

/**
* @swagger
* /aws:
* post:
* summary: sample aws-specific route
* description: contains a reference outside this file, and pulls response params from multiple files.
* security: []
* parameters:
* - in : body
* name: user
* description: the request body for a fictional request that re-uses anchors referenced else where
* schema:
* type: object
* required:
* - username
* - id
* properties:
* username: *username
* id: *id
* responses:
* 200:
* description: OK
* x-amazon-apigateway-integration: *default-integration
* x-second-integration: *second-integration
*/
app.post('/aws', () => {});
};
4 changes: 4 additions & 0 deletions examples/yaml-anchors-aliases/properties/id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x-template:
id: &id
type: integer
description: The user ID.
6 changes: 6 additions & 0 deletions examples/yaml-anchors-aliases/properties/username.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x-template:
username: &username
type: string
description: The user name.
minimum: 1
maximum: 30
62 changes: 61 additions & 1 deletion examples/yaml-anchors-aliases/reference-specification.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,68 @@
"/aws": {
"get": {
"summary": "sample aws-specific route",
"description": "contains a reference outside this file",
"description": "contains a reference outside this file, and pulls response params from multiple files.",
"security": [],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"properties": {
"id": { "type": "integer", "description": "The user ID." },
"username": {
"type": "string",
"description": "The user name.",
"minimum": 1,
"maximum": 30
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "object",
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"passthroughBehavior": "when_no_match",
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789:function:helloworldlambda/invocations"
}
},
"x-second-integration": {
"type": "object",
"x-amazon-apigateway-integration": {
"httpMethod": "GET",
"passthroughBehavior": "when_no_match",
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789:function:helloworldlambda/invocations"
}
}
},
"post": {
"summary": "sample aws-specific route",
"description": "contains a reference outside this file, and pulls response params from multiple files.",
"security": [],
"parameters": [
{
"in": "body",
"name": "user",
"description": "the request body for a fictional request that re-uses anchors referenced else where",
"schema": {
"type": "object",
"required": ["username", "id"],
"properties": {
"username": {
"type": "string",
"description": "The user name.",
"minimum": 1,
"maximum": 30
},
"id": { "type": "integer", "description": "The user ID." }
}
}
}
],
"responses": { "200": { "description": "OK" } },
"x-amazon-apigateway-integration": {
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions examples/yaml-anchors-aliases/yaml-anchors-aliases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Example for using anchors and aliases in YAML documents', () => {
},
apis: [
'./examples/yaml-anchors-aliases/x-amazon-apigateway-integrations.yaml',
'./examples/yaml-anchors-aliases/properties/*.yml',
'./examples/yaml-anchors-aliases/example.js',
],
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"doctrine": "3.0.0",
"github-changes": "^2.0.2",
"glob": "7.1.6",
"lodash.mergewith": "^4.6.2",
"swagger-parser": "10.0.2",
"yaml": "2.0.0-1"
},
Expand Down
20 changes: 9 additions & 11 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
hasEmptyProperty,
convertGlobPaths,
extractAnnotations,
mergeDeep,
extractYamlFromJsDoc,
isTagPresentInTags,
} = require('./utils');
Expand Down Expand Up @@ -95,7 +96,6 @@ function clean(swaggerObject) {
*/
function finalize(swaggerObject, options) {
let specification = swaggerObject;

parser.parse(swaggerObject, (err, api) => {
if (!err) {
specification = api;
Expand Down Expand Up @@ -137,13 +137,12 @@ function organize(swaggerObject, annotation, property) {
'parameters',
'definitions',
];

if (commonProperties.includes(property)) {
for (const definition of Object.keys(annotation[property])) {
swaggerObject[property][definition] = {
...swaggerObject[property][definition],
...annotation[property][definition],
};
swaggerObject[property][definition] = mergeDeep(
swaggerObject[property][definition],
annotation[property][definition]
);
}
} else if (property === 'tags') {
const { tags } = annotation;
Expand All @@ -159,10 +158,10 @@ function organize(swaggerObject, annotation, property) {
}
} else {
// Paths which are not defined as "paths" property, starting with a slash "/"
swaggerObject.paths[property] = {
...swaggerObject.paths[property],
...annotation[property],
};
swaggerObject.paths[property] = mergeDeep(
swaggerObject.paths[property],
annotation[property]
);
}
}

Expand Down Expand Up @@ -236,7 +235,6 @@ function build(options) {
.filter((a) => a)
.join('')
.split(' at line')[0];

const anchor = yamlDocsAnchors.get(refErr);
const anchorString = anchor.cstNode.toString();
const originalString = docWithErr.cstNode.toString();
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const mergeWith = require('lodash.mergewith');

/**
* Converts an array of globs to full paths
Expand Down Expand Up @@ -130,6 +131,17 @@ function loadDefinition(defPath, swaggerDefinition) {
return loader();
}

/**
* A recursive deep-merge that ignores null values when merging.
* This returns the merged object and does not mutate.
* @param {object} first the first object to get merged
* @param {object} second the second object to get merged
*/
function mergeDeep(first, second) {
return mergeWith({}, first, second, (a, b) => (b === null ? a : undefined));
}

module.exports.mergeDeep = mergeDeep;
module.exports.convertGlobPaths = convertGlobPaths;
module.exports.hasEmptyProperty = hasEmptyProperty;
module.exports.extractYamlFromJsDoc = extractYamlFromJsDoc;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3661,6 +3661,11 @@ lodash.isequal@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=

lodash.mergewith@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down