Skip to content

Commit

Permalink
feat: add support nested enum as query
Browse files Browse the repository at this point in the history
Signed-off-by: Sasha Melentyev <[email protected]>
  • Loading branch information
sashamelentyev committed Apr 2, 2024
1 parent 21010c2 commit 92c29d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions example/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ message Item {
message GetItemsRequest {
int32 limit = 1 [(google.api.field_behavior) = REQUIRED];
int32 offset = 2 [(google.api.field_behavior) = REQUIRED];
NestedEnum.ItemType nested_type = 3 [(google.api.field_behavior) = REQUIRED];
}

message GetItemsResponse {
Expand Down
5 changes: 5 additions & 0 deletions example/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ paths:
schema:
type: integer
format: int32
- name: nestedType
in: query
required: true
schema:
$ref: '#/components/schemas/NestedEnum.ItemType'
- name: offset
in: query
required: true
Expand Down
8 changes: 5 additions & 3 deletions internal/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func NewGenerator(files []*protogen.File, opts ...GeneratorOption) (*Generator,
}

for _, e := range f.Enums {
if err := g.mkEnum(e); err != nil {
return nil, err
}
g.mkEnum(e)
}

for _, s := range f.Services {
Expand Down Expand Up @@ -325,6 +323,10 @@ func (g *Generator) mkQueryParameters(op *ogen.Operation, fields map[string]*pro
delete(seen, msg)
continue
}
case protoreflect.EnumKind:
name := descriptorName(fd.Enum())

Check failure on line 327 in internal/gen/generator.go

View workflow job for this annotation

GitHub Actions / lint / run

shadow: declaration of "name" shadows declaration at line 301 (govet)
s := mkEnumOgenSchema(fd.Enum())
g.spec.AddSchema(name, s)
case protoreflect.GroupKind:
return errors.Errorf("unsupported kind: %s", kind)
}
Expand Down
25 changes: 13 additions & 12 deletions internal/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ import (
"github.com/ogen-go/ogen"
)

func (g *Generator) mkEnum(e *protogen.Enum) error {
s := &ogen.Schema{
Type: "string",
Enum: enum(e.Desc),
}
func (g *Generator) mkEnum(e *protogen.Enum) {
s := mkEnumOgenSchema(e.Desc)

name := descriptorName(e.Desc)
g.spec.AddSchema(name, s)
return nil
}

func enum(ed protoreflect.EnumDescriptor) []json.RawMessage {
Expand All @@ -41,6 +37,15 @@ func enum(ed protoreflect.EnumDescriptor) []json.RawMessage {
return enum
}

func mkEnumOgenSchema(ed protoreflect.EnumDescriptor) *ogen.Schema {
s := &ogen.Schema{
Type: "string",
Enum: enum(ed),
}

return s
}

func (g *Generator) mkSchema(msg *protogen.Message) error {
s := ogen.NewSchema().SetType("object")

Expand All @@ -59,9 +64,7 @@ func (g *Generator) mkSchema(msg *protogen.Message) error {
}
}
if field.Enum != nil {
if err := g.mkEnum(field.Enum); err != nil {
return err
}
g.mkEnum(field.Enum)
}
}

Expand All @@ -72,9 +75,7 @@ func (g *Generator) mkSchema(msg *protogen.Message) error {
}

for _, e := range msg.Enums {
if err := g.mkEnum(e); err != nil {
return err
}
g.mkEnum(e)
}

name := descriptorName(msg.Desc)
Expand Down

0 comments on commit 92c29d3

Please sign in to comment.