Git Product home page Git Product logo

Comments (9)

aeschli avatar aeschli commented on July 23, 2024 1

It shows if there's no value at all, but as soon as there's a value, the second variant is gone.

image

Sorry, need to be fixed in code.

from vscode-json-languageservice.

aeschli avatar aeschli commented on July 23, 2024

Because of the unfulfilled "required": [ "type", "prop1.2"], the first variant gets more weight and is preferred.

from vscode-json-languageservice.

stanislawosinski avatar stanislawosinski commented on July 23, 2024

Thanks for the explanation. I wouldn't mind the sorting order depending on the preference determined by the number of missing required properties, but the fact that the "component1.2" suggestion does not appear at all effectively "hides" this variant from the user. Is there any workaround (apart from changing the schema) I could use to have both variants present in the autocomplete list?

from vscode-json-languageservice.

stanislawosinski avatar stanislawosinski commented on July 23, 2024

All right, good to know. Looking forward to the fix then!

from vscode-json-languageservice.

abdalla-rko avatar abdalla-rko commented on July 23, 2024

I still have the same problem. Is there still no fix or way to fix this?

from vscode-json-languageservice.

stanislawosinski avatar stanislawosinski commented on July 23, 2024

One workaround I've found is to add an explicit list of the allowed property (the type property in my example) values to the polymorphic oneOf definition:

"component1": {
    // workaround start
    "properties": {
        "type": {
            "type": "String",
            "enum": [
                "component1.1",
                "component1.2"
            ]
        }
    },
    "required": [ "type" ],
    // workaround end
    "oneOf": [
        {
            "$ref": "#/definitions/component1.1"
        },
        {
            "$ref": "#/definitions/component1.2"
        }
    ]
}
The complete working Monaco playground example with workaround
var jsonCode = `{
  "component": {
      "type": ""
  }
}`;
var modelUri = monaco.Uri.parse("a://b/foo.json"); // a made up unique URI for our model
var model = monaco.editor.createModel(jsonCode, "json", modelUri);

// configure the JSON language support with schemas and schema associations
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
    validate: true,
    schemas: [{
        uri: "http://myserver/foo-schema.json", // id of the first schema
        fileMatch: [modelUri.toString()], // associate with our model
        schema: {
            "$schema": "http://json-schema.org/draft-07/schema#",
            "type": "object",
            "definitions": {
                "component1.1": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "component1.1"
                            ]
                        },
                        "prop1.1": {
                            "type": "integer"
                        }
                    },
                    "required": [
                        "type"
                    ]
                },
                "component1.2": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "component1.2"
                            ]
                        },
                        "prop1.2": {
                            "type": "integer"
                        }
                    },
                    "required": [
                        "type",
                        "prop1.2"
                    ]
                },
                "component1": {
                    "properties": {
                        "type": {
                            "type": "String",
                            "enum": [
                                "component1.1",
                                "component1.2"
                            ]
                        }
                    },
                    "required": [ "type" ],
                    "oneOf": [
                        {
                            "$ref": "#/definitions/component1.1"
                        },
                        {
                            "$ref": "#/definitions/component1.2"
                        }
                    ]
                }
            },
            "properties": {
                "component": {
                    "$ref": "#/definitions/component1"
                }
            }
        }
    }]
});

monaco.editor.create(document.getElementById("container"), {
    model: model
});

The cleanest solution is probably to write a piece of JavaScript that would patch the schema at runtime, just before feeding the schema to Monaco.

from vscode-json-languageservice.

NotWearingPants avatar NotWearingPants commented on July 23, 2024

Similar problem but not with enums:

Schema:

{
    "oneOf": [
        {
            "properties": {
                "action": { "type": "string", "pattern": "random" },
                "value": { "type": "integer" }
            },
            "required": ["value"]
        },
        {
            "properties": {
                "action": { "type": "string", "pattern": "argInteger" },
                "arg": { "type": "integer", "description": "integer" }
            },
            "required": ["arg"]
        },
        {
            "properties": {
                "action": { "type": "string", "pattern": "argString" },
                "arg": { "type": "string", "description": "string" }
            },
            "required": ["arg"]
        }
    ]
}

Document:

{
    "action": "argString",
}

Triggering completions under the action property gives only the arg or argInteger which autocompletes to "arg": 0, instead of "arg": "" which is to be expected.

This is because of the requireds that make the oneOf not match anything, but the arg of argInteger hides the arg of argString since they share a name.

from vscode-json-languageservice.

rahulbanerjee26 avatar rahulbanerjee26 commented on July 23, 2024

Any updates on this?

from vscode-json-languageservice.

jeremyfiel avatar jeremyfiel commented on July 23, 2024

@NotWearingPants resurrecting from the dead.. :)

The oneOf defined is invalid because you cannot validate the same properties with different type definitions in a oneOf.

You would need to define it with a nested oneOf of type, or you can use the type array declaration

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "thing": {
            "oneOf": [
                {
                    "properties": {
                        "action": {
                            "type": "string",
                            "pattern": "random"
                        },
                        "value": {
                            "type": "integer"
                        }
                    },
                    "required": [
                        "value"
                    ]
                },
                {
                    "properties": {
                        "action": {
                            "type": "string",
                            "pattern": "argInteger"
                        },
                        "arg": {
                            "oneOf": [
                                {
                                    "type": "integer"
                                },
                                {
                                    "type": "string"
                                }
                            ]
                        }
                    },
                    "required": [
                        "arg"
                    ]
                }
            ]
        }
    }
}

OR type array

{
                    "properties": {
                        "action": {
                            "type": "string",
                            "pattern": "argInteger"
                        },
                        "arg": {
                            "type": ["integer", "string"]
                        }
                    },
                    "required": [
                        "arg"
                    ]
                }

from vscode-json-languageservice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.