Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-sampler

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-sampler - npm Package Compare versions

Comparing version 1.5.1 to 1.6.0

52

package.json
{
"name": "openapi-sampler",
"version": "1.5.1",
"version": "1.6.0",
"description": "Tool for generation samples based on OpenAPI payload/response schema",

@@ -9,9 +9,9 @@ "main": "dist/openapi-sampler.js",

"scripts": {
"test": "gulp",
"lint": "gulp lint",
"test-browser": "gulp test-browser",
"watch": "gulp watch",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "npm run lint && jest",
"test:watch": "jest --watch",
"coverage": "jest --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"build": "gulp build",
"build-dist": "gulp build",
"coverage": "gulp coverage",
"prepublishOnly": "npm run build"

@@ -25,3 +25,3 @@ },

"type": "git",
"url": "https://github.com/APIs-guru/openapi-sampler.git"
"url": "https://github.com/redocly/openapi-sampler.git"
},

@@ -38,5 +38,5 @@ "keywords": [

"bugs": {
"url": "https://github.com/APIs-guru/openapi-sampler/issues"
"url": "https://github.com/redocly/openapi-sampler/issues"
},
"homepage": "https://github.com/APIs-guru/openapi-sampler/",
"homepage": "https://github.com/redocly/openapi-sampler/",
"browserslist": "> 0.25%, not dead",

@@ -47,13 +47,12 @@ "devDependencies": {

"@babel/register": "^7.7.0",
"@types/jest": "^29.5.12",
"ajv": "^8.1.0",
"ajv-formats": "^2.0.2",
"babel-eslint": "^10.0.3",
"babel-jest": "^29.7.0",
"babel-loader": "^8.0.6",
"babel-plugin-istanbul": "^5.2.0",
"babelify": "^10.0.0",
"browserify": "^16.5.0",
"browserify-istanbul": "^3.0.1",
"chai": "^4.2.0",
"core-js": "^3.4.1",
"coveralls": "^3.0.7",
"coveralls-next": "^4.2.1",
"del": "^5.1.0",

@@ -65,27 +64,8 @@ "eslint": "^7.27.0",

"gulp-filter": "^6.0.0",
"gulp-istanbul": "^1.1.3",
"gulp-livereload": "^4.0.2",
"gulp-load-plugins": "^2.0.1",
"gulp-mocha": "^8.0.0",
"gulp-plumber": "^1.2.1",
"gulp-rename": "^1.4.0",
"gulp-sourcemaps": "^2.6.5",
"gulp-uglify": "^3.0.2",
"it-each": "^0.4.0",
"jest": "^29.7.0",
"json-loader": "^0.5.7",
"karma": "^6.3.2",
"karma-babel-preprocessor": "^8.0.1",
"karma-browserify": "^6.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.1",
"karma-firefox-launcher": "^1.2.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sinon-chai": "^2.0.2",
"lolex": "^5.1.1",
"mocha": "^6.2.2",
"phantomjs-prebuilt": "^2.1.16",
"sinon": "^7.5.0",
"sinon-chai": "^3.3.0",
"vinyl-buffer": "^1.0.1",

@@ -96,4 +76,8 @@ "vinyl-source-stream": "^2.0.0"

"@types/json-schema": "^7.0.7",
"fast-xml-parser": "^4.5.0",
"json-pointer": "0.6.2"
},
"overrides": {
"braces": "3.0.3"
}
}
import { traverse, clearCache } from './traverse';
import { sampleArray, sampleBoolean, sampleNumber, sampleObject, sampleString } from './samplers/index';
import { XMLBuilder } from 'fast-xml-parser';

@@ -11,6 +12,26 @@ export var _samplers = {};

function convertJsonToXml(obj, schema) {
if (!obj) {
throw new Error('Unknown format output for building XML.');
}
if (Array.isArray(obj) || Object.keys(obj).length > 1) {
obj = { [schema?.xml?.name || 'root']: obj }; // XML document must contain one root element
}
const builder = new XMLBuilder({
ignoreAttributes : false,
format: true,
attributeNamePrefix: '$',
textNodeName: '#text',
});
return builder.build(obj);
}
export function sample(schema, options, spec) {
let opts = Object.assign({}, defaults, options);
clearCache();
return traverse(schema, opts, spec).value;
let result = traverse(schema, opts, spec).value;
if (opts?.format === 'xml') {
return convertJsonToXml(result, schema);
}
return result;
};

@@ -17,0 +38,0 @@

