Skip to content

Ref validations not working as expected #34

@CptKirk

Description

@CptKirk

When a $ref node has a validation like readOnly or writeOnly in the schema, this parser fails to properly forward this information which results in wrong visualizations in Stoplight Elements depending on the view mode.

Context

This bug has the effect that in @stoplight/json-schema-viewer (and thus Stoplight Elements), readOnly and writeOnly objects ($ref node) are not rendered properly. It was noticed during fixing a bug in a Go REST API library called huma: danielgtaylor/huma#314

Steps to Reproduce

import { SchemaTree } from '@stoplight/json-schema-tree';
const tree = new SchemaTree({
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Nested": {
            "additionalProperties": false,
            "properties": {
                "nestedValue": { "type": "string" },
                "nestedValue2": { "type": "string" }
            },
            "required": ["nestedValue", "nestedValue2"],
            "type": "object"
        }
    },
    "additionalProperties": false,
    "properties": {
        "$schema": {
            "description": "A URL to the JSON Schema for this object.",
            "format": "uri",
            "readOnly": true,
            "type": "string"
        },
        "foo": {
            "$ref": "#/definitions/Nested",
            "readOnly": true
        },
        "foo2": {
            "$ref": "#/definitions/Nested",
            "writeOnly": true
        },
        "bar": {
            "format": "int64", "type": "integer"
        }
    },
    "required": ["bar", "foo", "foo2"],
    "type": "object"
});
tree.populate();
tree.root; // populated tree

for (const node of tree.root.children[0].children) {
    console.log('---');
    console.log(node.path);
    console.log(node.fragment);
    console.log(node.validations);
}

Current Behavior

trimmed test output:

---
[ 'properties', 'foo' ]
{
  additionalProperties: false,
  properties: { nestedValue: { type: 'string' }, nestedValue2: { type: 'string' } },
  required: [ 'nestedValue', 'nestedValue2' ],
  type: 'object'
}
{}
---
[ 'properties', 'foo2' ]
{
  additionalProperties: false,
  properties: { nestedValue: { type: 'string' }, nestedValue2: { type: 'string' } },
  required: [ 'nestedValue', 'nestedValue2' ],
  type: 'object'
}
{}
---

json-schema-viewer output:

image

Expected Behavior

trimmed test output:

---
[ 'properties', 'foo' ]
{
  additionalProperties: false,
  properties: { nestedValue: { type: 'string' }, nestedValue2: { type: 'string' } },
  required: [ 'nestedValue', 'nestedValue2' ],
  type: 'object'
}
{ readOnly: true }
---
[ 'properties', 'foo2' ]
{
  additionalProperties: false,
  properties: { nestedValue: { type: 'string' }, nestedValue2: { type: 'string' } },
  required: [ 'nestedValue', 'nestedValue2' ],
  type: 'object'
}
{ writeOnly: true }
---

json-schema-viewer output:

image

Possible Workaround/Solution

I will mention this issue in a PR with my proposed solution.

Environment

  • Version used: master (8300b12)
  • Environment name and version (e.g. Chrome 39, node.js 5.4): node.js v21.7.0
  • Operating System and version (desktop or mobile): Linux 6.7.9-arch1-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions