From 59e6634eae79a75a7169b4a08433e01fda594d0c Mon Sep 17 00:00:00 2001 From: tdakkota Date: Wed, 6 Nov 2024 13:16:44 +0300 Subject: [PATCH] fix(gen): properly handle recursive schemas --- internal/gen/schema.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/gen/schema.go b/internal/gen/schema.go index e3342e4..3819d22 100644 --- a/internal/gen/schema.go +++ b/internal/gen/schema.go @@ -51,7 +51,18 @@ func mkEnumOgenSchema(ed protoreflect.EnumDescriptor) *ogen.Schema { } func (g *Generator) mkSchema(msg *protogen.Message) error { - s := ogen.NewSchema().SetType("object") + name := descriptorName(msg.Desc) + if g.hasDescriptorName(name) { + return nil + } + g.setDescriptorName(name) + + s := ogen.NewSchema() + { + g.spec.AddSchema(name, s) + g.messages[name] = msg + } + s.SetType("object") if err := g.mkJSONFields(s, msg.Fields); err != nil { return err @@ -62,22 +73,13 @@ func (g *Generator) mkSchema(msg *protogen.Message) error { continue } - if field.Message != nil { - name := descriptorName(field.Desc) - if g.hasDescriptorName(name) { - s.SetRef(descriptorRef(field.Message.Desc)) - - continue - } - - g.setDescriptorName(name) - - if err := g.mkSchema(field.Message); err != nil { + if fmsg := field.Message; fmsg != nil { + if err := g.mkSchema(fmsg); err != nil { return err } } - if field.Enum != nil { - g.mkEnum(field.Enum) + if enum := field.Enum; enum != nil { + g.mkEnum(enum) } } @@ -91,9 +93,6 @@ func (g *Generator) mkSchema(msg *protogen.Message) error { g.mkEnum(e) } - name := descriptorName(msg.Desc) - g.spec.AddSchema(name, s) - g.messages[name] = msg return nil }