microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.55k stars 29.4k forks source link

[json] improve property suggestions with oneOf #42758

Open hipstersmoothie opened 6 years ago

hipstersmoothie commented 6 years ago

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

hipstersmoothie commented 6 years ago

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"
                    }
                }
            ]
        }
    }
}
ghost commented 6 years ago

+1

JohanSpannare commented 6 years ago

+1

CarlosCarmona commented 4 years ago

I have the same problem.

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": [
    {
      ""
    }
    ]
    }
  2. 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.

alo commented 4 years ago

+1

Jhonsensf commented 4 years ago

Same problem here.

gocastilla commented 4 years ago

+1

pakitovic commented 4 years ago

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

judmft commented 4 years ago

+1

hipstersmoothie commented 4 years ago

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

jboero commented 4 years ago

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

n10v commented 4 years ago

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

n10v commented 4 years ago

@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.
n10v commented 4 years ago

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 🙂

Morriz commented 3 years ago

any more progress here?

tomasbruckner commented 1 year ago

still waiting for the fix

tripleeagle commented 1 year ago

any more progress plz?