Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options #85

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions internal/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"strings"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"

"github.com/go-faster/errors"

"github.com/ogen-go/ogen"

"github.com/ogen-go/protoc-gen-oas/options"
)

func (g *Generator) mkEnum(e *protogen.Enum) error {
Expand Down Expand Up @@ -109,43 +111,43 @@ func (g *Generator) mkFieldSchema(fd protoreflect.FieldDescriptor) (s *ogen.Sche

switch kind := fd.Kind(); kind {
case protoreflect.BoolKind:
return ogen.NewSchema().SetType("boolean").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("boolean")

case protoreflect.Int32Kind,
protoreflect.Sint32Kind,
protoreflect.Sfixed32Kind:
return ogen.NewSchema().SetType("integer").SetFormat("int32").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("integer").SetFormat("int32")

case protoreflect.Uint32Kind,
protoreflect.Fixed32Kind:
return ogen.NewSchema().SetType("integer").SetFormat("uint32").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("integer").SetFormat("uint32")

case protoreflect.Int64Kind,
protoreflect.Sint64Kind,
protoreflect.Sfixed64Kind:
return ogen.NewSchema().SetType("integer").SetFormat("int64").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("integer").SetFormat("int64")

case protoreflect.Uint64Kind,
protoreflect.Fixed64Kind:
return ogen.NewSchema().SetType("integer").SetFormat("uint64").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("integer").SetFormat("uint64")

case protoreflect.FloatKind:
return ogen.NewSchema().SetType("number").SetFormat("float").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("number").SetFormat("float")
case protoreflect.DoubleKind:
return ogen.NewSchema().SetType("number").SetFormat("double").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("number").SetFormat("double")

case protoreflect.StringKind:
return ogen.NewSchema().SetType("string").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("string")
case protoreflect.BytesKind:
// Go's protojson encodes binary data as base64 string.
//
// https://github.com/protocolbuffers/protobuf-go/blob/f221882bfb484564f1714ae05f197dea2c76898d/encoding/protojson/encode.go#L287-L288
//
// Do the same here.
return ogen.NewSchema().SetType("string").SetFormat("base64").SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetType("string").SetFormat("base64")

case protoreflect.EnumKind:
return ogen.NewSchema().SetRef(descriptorRef(fd.Enum())).SetDeprecated(isDeprecated(fd.Options())), nil
s = ogen.NewSchema().SetRef(descriptorRef(fd.Enum()))

case protoreflect.MessageKind:
msg := fd.Message()
Expand Down Expand Up @@ -173,15 +175,22 @@ func (g *Generator) mkFieldSchema(fd protoreflect.FieldDescriptor) (s *ogen.Sche
s.AdditionalProperties = &ogen.AdditionalProperties{
Schema: *elem,
}
return s, nil
} else {
// User-defined type.
s = ogen.NewSchema().SetRef(descriptorRef(msg))
}

// User-defined type.
return ogen.NewSchema().SetRef(descriptorRef(msg)).SetDeprecated(isDeprecated(fd.Options())), nil
}
default: // protoreflect.GroupKind
return nil, errors.Errorf("unsupported kind: %s", kind)
}

if fds, ok := fieldOptionSchema(fd.Options()); ok {
s.SetDescription(fds.Description).
SetMaximum(fds.Maximum).
SetExclusiveMaximum(fds.ExclusiveMaximum)
}

return s.SetDeprecated(isDeprecated(fd.Options())), nil
}

func (g *Generator) mkWellKnownPrimitive(msg protoreflect.MessageDescriptor) (s *ogen.Schema, ok bool, _ error) {
Expand Down Expand Up @@ -267,3 +276,9 @@ func isDeprecated(opts protoreflect.ProtoMessage) bool {
}
return false
}

func fieldOptionSchema(opts protoreflect.ProtoMessage) (s *options.Schema, ok bool) {
s, ok = proto.GetExtension(opts, options.E_Field).(*options.Schema)
ok = ok && s != nil
return s, ok
}
206 changes: 206 additions & 0 deletions options/openapi.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions options/openapi.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package protocgenoas.options;

option go_package = "github.com/ogen-go/protoc-gen-oas/options";

import "google/protobuf/descriptor.proto";

message Schema {
string summary = 1;
string description = 2;
optional int64 maximum = 3;
bool exclusive_maximum = 4;
}

extend google.protobuf.FieldOptions {
Schema field = 1042;
}