Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples generation #947

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"lit": "^2.7.5",
"marked": "^5.1.0",
"prismjs": "^1.29.0",
"randexp": "^0.5.3",
"xml-but-prettier": "^1.0.1"
},
"scripts": {
Expand Down
27 changes: 23 additions & 4 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import RandExp from 'randexp';

// Make RandExp determinist
RandExp.prototype.randInt = (from) => from;

// Takes a value as input and provides a printable string to replresent null values, spaces, blankstring etc
export function getPrintableVal(val) {
if (val === undefined) {
Expand Down Expand Up @@ -223,17 +228,20 @@ export function getSampleValueByType(schemaObj) {
}
if (schemaObj.$ref) {
// Indicates a Circular ref
return schemaObj.$ref;
return {};
}
if (schemaObj.const === false || schemaObj.const === 0 || schemaObj.const === null || schemaObj.const === '') {
return schemaObj.const;
}
if (schemaObj.const) {
return schemaObj.const;
}
if (schemaObj.default) {
return schemaObj.default;
}
const typeValue = Array.isArray(schemaObj.type) ? schemaObj.type[0] : schemaObj.type;
if (!typeValue) {
return '?';
return null;
}
if (typeValue.match(/^integer|^number/g)) {
const multipleOf = Number.isNaN(Number(schemaObj.multipleOf)) ? undefined : Number(schemaObj.multipleOf);
Expand All @@ -257,7 +265,13 @@ export function getSampleValueByType(schemaObj) {
if (typeValue.match(/^string/g)) {
if (schemaObj.enum) { return schemaObj.enum[0]; }
if (schemaObj.const) { return schemaObj.const; }
if (schemaObj.pattern) { return schemaObj.pattern; }
if (schemaObj.pattern) {
try {
return new RandExp(schemaObj.pattern).gen();
} catch (error) {
return schemaObj.pattern;
}
}
if (schemaObj.format) {
const u = `${Date.now().toString(16)}${Math.random().toString(16)}0`.repeat(16);
switch (schemaObj.format.toLowerCase()) {
Expand Down Expand Up @@ -297,7 +311,7 @@ export function getSampleValueByType(schemaObj) {
}
}
// If type cannot be determined
return '?';
return null;
}

/*
Expand Down Expand Up @@ -590,6 +604,11 @@ export function schemaToSampleObj(schema, config = { }) {
}
obj = mergePropertyExamples(obj, propertyName, schemaToSampleObj(schema.properties[propertyName], config));
}
if (typeof schema.additionalProperties === 'object') {
const propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property';
obj = mergePropertyExamples(obj, `${propertyName}1`, schemaToSampleObj(schema.additionalProperties, config));
obj = mergePropertyExamples(obj, `${propertyName}2`, schemaToSampleObj(schema.additionalProperties, config));
}
}
} else if (schema.type === 'array' || schema.items) {
if (schema.items || schema.example) {
Expand Down