Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
refactor(samplers): strengthen the checks of keywords (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk committed Nov 3, 2019
1 parent 5e18fd6 commit 6c50a8c
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 104 deletions.
30 changes: 1 addition & 29 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,5 @@ jobs:
- run: npm run build
- run: npm run semantic-release
env:
NPM_TOKEN: ${{secrets.npm_token}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

publish-npm:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-gpr:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
scope: '@NeuraLegion'
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ test

commitlint.config.js
release.config.js
.idea
132 changes: 131 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neuralegion/openapi-sampler",
"version": "0.5.0",
"version": "0.6.0",
"private": false,
"description": "Tool for generation samples based on OpenAPI payload/response schema",
"main": "dist/openapi-sampler.js",
Expand All @@ -20,14 +20,20 @@
"type": "git",
"url": "git+https://github.com/Neuralegion/openapi-sampler.git"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"OpenAPI",
"Swagger",
"instantiator",
"sampler",
"faker"
],
"author": "Mirsad Halilcevic <[email protected]>",
"contributors": [
"Mirsad Halilcevic <[email protected]>",
"Artem Derevnjuk <[email protected]>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Neuralegion/openapi-sampler/issues"
Expand All @@ -40,6 +46,7 @@
"@babel/register": "^7.4.4",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@semantic-release/git": "^7.0.17",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"babel-plugin-istanbul": "^5.1.4",
Expand Down Expand Up @@ -98,7 +105,7 @@
}
},
"lint-staged": {
"*.js": [
"src/**/*.js": [
"eslint --fix",
"git add"
]
Expand Down
18 changes: 8 additions & 10 deletions release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module.exports = {
{
parserOpts: {
noteKeywords: ['BREAKING CHANGE', 'BREAKING CHANGES']
}
},
releaseRules: [
{type: 'refactor', release: 'patch'}
]
}
],
[
Expand All @@ -16,16 +19,11 @@ module.exports = {
}
}
],
'@semantic-release/npm',
[
'@semantic-release/npm',
{
npmPublish: false
}
],
[
'@semantic-release/github',
{
assets: ['dist/**']
'@semantic-release/git', {
assets: ['dist/**', 'src/**', 'package.json', 'package-lock.json', 'README.md'],
message: 'chore(release): ${nextRelease.version} [skip ci]'
}
]
],
Expand Down
23 changes: 16 additions & 7 deletions src/allOf.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
'use strict';

import { traverse } from './traverse';
import { mergeDeep } from './utils';

export function allOfSample(into, children, options, spec) {
let res = traverse(into, options, spec);
const res = traverse(into, options, spec);

const subSamples = [];

for (let subSchema of children) {
const { type, readOnly, writeOnly, value } = traverse({ type, ...subSchema }, options, spec);

if (res.type && type && type !== res.type) {
throw new Error('allOf: schemas with different types can\'t be merged');
}

res.type = res.type || type;
res.readOnly = res.readOnly || readOnly;
res.writeOnly = res.writeOnly || writeOnly;
if (value != null) subSamples.push(value);

if (value != null) {
subSamples.push(value);
}
}

if (res.type === 'object') {
res.value = mergeDeep(res.value || {}, ...subSamples);
return res;
} else {
if (res.type === 'array') {
// TODO: implement arrays
if (!options.quiet) console.warn('OpenAPI Sampler: found allOf with "array" type. Result may be incorrect');
// TODO: implement arrays
if (res.type === 'array' && !options.quiet) {
console.warn('OpenAPI Sampler: found allOf with "array" type. Result may be incorrect');
}

const lastSample = subSamples[subSamples.length - 1];
res.value = lastSample != null ? lastSample : res.value;
return res;
}

return res;
}
15 changes: 10 additions & 5 deletions src/infer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const schemaKeywordTypes = {
multipleOf: 'number',
maximum: 'number',
Expand Down Expand Up @@ -25,14 +27,17 @@ const schemaKeywordTypes = {
};

export function inferType(schema) {
if (schema.type !== undefined) {
if (schema.type) {
return schema.type;
}

const keywords = Object.keys(schemaKeywordTypes);
for (var i = 0; i < keywords.length; i++) {
let keyword = keywords[i];
let type = schemaKeywordTypes[keyword];
if (schema[keyword] !== undefined) {

for (let i = 0; i < keywords.length; i++) {
const keyword = keywords[i];
const type = schemaKeywordTypes[keyword];

if (schema[keyword]) {
return type;
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/openapi-sampler.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
'use strict';

import { traverse, clearCache } from './traverse';
import { sampleArray, sampleBoolean, sampleNumber, sampleObject, sampleString } from './samplers/index';
import { sampleArray, sampleBoolean, sampleNumber, sampleNull, sampleObject, sampleString } from './samplers/index';

export var _samplers = {};
export const _samplers = {};

const defaults = {
skipReadOnly: false
};

export function sample(schema, options, spec) {
let opts = Object.assign({}, defaults, options);
const opts = Object.assign({}, defaults, options);
clearCache();
return traverse(schema, opts, spec).value;
};
}

export function _registerSampler(type, sampler) {
_samplers[type] = sampler;
};
}

export { inferType } from './infer';

_registerSampler('array', sampleArray);
_registerSampler('boolean', sampleBoolean);
_registerSampler('null', sampleNull);
_registerSampler('integer', sampleNumber);
_registerSampler('number', sampleNumber);
_registerSampler('object', sampleObject);
Expand Down
Loading

0 comments on commit 6c50a8c

Please sign in to comment.