Skip to content

Commit

Permalink
feat(schema): Add the ability to create items using pattern propertie…
Browse files Browse the repository at this point in the history
…s rather than normal references
  • Loading branch information
jpinkney-aws authored and rubenfonseca committed Apr 26, 2022
1 parent 28db940 commit 7b60160
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions generate/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ type Property struct {
// This will allow a single array to contain multiple item types rather than one type per array instance
InclusiveItemTypes []string `json:"InclusiveItemTypes"`

// InclusiveItemPattern - Indicates if item types should be created with a pattern property rather than
// a regular reference. This is useful in cases where you have a List of objects where the key is arbitrary
// but the values need to be referenced.
InclusiveItemPattern bool `json:"InclusiveItemPattern"`

// Types - if a property can be different types, they will be listed here
PrimitiveTypes []string `json:"PrimitiveTypes"`
PrimitiveItemTypes []string `json:"PrimitiveItemTypes"`
Expand Down
14 changes: 13 additions & 1 deletion generate/templates/schema-property.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@
"type": "{{convertToJSONType $primitiveType}}"
}{{if (or $.Property.InclusiveItemTypes (call $rc))}},{{end}}
{{end}}
{{$length := len .Property.InclusiveItemTypes}}{{$rc := counter $length}}{{range $index, $itemType := .Property.InclusiveItemTypes}}
{{ if .Property.InclusiveItemPattern }}
{{$length := len .Property.InclusiveItemTypes}}{{$rc := counter $length}}{{range $index, $itemType := .Property.InclusiveItemTypes}}
{
"patternProperties": {
"^[a-zA-Z0-9]+$": {
"$ref": "#/definitions/{{$.Parent}}.{{$itemType}}"
}
}{{if call $rc}},{{end}}
}
{{end}}
{{else}}
{{$length := len .Property.InclusiveItemTypes}}{{$rc := counter $length}}{{range $index, $itemType := .Property.InclusiveItemTypes}}
{
"$ref": "#/definitions/{{$.Parent}}.{{$itemType}}"
}{{if call $rc}},{{end}}
{{end}}
{{end}}
]
}
Expand Down

0 comments on commit 7b60160

Please sign in to comment.