json-schema-viewer
json-schema-viewer copied to clipboard
Support schemas inside of `additionalProperties`
Describe the bug
When I have a JSON schema that defines further schema constraints inside of additionalProperties...JSV does not render them properly in the UI.
To Reproduce
Sample JSON Schema
{
"$id": "https://example.com/sample.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Sample schema",
"description": "To reproduce a JSV issue",
"required": [
"foo",
"bar",
"baz"
],
"additionalProperties": false,
"properties": {
"foo": {
"type": "string",
"description": "Some foo property",
"const": "foo_value"
},
"bar": {
"type": "object",
"description": "An object where keys can be anything...but I want to restrict the values of each key",
"required": [
"bing",
"bang"
],
"additionalProperties": {
"type": "object",
"description": "Constraints for each value",
"additionalProperties": false,
"properties": {
"bar_prop_one": {
"type": "string"
},
"bar_prop_two": {
"type": "string"
}
}
}
},
"baz": {
"type": "object",
"description": "An object where keys can be anything...but I want to restrict the values of each key by a shared definition",
"required": [
"boom"
],
"additionalProperties": {
"$ref": "#/definitions/defined_entity"
}
}
},
"definitions": {
"defined_entity": {
"$id": "#/definitions/defined_entity",
"type": "string",
"description": "Definition for an Entity.",
"enum": [
"entity_one",
"entity_two"
]
}
}
}
Sample JSON object that would work with schema above
{
"foo": "foo_value",
"bar": {
"bing": {
"bar_prop_one": "a",
"bar_prop_two": "b",
},
"bang": {
"bar_prop_one": "a",
"bar_prop_two": "b",
},
"tang": {
"bar_prop_one": "tango",
"bar_prop_two": "tangotango",
},
},
"baz": {
"boom": "entity_one",
"one": "entity_one",
"two": "entity_one",
"three": "entity_two",
"four": "entity_two",
"eleven": "entity_two"
}
}
JSV Configuration
import sampleSchema = "../../../schemas/sample.schema.json";
<JsonSchemaViewer
schema={sampleSchema}
viewMode="standalone"
/>
Expected behavior
A clear and concise description of what you expected to happen.
My expectation is that the schema I provide inside additionalProperties is rendered in some way.
Additional context
Add any other context about the problem here.
Screenshots

Environment
{
"@stoplight/json-schema-viewer": "3.0.0",
"@stoplight/markdown-viewer": "3.8.1",
"@stoplight/ui-kit": "3.0.0-beta.39",
"mobx": "5.15.7"
}
This is effecting the elements v7 and is not handled in json-schema-viewer v4.
Maybe the viewer's internal logic could turn additionalProperties into properties/{*} to support this.
This could be considered "OpenAPI v3.1 Compatibility" work, as this was not supported in prior versions and is now.
http://json-schema.org/understanding-json-schema/reference/object.html#additional-properties
additionalProperties is part of JSON Schema Draft 4, and a quite important part of it, as it's needed to represent value maps. https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.4.4 It is quite surprising that this is not supported, considering the project claims full JSON Schema Draft 4 support.
This bug is a major inconvenience
Any update on this? It's hard to recommend Elements be used when a fairly basic feature is missing. This makes it so maps are just documented as object which isn't very useful, particularly when the map value is a complex type.
If it could help anyone, I started a viewer project called docusaurus-json-schema-plugin which supports additionalProperties . I had that need as well ...
If needed, you can migrate your schema from draft-04 to draft-07 using ajv-cli
It seems to be working as of 8.0.2