microsoft / vscode-json-languageservice

JSON language service extracted from VSCode to be reused, e.g in the Monaco editor.
Other
269 stars 113 forks source link

code completion breaks when anyOf defines `required` properties. #205

Open manoj-pillay-10gen opened 1 year ago

manoj-pillay-10gen commented 1 year ago

204 describes an issue with anyOf (and oneOf) error messaging. However, one way to solve this is to provide a schema-specific error message using the custom JSON property errorMessage as illustrated in this example.

However, the use of the required property (or the dependencies property) on anyOf subschemas will prevent monaco from surfacing it as a valid suggestion. In my example above, p1.shape can be fulfilled by both rectangle and square. However, the only autocomplete option that gets offered is rectangle

Screenshot 2023-09-08 at 12 25 21 PM

The reason this occurs is because the square subschema defines a required field side that is only available within that schema. It essentially ends up making square a hidden option for p1.shape critically breaking the expected completion.

PS: If side were to be removed from the required array in L62 of the example, completions start working as expected.

Screenshot 2023-09-08 at 2 19 18 PM

jeremyfiel commented 1 year ago

if you remove type: string it surfaces the error. This is a bug in the monaco implementation.

image

This one has got it right. https://json-schema.hyperjump.io

image

manoj-pillay-10gen commented 1 year ago

The problem highlighted in this github issue is that code completion does not work as expected and only offers one among many choices that may be available for the property defined using the oneOf/anyOf construct. I wonder if the issue is more with vscode-json-languageservice treatment of oneOf/anyOf rather than it being with monaco itself.