From 7b426eb094efb2993c06b0d264e18f2299939673 Mon Sep 17 00:00:00 2001 From: dieg0code Date: Fri, 6 Dec 2024 06:43:12 -0300 Subject: [PATCH] Added support for enum tag in schema generation --- jsonschema/json.go | 11 +++++++++++ test.mp3 | 1 + 2 files changed, 12 insertions(+) create mode 100644 test.mp3 diff --git a/jsonschema/json.go b/jsonschema/json.go index bcb253fae..3036b99ab 100644 --- a/jsonschema/json.go +++ b/jsonschema/json.go @@ -131,6 +131,12 @@ func reflectSchemaObject(t reflect.Type) (*Definition, error) { required = false } + enumTag := field.Tag.Get("enum") + var enumValues []string + if enumTag != "" { + enumValues = strings.Split(enumTag, ",") + } + item, err := reflectSchema(field.Type) if err != nil { return nil, err @@ -139,6 +145,11 @@ func reflectSchemaObject(t reflect.Type) (*Definition, error) { if description != "" { item.Description = description } + + if len(enumValues) > 0 { + item.Enum = enumValues + } + properties[jsonTag] = *item if s := field.Tag.Get("required"); s != "" { diff --git a/test.mp3 b/test.mp3 new file mode 100644 index 000000000..b6fc4c620 --- /dev/null +++ b/test.mp3 @@ -0,0 +1 @@ +hello \ No newline at end of file