From 7b6016076b6653a7c930b15a94262db16aa93b5a Mon Sep 17 00:00:00 2001 From: Joshua Pinkney Date: Tue, 26 Apr 2022 14:15:56 -0400 Subject: [PATCH] feat(schema): Add the ability to create items using pattern properties rather than normal references --- generate/property.go | 5 +++++ generate/templates/schema-property.template | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) 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}} ] }