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

Refactor handling of additionalProperties and add support for propertyNames #68

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
177 changes: 102 additions & 75 deletions lib/generateMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,7 @@ function getSchemaMarkdown(schema, fileName, headerLevel, suppressWarnings, sche
// Render table with summary of each property
md += createPropertiesSummary(schema, knownTypes, autoLink);

value = schema.additionalProperties;
if (defined(value) && !value) {
md += 'Additional properties are not allowed.\n\n';
} else {
md += 'Additional properties are allowed.\n\n';
// TODO: display their schema
}
md += getAdditionalProperties(schema, autoLink, 'Type of additional properties')

// Schema reference
if (embedMode === enums.embedMode.referenceIncludeDocument) {
Expand All @@ -215,6 +209,8 @@ function getSchemaMarkdown(schema, fileName, headerLevel, suppressWarnings, sche
schemaRelativeBasePath += '/';
}
md += style.bulletItem(style.bold('JSON schema') + ': ' + style.getLinkMarkdown(fileName, schemaRelativeBasePath.replace(/\\/g, '/') + fileName)) + '\n';
} else {
md += '\n'
}

// Render section for each property
Expand Down Expand Up @@ -346,90 +342,121 @@ function createPropertiesDetails(schema, title, headerLevel, knownTypes, autoLin
}

md += style.bulletItem(style.propertyDetails('Required') + ': ' + summary.required, 0);
md += getBasicSchemaInfo(property, summary, title, 0, knownTypes, autoLink)
}
}
md += '\n';

if (defined(property.exclusiveMinimum) && typeof property.exclusiveMinimum === 'number')
{
md += style.bulletItem(style.propertyDetails('Minimum') + ': ' + style.minMax(' > ' + property.exclusiveMinimum), 0);
} else {
var minimum = property.minimum;
if (defined(minimum)) {
var exclusiveMinimum = (defined(property.exclusiveMinimum) && property.exclusiveMinimum);
md += style.bulletItem(style.propertyDetails('Minimum') + ': ' + style.minMax((exclusiveMinimum ? ' > ' : ' >= ') + minimum), 0);
}
}
return md;
}

