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

Ref validations not working as expected #34

Open
CptKirk opened this issue Mar 18, 2024 · 0 comments · May be fixed by #35
Open

Ref validations not working as expected #34

CptKirk opened this issue Mar 18, 2024 · 0 comments · May be fixed by #35

Comments

@CptKirk
Copy link

CptKirk commented Mar 18, 2024

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
CptKirk added a commit to CptKirk/json-schema-tree that referenced this issue Mar 18, 2024
Allow usage of readOnly and WriteOnly tags on $ref nodes

fix stoplightio#34
@CptKirk CptKirk linked a pull request Mar 18, 2024 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant