cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.16k stars 297 forks source link

internal/eval: cannot make field optional if name is interpolated #1222

Open rycee opened 3 years ago

rycee commented 3 years ago

What version of CUE are you using (cue version)?

$ cue version
cue version 0.4.0 linux/amd64

Does this issue reproduce with the latest release?

Yeah

What did you do?

I want to export an OpenAPI file with an optional field whose name is created by interpolation. Specifically, I would like to accomplish something like in this reproducer:

exec cue def test.cue -e '{#Out: #Example}' -o openapi:-
cmp stdout stdout.golden

-- test.cue --
x: "hello"

#Example: {
    "\(x)"?: int
}
-- stdout.golden --
{
    "openapi": "3.0.0",
    "info": {
        "title": "Generated by cue.",
        "version": "no version"
    },
    "paths": {},
    "components": {
        "schemas": {
            "Example": {
                "type": "object",
                "properties": {
                    "hello": {
                        "type": "integer"
                    }
                }
            },
            "Out": {
                "$ref": "#/components/schemas/Example"
            }
        }
    }
}

What did you expect to see?

The output of stdout.golden above.

What did you see instead?

The output claims that "hello" is a required field:

{
    "openapi": "3.0.0",
    "info": {
        "title": "Generated by cue.",
        "version": "no version"
    },
    "paths": {},
    "components": {
        "schemas": {
            "Example": {
                "type": "object",
                "required": [
                    "hello"
                ],
                "properties": {
                    "hello": {
                        "type": "integer"
                    }
                }
            },
            "Out": {
                "$ref": "#/components/schemas/Example"
            }
        }
    }
}

Changing test.cue to just contain

#Example: {
    "hello"?: int
}

produces the output I expected.

myitcv commented 3 years ago

@rycee nice catch, thank you! This broke all the way back in 7f52c107b61980629c4c0d6751386a8ce360ddb9.

tmm1 commented 2 years ago

This is a blocker for something I'm trying to do, so I'd like to try fixing it. Seems like it's a regression in the new runtime introduced with v0.3?

If someone can give me a pointer of where to start looking in the code base that would be appreciated!