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.12k stars 293 forks source link

open API exporter supports custom extensions #2638

Open gotwarlost opened 1 year ago

gotwarlost commented 1 year ago

Is your feature request related to a problem? Please describe.

I'm trying to create openAPI schemas from cue schemas and this works well. One limitation of the exporter is that there is no way to add extensions to the generated schema.

See: https://swagger.io/docs/specification/openapi-extensions/

The specific use-case that I'm struggling with is to declare an open struct in a Kubernetes CRD schema. Kubernetes will not allow an empty object spec unless it also has the extension x-kubernetes-preserve-unknown-fields set to true.

Describe the solution you'd like

I'd like a way to tag a field in a cue schema such that the schema generator can write the extension when generating the type definition in openAPI.

Seems to me that this is expressed best as a cue attribute of the form @extension("x-name=value") which the openAPI exporter respects and emits in its output.

Describe alternatives you've considered

There is no workaround for this at the cue level. I have a horrible sed hack that looks like this:

sed 's/additionalProperties: {}/additionalProperties: true, "x-kubernetes-preserve-unknown-fields": true/g'

Additional context

I think the above is self-explanatory. If not, please let me know.

verdverm commented 1 year ago

I'd posit that a better CUE attribute would be @openapi(...) which could support a variety of options, @extension sounds a bit too generic for people to look at and understand what it is for

@openapi(extension:"x-name=value",extension:x-foo:bar)

gotwarlost commented 1 year ago

works for me. Just need to get the functionality :)