if (defined(property.exclusiveMaximum) && typeof property.exclusiveMaximum === 'number')
{
md += style.bulletItem(style.propertyDetails('Maximum') + ': ' + style.minMax(' < ' + property.exclusiveMaximum), 0);
} else {
var maximum = property.maximum;
if (defined(maximum)) {
var exclusiveMaximum = (defined(property.exclusiveMaximum) && property.exclusiveMaximum);
md += style.bulletItem(style.propertyDetails('Maximum') + ': ' + style.minMax((exclusiveMaximum ? ' < ' : ' <= ') + maximum), 0);
}
}
function getBasicSchemaInfo(property, summary, title, depth, knownTypes, autoLink) {
var md = ''

var format = property.format;
if (defined(format)) {
md += style.bulletItem(style.propertyDetails('Format') + ': ' + format, 0);
}
if (defined(property.exclusiveMinimum) && typeof property.exclusiveMinimum === 'number')
{
md += style.bulletItem(style.propertyDetails('Minimum') + ': ' + style.minMax(' > ' + property.exclusiveMinimum), depth);
} else {
var minimum = property.minimum;
if (defined(minimum)) {
var exclusiveMinimum = (defined(property.exclusiveMinimum) && property.exclusiveMinimum);
md += style.bulletItem(style.propertyDetails('Minimum') + ': ' + style.minMax((exclusiveMinimum ? ' > ' : ' >= ') + minimum), depth);
}
}

var pattern = property.pattern;
if (defined(pattern)) {
md += style.bulletItem(style.propertyDetails('Pattern') + ': ' + style.minMax(pattern), 0);
}
if (defined(property.exclusiveMaximum) && typeof property.exclusiveMaximum === 'number')
{
md += style.bulletItem(style.propertyDetails('Maximum') + ': ' + style.minMax(' < ' + property.exclusiveMaximum), depth);
} else {
var maximum = property.maximum;
if (defined(maximum)) {
var exclusiveMaximum = (defined(property.exclusiveMaximum) && property.exclusiveMaximum);
md += style.bulletItem(style.propertyDetails('Maximum') + ': ' + style.minMax((exclusiveMaximum ? ' < ' : ' <= ') + maximum), depth);
}
}

var minLength = property.minLength;
if (defined(minLength)) {
md += style.bulletItem(style.propertyDetails('Minimum Length') + style.minMax(': >= ' + minLength), 0);
}
var format = property.format;
if (defined(format)) {
md += style.bulletItem(style.propertyDetails('Format') + ': ' + format, depth);
}

var maxLength = property.maxLength;
if (defined(maxLength)) {
md += style.bulletItem(style.propertyDetails('Maximum Length') + style.minMax(': <= ' + maxLength), 0);
}
var pattern = property.pattern;
if (defined(pattern)) {
md += style.bulletItem(style.propertyDetails('Pattern') + ': ' + style.minMax(pattern), depth);
}

var enumString = getEnumString(property, type, 1);
if (defined(enumString)) {
md += style.bulletItem(style.propertyDetails('Allowed values') + ':', 0) + enumString;
}
var minLength = property.minLength;
if (defined(minLength)) {
md += style.bulletItem(style.propertyDetails('Minimum Length') + style.minMax(': >= ' + minLength), depth);
}

var additionalProperties = property.additionalProperties;
if (defined(additionalProperties) && (typeof additionalProperties === 'object')) {
var additionalPropertiesType = getPropertyType(additionalProperties);
if (defined(additionalPropertiesType)) {
// TODO: additionalProperties is really a full schema
var formattedType = style.typeValue(additionalPropertiesType);
if ((additionalProperties.type === 'object') && defined(property.title)) {
formattedType = style.linkType(property.title, property.title, autoLink);
}

md += style.bulletItem(style.propertyDetails('Type of each property') + ': ' + formattedType, 0);
}
}
var maxLength = property.maxLength;
if (defined(maxLength)) {
md += style.bulletItem(style.propertyDetails('Maximum Length') + style.minMax(': <= ' + maxLength), depth);
}

var examples = property.examples;
if (defined(examples)) {
md += style.bulletItem(style.propertyDetails('Examples') + ':');
for (const example of examples) {
md += style.bulletItem(style.defaultValue(example, type), 1);
}
}
var enumString = getEnumString(property, summary.type, depth+1);
if (defined(enumString)) {
md += style.bulletItem(style.propertyDetails('Allowed values') + ':', depth) + enumString;
}

// TODO: Add plugin point for custom JSON schema properties like gltf_*
var webgl = property.gltf_webgl;
if (defined(webgl)) {
md += style.bulletItem(style.propertyGltfWebGL('Related WebGL functions') + ': ' + webgl, 0);
}
if (defined(property.additionalProperties) && (typeof property.additionalProperties === 'object')) {
md += getAdditionalProperties(property, autoLink, 'Type of each property')
}

if (defined(property.propertyNames)) {
var names = property.propertyNames
names.type = 'string' // property names are always strings, so usually omitted.
md += style.bulletItem(style.propertyDetails('Property names') + ':', depth)
+ getBasicSchemaInfo(names, summary, title, depth+1, knownTypes, autoLink)
}
var summary = getPropertySummary(property, knownTypes, autoLink);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that summary is already declared here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(More generally, there are several errors (e.g. missing semicolons) that could be caught with eslint)


md += '\n';
var examples = property.examples;
if (defined(examples)) {
md += style.bulletItem(style.propertyDetails('Examples') + ':');
for (const example of examples) {
md += style.bulletItem(style.defaultValue(example, summary.type), 1);
}
}
md += '\n';

return md;
// TODO: Add plugin point for custom JSON schema properties like gltf_*
var webgl = property.gltf_webgl;
if (defined(webgl)) {
md += style.bulletItem(style.propertyGltfWebGL('Related WebGL functions') + ': ' + webgl, depth);
}

return md + '\n'
}

