vscode-yaml icon indicating copy to clipboard operation
vscode-yaml copied to clipboard

only the first choice is shown when hovering anyOf-typed properties

Open thedmi opened this issue 3 years ago • 0 comments

Describe the bug

Documentation that is shown when hovering properties is misleading in some cases related to anyOf properties. While correct documentation is shown for valid YAML, the user sees misleading information while in the process of writing YAML. This is problematic for cases where a user relies on auto-completion and hover documentation to discover available properties.

Schema

{
  "title": "TheRoot",
  "description": "Root object",
  "type": "object",
  "properties": {
    "child": {
      "title": "Child",
      "env_names": [
        "child"
      ],
      "anyOf": [
        {
          "$ref": "#/definitions/FirstChoice"
        },
        {
          "$ref": "#/definitions/SecondChoice"
        }
      ]
    }
  },
  "required": [
    "child"
  ],
  "additionalProperties": false,
  "definitions": {
    "FirstChoice": {
      "title": "FirstChoice",
      "description": "The first choice",
      "type": "object",
      "properties": {
        "choice": {
          "title": "Choice",
          "default": "first",
          "enum": [
            "first"
          ],
          "type": "string"
        },
        "property_a": {
          "title": "Property A",
          "type": "string"
        }
      },
      "required": [
        "property_a"
      ]
    },
    "SecondChoice": {
      "title": "SecondChoice",
      "description": "The second choice",
      "type": "object",
      "properties": {
        "choice": {
          "title": "Choice",
          "default": "second",
          "enum": [
            "second"
          ],
          "type": "string"
        },
        "property_b": {
          "title": "Property B",
          "type": "string"
        }
      },
      "required": [
        "property_b"
      ]
    }
  }
}

Side note: This schema was generated using Pydantic with the below Pydantic model.

from typing import Union
from pydantic import Field, BaseSettings, BaseModel
from typing_extensions import Literal


class FirstChoice(BaseModel):
    """The first choice"""

    choice: Literal["first"] = Field(default="first")

    property_a: str = Field()


class SecondChoice(BaseModel):
    """The second choice"""

    choice: Literal["second"] = Field(default="second")

    property_b: str = Field()


Choices = Union['FirstChoice', 'SecondChoice']


class TheRoot(BaseSettings):
    """Root object"""

    child: Choices = Field()

Example YAML

This is intentionally incomplete, assume the user is currently in the process of writing it.

# yaml-language-server: $schema=bugreport.schema.json

child: 

Expected Behavior

When hovering child, hover documentation indicates that there are two choices, FirstChoice and SecondChoice.

Current Behavior

When hovering child, documentation for FirstChoice is shown, without any indication that there are other possibilities.

Note that the hover documentation works as expected as soon as it is clear which anyOf choice applies. For example, the following YAML will show documentation for SecondChoice when hovering child:

# yaml-language-server: $schema=bugreport.schema.json

child: 
  choice: second

Steps to Reproduce

  1. Store the above schema and example YAML in files, make sure the schema reference matches the schema file name
  2. Hover child

Environment

  • Windows
  • VS Code 1.68.0
  • YAML Extension 1.8.0

thedmi avatar Jun 15 '22 07:06 thedmi