Skip to content

Commit

Permalink
Merge pull request #147 from ogen-go/fix/nested-item
Browse files Browse the repository at this point in the history
fix: nested item
  • Loading branch information
tdakkota authored Apr 8, 2024
2 parents b2dcbd0 + 8b38c00 commit 6e4b532
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions example/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ message GetItemsResponse {
ItemType type = 2 [(google.api.field_behavior) = REQUIRED];
string name = 3 [(google.api.field_behavior) = REQUIRED];
google.protobuf.Timestamp created_at = 4 [(google.api.field_behavior) = REQUIRED];
repeated NestedItem nested_item = 5;
}

repeated Item items = 1 [(google.api.field_behavior) = REQUIRED];
Expand Down
6 changes: 6 additions & 0 deletions example/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ components:
createdAt:
type: string
format: date-time
nestedItem:
type: array
items:
$ref: '#/components/schemas/GetItemsResponse.NestedItem'
required:
- id
- type
Expand Down Expand Up @@ -202,6 +206,8 @@ components:
enum:
- ITEM_TYPE_UNSPECIFIED
- ITEM_TYPE_BASIC
NestedEnum:
type: object
NestedEnum.ItemType:
type: string
enum:
Expand Down
41 changes: 35 additions & 6 deletions internal/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func NewGenerator(files []*protogen.File, opts ...GeneratorOption) (*Generator,
}
}
}
}

for _, f := range files {
if !f.Generate {
continue
}

for _, m := range f.Messages {
name := descriptorName(m.Desc)
Expand All @@ -93,9 +99,10 @@ func NewGenerator(files []*protogen.File, opts ...GeneratorOption) (*Generator,

// Generator instance.
type Generator struct {
spec *ogen.Spec
indent int
requests map[string]struct{}
spec *ogen.Spec
indent int
requests map[string]struct{}
descriptorNames map[string]struct{}
}

// YAML returns OpenAPI specification bytes.
Expand All @@ -121,6 +128,7 @@ func (g *Generator) init() {
g.spec = ogen.NewSpec()
g.spec.Init()
g.requests = make(map[string]struct{})
g.descriptorNames = make(map[string]struct{})
}

func (g *Generator) mkMethod(rule HTTPRule, m *protogen.Method) (string, *ogen.Operation, error) {
Expand All @@ -143,7 +151,7 @@ func (g *Generator) mkMethod(rule HTTPRule, m *protogen.Method) (string, *ogen.O

func (g *Generator) mkInput(rule HTTPRule, m *protogen.Method, op *ogen.Operation) (string, error) {
name := descriptorName(m.Input.Desc)
g.requests[name] = struct{}{}
g.setRequest(name)

var (
fields = collectFields(m.Input)
Expand Down Expand Up @@ -403,8 +411,29 @@ func (g *Generator) hasSchema(s string) bool {
return ok
}

func (g *Generator) hasRequest(r string) bool {
_, ok := g.requests[r]
func (g *Generator) setRequest(s string) {
if g.hasRequest(s) {
return
}

g.requests[s] = struct{}{}
}

func (g *Generator) hasRequest(s string) bool {
_, ok := g.requests[s]
return ok
}

func (g *Generator) setDescriptorName(s string) {
if g.hasDescriptorName(s) {
return
}

g.descriptorNames[s] = struct{}{}
}

func (g *Generator) hasDescriptorName(s string) bool {
_, ok := g.descriptorNames[s]
return ok
}

Expand Down
9 changes: 9 additions & 0 deletions internal/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func (g *Generator) mkSchema(msg *protogen.Message) error {
}

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 {
return err
}
Expand Down

0 comments on commit 6e4b532

Please sign in to comment.