Skip to content

Commit 08b3544

Browse files
authored
Merge pull request #826 from grafana/add-open-attribute
[CUE->OpenAPI] Add Manual Open Attribute for CUE -> OpenAPI Generation
2 parents eda7037 + d8d80ca commit 08b3544

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

internal/ast/hints.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ const (
2727
// HintStringFormatDateTime hints refers to a string that should be formatted
2828
// as a datetime as defined by RFC 3339, section 5.6 (ex: 2017-07-21T17:32:28Z)
2929
HintStringFormatDateTime = "string_format_datetime"
30+
31+
// HintOpenStruct hints that the struct is to be considered open even if not type Any
32+
// when generating outputs which make explicit allowances for
33+
// arbitrary additional properties, such as OpenAPI/JSONSchema.
34+
HintOpenStruct = "open"
3035
)
3136

3237
const DiscriminatorCatchAll = "cog_discriminator_catch_all"

internal/jennies/jsonschema/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func (jenny Schema) formatStruct(typeDef ast.Type) Definition {
202202

203203
definition.Set("type", "object")
204204
definition.Set("additionalProperties", false)
205+
if typeDef.HasHint(ast.HintOpenStruct) {
206+
if val, _ := typeDef.Hints[ast.HintOpenStruct].(string); strings.ToLower(val)[0] == 't' {
207+
definition.Set("additionalProperties", map[string]any{})
208+
}
209+
}
205210

206211
properties := orderedmap.New[string, any]()
207212
var required []string

internal/simplecue/generator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ func (g *generator) declareNode(v cue.Value) (ast.Type, error) {
404404
}
405405

406406
def := ast.NewStruct(fields...)
407+
// Add the hints since we can't supply them in the constructor
408+
def.Hints = hints
407409
def.Default = defVal
408410

409411
return def, nil

0 commit comments

Comments
 (0)