json-schema-viewer icon indicating copy to clipboard operation
json-schema-viewer copied to clipboard

Support schemas inside of `additionalProperties`

Open GAUNSD opened this issue 4 years ago • 8 comments

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

image

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"
}

GAUNSD avatar Feb 23 '21 23:02 GAUNSD

This is effecting the elements v7 and is not handled in json-schema-viewer v4.

image

philsturgeon avatar Jun 28 '21 14:06 philsturgeon

Maybe the viewer's internal logic could turn additionalProperties into properties/{*} to support this.

Airkro avatar Jun 16 '22 01:06 Airkro

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

philsturgeon avatar Jun 16 '22 11:06 philsturgeon

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.

siimsoni avatar Jan 10 '23 14:01 siimsoni

This bug is a major inconvenience

onavratil-monetplus avatar Mar 16 '23 12:03 onavratil-monetplus

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.

danielgtaylor avatar Oct 18 '23 23:10 danielgtaylor

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

jy95 avatar Jan 03 '24 18:01 jy95

It seems to be working as of 8.0.2

siimsoni avatar May 23 '24 11:05 siimsoni