function getAdditionalProperties(schema, autoLink, detailsHeader) {
var additionalProperties = schema.additionalProperties;
if (defined(additionalProperties) && !additionalProperties) {
return style.bulletItem(style.propertyDetails('Additional properties are not allowed.'))
}
if (!defined(additionalProperties) || typeof additionalProperties !== 'object') {
return style.bulletItem(style.propertyDetails('Additional properties are allowed.'))
}
var additionalPropertiesType = getPropertyType(additionalProperties);
if (defined(additionalPropertiesType)) {
// TODO: additionalProperties is really a full schema
var formattedType = style.typeValue(additionalPropertiesType);
if ((additionalProperties.type === 'object') && defined(additionalProperties.title)) {
formattedType = style.linkType(formattedType, additionalPropertiesType, autoLink);
}
// XXX Previous method, but it showed the type of the parent, which is not right.
// Left for future reference and/or repairs.
// if ((additionalProperties.type === 'object') && defined(schema.title)) {
// formattedType = style.linkType(schema.title, schema.title, autoLink);
// }

return style.bulletItem(style.propertyDetails('Additional properties are allowed.'))
+ style.bulletItem(style.propertyDetails(detailsHeader) + ': ' + formattedType, 0);
}
return ''
}


function getPropertySummary(property, knownTypes, autoLink) {
var type = defaultValue(getPropertyType(property), 'any');
var formattedType = style.linkType(style.typeValue(type), type, autoLink);
Expand Down
3 changes: 1 addition & 2 deletions test/test-golden/example-embed.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Example description.

|===

Additional properties are not allowed.

* **Additional properties are not allowed.**
* **JSON schema**: <<schema-reference-example,`example.schema.json`>>

=== example.byteOffset
Expand Down
2 changes: 1 addition & 1 deletion test/test-golden/example-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Example description.
|**byteOffset**|`integer`|The offset relative to the start of the buffer in bytes.|No, default: `0`|
|**type**|`string`|Specifies if the elements are scalars, vectors, or matrices.| &#10003; Yes|

Additional properties are not allowed.
* **Additional properties are not allowed.**

### example.byteOffset

Expand Down
3 changes: 1 addition & 2 deletions test/test-golden/example-linked.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Example description.

|===

Additional properties are not allowed.

* **Additional properties are not allowed.**
* **JSON schema**: link:schema/example.schema.json[example.schema.json]

==== example.byteOffset
Expand Down
3 changes: 1 addition & 2 deletions test/test-golden/example-linked.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Example description.
|**byteOffset**|`integer`|The offset relative to the start of the buffer in bytes.|No, default: `0`|
|**type**|`string`|Specifies if the elements are scalars, vectors, or matrices.| &#10003; Yes|

Additional properties are not allowed.

* **Additional properties are not allowed.**
* **JSON schema**: [example.schema.json](schema/example.schema.json)

#### example.byteOffset
Expand Down
3 changes: 1 addition & 2 deletions test/test-golden/example-remote.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Example description.

|===

Additional properties are not allowed.

* **Additional properties are not allowed.**
* **JSON schema**: link:https://www.khronos.org/wetzel/just/testing/schema/example.schema.json[example.schema.json]

=== example.byteOffset
Expand Down
3 changes: 1 addition & 2 deletions test/test-golden/example-remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Example description.
|**byteOffset**|`integer`|The offset relative to the start of the buffer in bytes.|No, default: `0`|
|**type**|`string`|Specifies if the elements are scalars, vectors, or matrices.| &#10003; Yes|

Additional properties are not allowed.

* **Additional properties are not allowed.**
* **JSON schema**: [example.schema.json](https://www.khronos.org/wetzel/just/testing/schema/example.schema.json)

### example.byteOffset
Expand Down
2 changes: 1 addition & 1 deletion test/test-golden/example-simple.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Example description.

|===

Additional properties are not allowed.
* **Additional properties are not allowed.**

=== example.byteOffset

Expand Down
2 changes: 1 addition & 1 deletion test/test-golden/example-simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Example description.
|**byteOffset**|`integer`|The offset relative to the start of the buffer in bytes.|No, default: `0`|
|**type**|`string`|Specifies if the elements are scalars, vectors, or matrices.| &#10003; Yes|

Additional properties are not allowed.
* **Additional properties are not allowed.**

### example.byteOffset

Expand Down
Loading