openapi-sampler
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -882,2 +882,33 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.OpenAPISampler = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
function inferExample(schema) { | ||
var example; | ||
if (schema.const !== undefined) { | ||
example = schema.const; | ||
} else if (schema.examples !== undefined && schema.examples.length) { | ||
example = schema.examples[0]; | ||
} else if (schema.enum !== undefined && schema.enum.length) { | ||
example = schema.enum[0]; | ||
} else if (schema.default !== undefined) { | ||
example = schema.default; | ||
} | ||
return example; | ||
} | ||
function tryInferExample(schema) { | ||
var example = inferExample(schema); // case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords | ||
if (example !== undefined) { | ||
return { | ||
value: example, | ||
readOnly: schema.readOnly, | ||
writeOnly: schema.writeOnly, | ||
type: null | ||
}; | ||
} | ||
return; | ||
} | ||
function traverse(schema, options, spec, context) { | ||
@@ -936,3 +967,3 @@ // checking circular JS references by checking context | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return (0, _allOf.allOfSample)(_objectSpread(_objectSpread({}, schema), {}, { | ||
return tryInferExample(schema) || (0, _allOf.allOfSample)(_objectSpread(_objectSpread({}, schema), {}, { | ||
allOf: undefined | ||
@@ -948,3 +979,3 @@ }), schema.allOf, options, spec, context); | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return traverse(schema.oneOf[0], options, spec, context); | ||
return tryInferExample(schema) || traverse(schema.oneOf[0], options, spec, context); | ||
} | ||
@@ -954,21 +985,15 @@ | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return traverse(schema.anyOf[0], options, spec, context); | ||
return tryInferExample(schema) || traverse(schema.anyOf[0], options, spec, context); | ||
} | ||
if (schema.if && schema.then) { | ||
return traverse((0, _utils.mergeDeep)(schema.if, schema.then), options, spec, context); | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return tryInferExample(schema) || traverse((0, _utils.mergeDeep)(schema.if, schema.then), options, spec, context); | ||
} | ||
var example = null; | ||
var example = inferExample(schema); | ||
var type = null; | ||
if (schema.default !== undefined) { | ||
example = schema.default; | ||
} else if (schema.const !== undefined) { | ||
example = schema.const; | ||
} else if (schema.enum !== undefined && schema.enum.length) { | ||
example = schema.enum[0]; | ||
} else if (schema.examples !== undefined && schema.examples.length) { | ||
example = schema.examples[0]; | ||
} else { | ||
if (example === undefined) { | ||
example = null; | ||
type = schema.type; | ||
@@ -975,0 +1000,0 @@ |
{ | ||
"name": "openapi-sampler", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Tool for generation samples based on OpenAPI payload/response schema", | ||
@@ -5,0 +5,0 @@ "main": "dist/openapi-sampler.js", |
@@ -12,3 +12,3 @@ # openapi-sampler | ||
- Supports `additionalProperties` | ||
- Uses `default`, `const`, `enum` and `examples` where possible | ||
- Uses `const`, `examples`, `enum` and `default` where possible - in this order | ||
- Good array support: supports `contains`, `minItems`, `maxItems`, and tuples (`items` as an array) | ||
@@ -15,0 +15,0 @@ - Supports `minLength`, `maxLength`, `min`, `max`, `exclusiveMinimum`, `exclusiveMaximum` |
@@ -16,2 +16,30 @@ import { _samplers } from './openapi-sampler'; | ||
function inferExample(schema) { | ||
let example; | ||
if (schema.const !== undefined) { | ||
example = schema.const; | ||
} else if (schema.examples !== undefined && schema.examples.length) { | ||
example = schema.examples[0]; | ||
} else if (schema.enum !== undefined && schema.enum.length) { | ||
example = schema.enum[0]; | ||
} else if (schema.default !== undefined) { | ||
example = schema.default; | ||
} | ||
return example; | ||
} | ||
function tryInferExample(schema) { | ||
const example = inferExample(schema); | ||
// case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords | ||
if (example !== undefined) { | ||
return { | ||
value: example, | ||
readOnly: schema.readOnly, | ||
writeOnly: schema.writeOnly, | ||
type: null, | ||
}; | ||
} | ||
return; | ||
} | ||
export function traverse(schema, options, spec, context) { | ||
@@ -41,3 +69,2 @@ // checking circular JS references by checking context | ||
const referenced = JsonPointer.get(spec, ref); | ||
let result; | ||
@@ -69,3 +96,3 @@ | ||
popSchemaStack(seenSchemasStack, context); | ||
return allOfSample( | ||
return tryInferExample(schema) || allOfSample( | ||
{ ...schema, allOf: undefined }, | ||
@@ -84,3 +111,3 @@ schema.allOf, | ||
popSchemaStack(seenSchemasStack, context); | ||
return traverse(schema.oneOf[0], options, spec, context); | ||
return tryInferExample(schema) || traverse(schema.oneOf[0], options, spec, context); | ||
} | ||
@@ -90,20 +117,14 @@ | ||
popSchemaStack(seenSchemasStack, context); | ||
return traverse(schema.anyOf[0], options, spec, context); | ||
return tryInferExample(schema) || traverse(schema.anyOf[0], options, spec, context); | ||
} | ||
if (schema.if && schema.then) { | ||
return traverse(mergeDeep(schema.if, schema.then), options, spec, context); | ||
popSchemaStack(seenSchemasStack, context); | ||
return tryInferExample(schema) || traverse(mergeDeep(schema.if, schema.then), options, spec, context); | ||
} | ||
let example = null; | ||
let example = inferExample(schema); | ||
let type = null; | ||
if (schema.default !== undefined) { | ||
example = schema.default; | ||
} else if (schema.const !== undefined) { | ||
example = schema.const; | ||
} else if (schema.enum !== undefined && schema.enum.length) { | ||
example = schema.enum[0]; | ||
} else if (schema.examples !== undefined && schema.examples.length) { | ||
example = schema.examples[0]; | ||
} else { | ||
if (example === undefined) { | ||
example = null; | ||
type = schema.type; | ||
@@ -110,0 +131,0 @@ if (Array.isArray(type) && schema.type.length > 0) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58108
1452