Skip to content

Commit

Permalink
fix(gen): properly handle recursive schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Nov 6, 2024
1 parent d1a6df8 commit 59e6634
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions internal/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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
}

Expand Down

0 comments on commit 59e6634

Please sign in to comment.