Skip to content

Commit

Permalink
fix: parameter examples don't get picked up if not defined in-schema (#…
Browse files Browse the repository at this point in the history
…477)

* refactor: splitting out code openapi->jsonschema work into its own library

* refactor: moving test oas files into the __datasets__ dir

* fix: issue where parameter examples wouldn't be seen if not on the schema

* chore: reverting an unnecessary change
  • Loading branch information
erunion authored Aug 10, 2021
1 parent 1009ee0 commit 34d3c3a
Show file tree
Hide file tree
Showing 26 changed files with 3,828 additions and 6,558 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
__tests__/__fixtures__/
__tests__/cli/__fixtures__/
coverage/
dist/
File renamed without changes.
File renamed without changes.
181 changes: 181 additions & 0 deletions __tests__/__fixtures__/json-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
const SCHEMA_SCENARIOS = {
'array of primitives': (props, allowEmptyValue) => {
return {
type: 'array',
items: {
type: 'string',
...props,
...(allowEmptyValue !== undefined ? { allowEmptyValue } : {}),
},
};
},

'array with an array of primitives': (props, allowEmptyValue) => {
return {
type: 'array',
items: {
type: 'array',
items: {
type: 'string',
...props,
...(allowEmptyValue !== undefined ? { allowEmptyValue } : {}),
},
},
};
},

'object with primitives and mixed arrays': (props, allowEmptyValue) => {
return {
type: 'object',
properties: {
param1: {
type: 'string',
...props,
...(allowEmptyValue !== undefined ? { allowEmptyValue } : {}),
},
param2: {
type: 'array',
items: {
type: 'array',
items: {
type: 'string',
...props,
...(allowEmptyValue !== undefined ? { allowEmptyValue } : {}),
},
},
},
},
};
},

'primitive string': (props, allowEmptyValue) => {
return {
type: 'string',
...props,
...(allowEmptyValue !== undefined ? { allowEmptyValue } : {}),
};
},
};

function buildSchemaCore(opts) {
const props = {};
if (typeof opts.default === 'undefined' || opts.default === undefined) {
// Should not add a default.
} else if (opts.default === '') {
props.default = '';
} else {
props.default = opts.default ? opts.default : false;
}

if (opts.example !== undefined) {
props.example = opts.example;
}

if (opts.examples !== undefined) {
props.examples = opts.examples;
}

return props;
}

function generateScenarioName(testCase, opts) {
const caseOptions = [];

if (opts.allowEmptyValue !== undefined) caseOptions.push(`allowEmptyValue[${opts.allowEmptyValue}]`);
if (opts.example !== undefined) caseOptions.push(`example[${opts.example}]`);
if (opts.examples !== undefined) caseOptions.push(`examples[${opts.examples}]`);
if (opts.default !== undefined) caseOptions.push(`default[${opts.default}]`);

return `${testCase}:${caseOptions.join('')}`;
}

function generateJSONSchemaFixture(
opts = {
allowEmptyValue: undefined,
default: undefined,
example: undefined,
examples: undefined,
}
) {
const props = buildSchemaCore(opts);
const schemas = [];

const getScenario = (scenario, allowEmptyValue) => {
return {
scenario: generateScenarioName(scenario, { ...opts, allowEmptyValue }),
schema: SCHEMA_SCENARIOS[scenario](props, allowEmptyValue),
};
};

const getPolymorphismScenario = (polyType, scenario, allowEmptyValue) => {
return {
scenario: `${polyType}:${generateScenarioName(scenario, { ...opts, allowEmptyValue })}`,
schema: {
[polyType]: [
SCHEMA_SCENARIOS[scenario](props, allowEmptyValue),
SCHEMA_SCENARIOS[scenario](props, allowEmptyValue),
],
},
};
};

// When `allowEmptyValue` is present, we should make sure we're testing both states. If `true`, we should allow
// empty string `default` properties through. If `false`, they should ultimately be omitted from the final
// compilation.
schemas.push(
getScenario('array of primitives'),
getPolymorphismScenario('oneOf', 'array of primitives'),
getPolymorphismScenario('allOf', 'array of primitives'),
getPolymorphismScenario('anyOf', 'array of primitives'),

getScenario('array with an array of primitives'),
getPolymorphismScenario('oneOf', 'array with an array of primitives'),
getPolymorphismScenario('allOf', 'array with an array of primitives'),
getPolymorphismScenario('anyOf', 'array with an array of primitives'),

getScenario('object with primitives and mixed arrays'),
getPolymorphismScenario('oneOf', 'object with primitives and mixed arrays'),
getPolymorphismScenario('allOf', 'object with primitives and mixed arrays'),
getPolymorphismScenario('anyOf', 'object with primitives and mixed arrays'),

getScenario('primitive string'),
getPolymorphismScenario('oneOf', 'primitive string'),
getPolymorphismScenario('allOf', 'primitive string'),
getPolymorphismScenario('anyOf', 'primitive string')
);

if (opts.allowEmptyValue !== undefined) {
schemas.push(
getScenario('array of primitives', true),
getScenario('array of primitives', false),
getPolymorphismScenario('oneOf', 'array of primitives', true),
getPolymorphismScenario('allOf', 'array of primitives', false),
getPolymorphismScenario('anyOf', 'array of primitives', false),

getScenario('array with an array of primitives', true),
getScenario('array with an array of primitives', false),
getPolymorphismScenario('oneOf', 'array with an array of primitives', true),
getPolymorphismScenario('allOf', 'array with an array of primitives', false),
getPolymorphismScenario('anyOf', 'array with an array of primitives', false),

getScenario('object with primitives and mixed arrays', true),
getScenario('object with primitives and mixed arrays', false),
getPolymorphismScenario('oneOf', 'object with primitives and mixed arrays', true),
getPolymorphismScenario('allOf', 'object with primitives and mixed arrays', false),
getPolymorphismScenario('anyOf', 'object with primitives and mixed arrays', false),

getScenario('primitive string', true),
getScenario('primitive string', false),
getPolymorphismScenario('oneOf', 'primitive string', true),
getPolymorphismScenario('allOf', 'primitive string', false),
getPolymorphismScenario('anyOf', 'primitive string', false)
);
}

return {
type: 'object',
properties: Object.fromEntries(new Map(schemas.map(s => [s.scenario, s.schema]))),
};
}

module.exports = generateJSONSchemaFixture;
Loading

0 comments on commit 34d3c3a

Please sign in to comment.