Vscode: [json] improve property suggestions with oneOf

Created on 1 Feb 2018  路  14Comments  路  Source: microsoft/vscode

  • VSCode Version: Version 1.20.0-insider
  • OS Version: 10.13.3

Steps to Reproduce:

  1. Use this JSON schema
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "oneOf": [
        {
            "title": "Wrapper",
            "type": "object",
            "required": [
                "asset"
            ],
            "properties": {
                "asset": {
                    "type": "object"
                }
            }
        },
        {
            "title": "No Wrapper",
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                }
            }
        }
    ]
}
  1. Start writing JSON using this schema and type:
{
    ""
}
  1. Only id is suggested as an option, I expect both asset and id to be suggestions

NOTE: if i make "id" also required I get the correct suggestions.


Does this issue occur when all extensions are disabled?: Yes/No
Yes

feature-request json

Most helpful comment

I have the same problem.

  • VSCode Version: Version 1.44.0
  • macOS Version: 10.15.4

Steps to Reproduce:

  1. Use this JSON schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["chains"],
  "properties": {
    "chains": {
      "oneOf": [
        {
          "type": "array",
          "items": {
            "$ref": "chain.json"
          }
        },
        {
          "type": "array",
          "items": {
            "properties": {
              "chain_path": {
                "type": "string"
              }
            }
          }
        }
      ]
    }
  }
}
  1. Start writing JSON using this schema and type:
{
  "$schema": "the_schema.json",
  "chains": [
    {
      ""
    }
  ]
}
  1. Only id is suggested as an option ("chain_path"), I expect both asset and id to be suggestions.

Does this issue occur when all extensions are disabled?: Yes

thnx.

All 14 comments

Here is another case where I am only suggested object and array when i expect: object, string, and array:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "expressions": {
            "oneOf": [
                {
                    "type": "object"
                },
                {
                    "type": "string"
                },
                {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            ]
        }
    }
}

+1

+1

I have the same problem.

  • VSCode Version: Version 1.44.0
  • macOS Version: 10.15.4

Steps to Reproduce:

  1. Use this JSON schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["chains"],
  "properties": {
    "chains": {
      "oneOf": [
        {
          "type": "array",
          "items": {
            "$ref": "chain.json"
          }
        },
        {
          "type": "array",
          "items": {
            "properties": {
              "chain_path": {
                "type": "string"
              }
            }
          }
        }
      ]
    }
  }
}
  1. Start writing JSON using this schema and type:
{
  "$schema": "the_schema.json",
  "chains": [
    {
      ""
    }
  ]
}
  1. Only id is suggested as an option ("chain_path"), I expect both asset and id to be suggestions.

Does this issue occur when all extensions are disabled?: Yes

thnx.

+1

Same problem here.

+1

+1 It's about time to get an actual fix isn't it? It has been a loooong time since issue got opened

+1

Hey guys +1s are generally considered bad form on an issue. Please react to the first comment instead which will also help with prioritizing. On most project's maintainers will go through all the +1 comments and hide them, as they do not contribute to the conversation around the issue

Hi folks. I've noticed this same issue lately and was about to file an issue myself. I think this is a general property of the JSON parser and not so much a VS code issue actually. When listing available options, properties with a "required" list aren't possible since those required fields aren't already entered. It's very chicken & egg. I'll delve into the code myself to see if I can whip up a workaround to include partial options but this may be a limitation of the JSON parsers.

Peek 2020-06-04 09-44

Hello all!
I am working on the fix and have some progress:
Screenshot 2020-10-25 at 11 43 14

@jboero

When listing available options, properties with a "required" list aren't possible since those
required fields aren't already entered. It's very chicken & egg.

Makes sense. That is my suggestion of logic:

  1. If no props are entered, then suggest all props including required.
  2. If a prop is entered and this prop is a prop of a subschema, then suggest only props of this subschema.

The fix for the issue seems more complicated than I thought.

The problem is in validate function that not only validates the schema for the JSON, but also returns matching subschemas. If there is a required field in one of your subschemas, but your node is empty (as in first comment of this issue thread), then the subschema is not valid and it's not added to matches.

When the user only starts writing the JSON using the schema, it's completely ok that JSON is not valid and IMHO VSCode should help users to complete the JSON.

A possible solution is to split validate function in smaller parts, so it could be reused and make another function that returns matching subschemas but ignoring some validation problems. But that's a big work as far as I see 馃檪

Was this page helpful?
0 / 5 - 0 ratings