From da6de4ce90065927b0adc0db34145c8754c253cc Mon Sep 17 00:00:00 2001 From: Mathew Tonkin Henwood <7479946+mathewTH@users.noreply.github.com> Date: Thu, 5 Jan 2023 18:06:39 +1300 Subject: [PATCH] Add FieldNameTag config Allows field names to be based on a different tag. Json is still the default tag so this is not a breaking change. https://github.com/invopop/jsonschema/discussions/28#discussioncomment-4387240 --- reflect.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/reflect.go b/reflect.go index 6ebc6be..399c2ff 100644 --- a/reflect.go +++ b/reflect.go @@ -183,6 +183,9 @@ type Reflector struct { // root as opposed to a definition with a reference. ExpandedStruct bool + // FieldNameTag will change the tag used to get field names. json tags are used by default. + FieldNameTag string + // IgnoredTypes defines a slice of types that should be ignored in the schema, // switching to just allowing additional properties instead. IgnoredTypes []interface{} @@ -989,7 +992,11 @@ func ignoredByJSONSchemaTags(tags []string) bool { } func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool, bool) { - jsonTagString, _ := f.Tag.Lookup("json") + tagKey := r.FieldNameTag + if tagKey == "" { + tagKey = "json" + } + jsonTagString := f.Tag.Get(tagKey) jsonTags := strings.Split(jsonTagString, ",") if ignoredByJSONTags(jsonTags) {