import { traverse } from '../traverse';
import { applyXMLAttributes } from '../utils';
export function sampleArray(schema, options = {}, spec, context) {

@@ -25,5 +27,28 @@ const depth = (context && context.depth || 1);

let { value: sample } = traverse(itemSchema, options, spec, {depth: depth + 1});
res.push(sample);
if (options?.format === 'xml') {
const { value, propertyName } = applyXMLAttributes({value: sample}, itemSchema, context);
if (propertyName) {
if (!res?.[propertyName]) {
res = { ...res, [propertyName]: [] };
}
res[propertyName].push(value);
} else {
res = {...res, ...value};
}
} else {
res.push(sample);
}
}
if (options?.format === 'xml' && depth === 1) {
const { value, propertyName } = applyXMLAttributes({value: null}, schema, context);
if (propertyName) {
if (value) {
res = Array.isArray(res) ? { [propertyName]: {...value, ...res.map(item => ({['#text']: {...item}}))} } : { [propertyName]: {...res, ...value }};
} else {
res = { [propertyName]: res };
}
}
}
return res;
}
import { traverse } from '../traverse';
import { applyXMLAttributes } from '../utils';
export function sampleObject(schema, options = {}, spec, context) {

@@ -30,3 +32,13 @@ let res = {};

}
res[propertyName] = sample.value;
if (options?.format === 'xml') {
const { propertyName: newPropertyName, value } = applyXMLAttributes(sample, schema.properties[propertyName], { propertyName });
if (newPropertyName) {
res[newPropertyName] = value;
} else {
res = { ...res, ...value };
}
} else {
res[propertyName] = sample.value;
}
});

@@ -33,0 +45,0 @@ }

@@ -6,2 +6,3 @@ import { _samplers } from './openapi-sampler';

import JsonPointer from 'json-pointer';
import { applyXMLAttributes } from './utils';

@@ -73,3 +74,10 @@ let $refCache = {};

$refCache[ref] = true;
result = traverse(referenced, options, spec, context);
const traverseResult = traverse(referenced, options, spec, context);
if (options.format === 'xml') {
const {propertyName, value} = applyXMLAttributes(traverseResult, referenced, context);
result = {...traverseResult, value: {[propertyName || 'root']: value}};
} else {
result = traverseResult;
}
$refCache[ref] = false;

@@ -76,0 +84,0 @@ } else {

@@ -8,5 +8,6 @@ import type { JSONSchema7 } from 'json-schema';

readonly quiet?: boolean;
readonly enablePatterns?: boolean
readonly enablePatterns?: boolean;
readonly format?: 'json' | 'xml';
}
export function sample(schema: JSONSchema7, options?: Options, document?: object): unknown;

@@ -74,2 +74,59 @@ 'use strict';

export function getXMLAttributes(schema) {
return {
name: schema?.xml?.name || '',
prefix: schema?.xml?.prefix || '',
namespace: schema?.xml?.namespace || null,
attribute: schema?.xml?.attribute ?? false,
wrapped: schema?.xml?.wrapped ?? false,
};
}
export function applyXMLAttributes(result, schema = {}, context = {}) {
const { value: oldValue } = result;
const { propertyName: oldPropertyName } = context;
const { name, prefix, namespace, attribute, wrapped } =
getXMLAttributes(schema);
let propertyName = name || oldPropertyName ? `${prefix ? prefix + ':' : ''}${name || oldPropertyName}` : null;
let value = typeof oldValue === 'object'
? Array.isArray(oldValue)
? [...oldValue]
: { ...oldValue }
: oldValue;
if (attribute && propertyName) {
propertyName = `$${propertyName}`;
}
if (namespace) {
if (typeof value === 'object') {
value[`$xmlns${prefix ? ':' + prefix : ''}`] = namespace;
} else {
value = { [`$xmlns${prefix ? ':' + prefix : ''}`]: namespace, ['#text']: value };
}
}
if (schema.type === 'array') {
if (wrapped && Array.isArray(value)) {
value = { [propertyName]: [...value] };
}
if (!wrapped) {
propertyName = null;
}
if (schema.example !== undefined && !wrapped) {
propertyName = schema.items.xml?.name || propertyName;
}
}
if (schema.oneOf || schema.anyOf || schema.allOf || schema.$ref) {
propertyName = null;
}
return {
propertyName,
value,
};
}
function hashCode(str) {

@@ -76,0 +133,0 @@ var hash = 0;

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc