Skip to content

Commit

Permalink
add extended configuration options for path remanings: rename / regex…
Browse files Browse the repository at this point in the history
… / function

extend unit and integration tests
update readme.md
#16
  • Loading branch information
ckulbe committed Oct 26, 2017
1 parent 36c5d24 commit 1750887
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 11 deletions.
110 changes: 107 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ Parameters can be filtered by specifying the path and the parameter name as to `
### Renaming Paths

Paths can be renamed by specifying the path to rename and the new path name as key/value pairs in `paths.rename`.
This will replace each key matched by path with the new value.

```json
{
"swagger": "2.0",
"info": {
"title": "Swagger Combine Rename Example",
"title": "Swagger Combine simple Rename Example",
"version": "1.0.0"
},
"apis": [
Expand All @@ -223,9 +224,112 @@ Paths can be renamed by specifying the path to rename and the new path name as k
}
```

Paths can also be replaced by regular expressions and functions.

To configure this, its necessary to use an array like structure instead of in object with key/value pairs to ensure the
order of replacements.

In the `swagger.json` file only renaming and/or a string like regular expression can be used. For regular expression
objects or functions the (swagger)json configuration must be generated by javascript and used as input paramter of
swaggerCombine function.

The next example equals the simple example above but used an extended configuration style.

```json
{
"swagger": "2.0",
"info": {
"title": "Swagger Combine simple Rename Example",
"version": "1.0.0"
},
"apis": [
{
"url": "http://petstore.swagger.io/v2/swagger.json",
"paths": {
"rename": [
{
"type": "rename",
"from": "/pet/{petId}",
"to": "/pet/alive/{petId}"
}
]
}
},
{
"url": "https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.yaml"
}
]
}
```

To change the basePath of all paths a regular expression can be used.

```json
{
"swagger": "2.0",
"info": {
"title": "Swagger Combine Rename by regular expression Example",
"version": "1.0.0"
},
"apis": [
{
"url": "http://petstore.swagger.io/v2/swagger.json",
"paths": {
"rename": [
{
"type": "regex",
"from": "^\/pet\/(.*)",
"to": "/pet/alive/$1"
}
]
}
},
{
"url": "https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.yaml"
}
]
}
```

An example of dynamic generated configuration and renamings with regular expressions and functions.

```javascript
const swaggerJson = {
swagger: "2.0",
info: {
title: "Swagger Combine Rename by regular expression Example",
version: "1.0.0"
},
apis: [
{
url: "http://petstore.swagger.io/v2/swagger.json",
paths: {
rename: [
{
type: "regex",
from: /\/pet\/(.*)/,
to: "/pet/alive/$1"
},
{
type: "function",
to: (path) => path === "/pet/alive/{petId}" ? "/pet/alive/{petAliveId}" : path
}
]
}
},
{
url: "https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.yaml"
}
]
}
swaggerCombine(swaggerJson).
...
```

### Renaming Tags

Tags can be renamed in the same manner as paths, using the `tags.rename` field.
Tags can be renamed in the same manner as paths with simple, object like configuration style, using the `tags.rename` field.

```json
{
Expand Down Expand Up @@ -284,7 +388,7 @@ Tags can be added to all operations in a schema, using the `tags.add` field.

### Renaming Security Definitions

Security definitions can be renamed like paths and tags in the `securityDefinitions.rename` field. All usages of the security definition in the paths are renamed as well.
Security definitions can be renamed like paths (simple) and tags in the `securityDefinitions.rename` field. All usages of the security definition in the paths are renamed as well.

```json
{
Expand Down
46 changes: 46 additions & 0 deletions examples/extendedRename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const swaggerCombine = require('../src');

const config = (module.exports = {
swagger: '2.0',
info: {
title: 'Swagger Combine Rename Example',
version: {
$ref: './package.json#/version',
},
},
apis: [
{
url: 'http://petstore.swagger.io/v2/swagger.json',
paths: {
rename: [
{
type: 'rename',
from: '/pet/{petId}',
to: '/pet/alive/{petId}'
},
{
type: 'regex',
from: /^\/pet(.*)$/,
to: '/animal$1'
},
{
type: 'function',
to: (path) => path.indexOf('{petId}') > -1 ? path.replace('{petId}', '{animalId}') : path
},
],
},
},
{
url: 'https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.yaml',
tags: {
rename: {
Users: 'People',
},
},
},
],
});

if (!module.parent) {
swaggerCombine(config).then(res => console.log(JSON.stringify(res, false, 2))).catch(err => console.error(err));
}
56 changes: 50 additions & 6 deletions src/SwaggerCombine.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,38 @@ class SwaggerCombine {
renamePaths() {
this.schemas = this.schemas.map((schema, idx) => {
if (this.apis[idx].paths && this.apis[idx].paths.rename && Object.keys(this.apis[idx].paths.rename).length > 0) {
_.forIn(this.apis[idx].paths.rename, (renamePath, pathToRename) => {
schema.paths = _.mapKeys(schema.paths, (value, curPath) => {
if (pathToRename === curPath) {
return renamePath;
}
let renamings;

if (_.isPlainObject(this.apis[idx].paths.rename)) {
renamings = [];
_.forIn(this.apis[idx].paths.rename, (renamePath, pathToRename) => {
renamings.push({
type: 'rename',
from: pathToRename,
to: renamePath
});
});
} else {
renamings = this.apis[idx].paths.rename;
}

return curPath;
_.forEach(renamings, (value) => {
schema.paths = _.mapKeys(schema.paths, (curPathValue, curPath) => {
switch (value.type) {
case 'rename':
return this.renamePathByReplace(curPath, value.from, value.to);
break;
case 'regex':
case 'regexp':
return this.renamePathByRegexp(curPath, value.from, value.to);
break;
case 'fnc':
case 'function':
return (value.to || value.from)(curPath);
break;
default:
return curPath;
}
});
});
}
Expand All @@ -166,6 +191,25 @@ class SwaggerCombine {
return this;
}

renamePathByReplace(curPath, pathToRename, renamePath) {
if (pathToRename === curPath) {
return renamePath;
}

return curPath;
}

renamePathByRegexp(curPath, pathToRename, renamePath) {
let regex;
if (_.isRegExp(pathToRename)) {
regex = pathToRename;
} else {
regex = new RegExp(pathToRename);
}

return curPath.replace(regex, renamePath);
}

renameTags() {
this.schemas = this.schemas.map((schema, idx) => {
if (this.apis[idx].tags && this.apis[idx].tags.rename && Object.keys(this.apis[idx].tags.rename).length > 0) {
Expand Down
11 changes: 11 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const addTagsConfig = require('../examples/add-tags');
const basicConfig = require('../examples/basic');
const filterConfig = require('../examples/filter');
const renameConfig = require('../examples/rename');
const extendedRenameConfig = require('../examples/extendedRename');
const securityConfig = require('../examples/security');
const { app, app2 } = require('../examples/middleware');

Expand Down Expand Up @@ -99,6 +100,16 @@ describe('[Integration] SwaggerCombine.js', () => {
expect(schema.paths['/pet/alive/{petId}']).to.be.ok;
}));

it('renames paths (extended)', () =>
swaggerCombine(extendedRenameConfig).then(schema => {
expect(schema.paths).to.not.have.any.keys(
'/pet', '/pet/findByStatus', '/pet/findByTags', '/pet/{petId}', '/pet/{petId}/uploadImage'
);
expect(schema.paths).to.contain.keys(
'/animal', '/animal/findByStatus', '/animal/findByTags', '/animal/alive/{animalId}', '/animal/{animalId}/uploadImage'
);
}));

it('renames tags', () =>
swaggerCombine(renameConfig).then(schema => {
const tags = Object.values(schema.paths).reduce(
Expand Down
Loading

0 comments on commit 1750887

Please sign in to comment.