diff --git a/generate/property.go b/generate/property.go index e314c096a8..9e039cd8fc 100644 --- a/generate/property.go +++ b/generate/property.go @@ -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"` diff --git a/generate/templates/schema-property.template b/generate/templates/schema-property.template index 2d8efecfc4..428a8bef00 100644 --- a/generate/templates/schema-property.template +++ b/generate/templates/schema-property.template @@ -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}} ] }