Skip to content

Commit

Permalink
Merge pull request #211 from Authress-Engineering/wparad/fix-non-id-p…
Browse files Browse the repository at this point in the history
…roperty-examples

Enable non-id properties to still generate examples. fix #210
  • Loading branch information
wparad authored Dec 1, 2023
2 parents 5ffd2f7 + 7569a17 commit 3dafa62
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export function getTypeInfo(schema, options = { includeNulls: false }) {
return info;
}

export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampleStrings) {
export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampleIds) {
const example = Array.isArray(schemaObj.examples) ? schemaObj.examples[0] : Object.values(schemaObj.examples || {})[0]?.value ?? schemaObj.example;
if (skipExampleStrings && typeof example === 'string') { return ''; }
if (skipExampleIds && typeof example === 'string' && fallbackPropertyName.match(/id$/i)) { return ''; }
if (typeof example !== 'undefined') { return example; }

if (schemaObj.default) { return schemaObj.default; }
Expand Down Expand Up @@ -139,7 +139,7 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
}
if (typeValue.match(/^boolean/g)) { return false; }
if (typeValue.match(/^null/g)) { return null; }
if (skipExampleStrings && typeValue.match(/^string/g)) { return ''; }
if (skipExampleIds && typeValue.match(/^string/g) && fallbackPropertyName.match(/id$/i)) { return ''; }
if (typeValue.match(/^string/g)) {
if (schemaObj.pattern) {
const examplePattern = schemaObj.pattern.replace(/[+*](?![^\][]*[\]])/g, '{8}').replace(/\{\d*,(\d+)?\}/g, '{8}');
Expand Down Expand Up @@ -292,7 +292,7 @@ function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes,
return config.xml ? [xmlTagProperties] : objectExamples;
}

const value = getSampleValueByType(schema, config.propertyName, config.skipExampleStrings);
const value = getSampleValueByType(schema, config.propertyName, config.skipExampleIds);
return [value];
}

Expand Down Expand Up @@ -503,7 +503,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
}

/* Create Example object */
export function generateExample(examples, example, schema, rawMimeType, includeReadOnly = true, includeWriteOnly = true, outputType, skipExampleStrings = false) {
export function generateExample(examples, example, schema, rawMimeType, includeReadOnly = true, includeWriteOnly = true, outputType, skipExampleIds = false) {
const mimeType = rawMimeType || 'application/json';
const finalExamples = [];

Expand Down Expand Up @@ -595,7 +595,7 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
const config = {
includeReadOnly,
includeWriteOnly,
skipExampleStrings,
skipExampleIds,
xml: mimeType.toLowerCase().includes('xml'),
};

Expand Down

0 comments on commit 3dafa62

Please sign in to comment.