From 06eb48f6b9bf880aaf672b19c716b4e992105a3e Mon Sep 17 00:00:00 2001 From: Hanjiang Yu Date: Tue, 17 Jan 2023 14:46:23 +0800 Subject: [PATCH] Skip unexported struct fields in swaggergen Go JSON serialization only handles exported struct fields. --- generate/swaggergen/g_docs.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index 23f16ac..292a46d 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -37,9 +37,10 @@ import ( bu "github.com/beego/bee/v2/utils" - beeLogger "github.com/beego/bee/v2/logger" "github.com/beego/beego/v2/core/utils" "github.com/beego/beego/v2/server/web/swagger" + + beeLogger "github.com/beego/bee/v2/logger" ) const ( @@ -1092,6 +1093,17 @@ func parseStruct(imports []*ast.ImportSpec, st *ast.StructType, k string, m *swa if st.Fields.List != nil { m.Properties = make(map[string]swagger.Propertie) for _, field := range st.Fields.List { + // skip processing if field is not exported + var exported = field.Names == nil + for _, ident := range field.Names { + if ident.IsExported() { + exported = true + } + } + if !exported { + continue + } + isSlice, realType, sType := typeAnalyser(field) if (isSlice && isBasicType(realType)) || sType == astTypeObject { if len(strings.Split(realType, " ")) > 1 {