Skip to content

Commit

Permalink
Fix crash when gnostic options aren't set
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Sep 3, 2024
1 parent 840d811 commit 7ce64e7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 82 deletions.
1 change: 1 addition & 0 deletions examples/basic.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ components:
type: string
title: name
additionalProperties: false
example: Ein
title: HelloRequest
additionalProperties: false
description: The request message containing the user's name.
Expand Down
33 changes: 9 additions & 24 deletions examples/basic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,17 @@ option java_package = "io.grpc.examples.helloworld";
// Field options (gnostic.openapi.v3.property): Schema message
option (gnostic.openapi.v3.document) = {
info: {
title: "Hello World";
version: "v2";
description: "This is a service which says hello to you!";
title: "Hello World"
version: "v2"
description: "This is a service which says hello to you!"
contact: {
name: "Ein";
url: "https://github.com/sudorandom/protoc-gen-connect-openapi";
email: "[email protected]";
name: "Ein"
url: "https://github.com/sudorandom/protoc-gen-connect-openapi"
email: "[email protected]"
}
license: {
name: "MIT License";
url: "https://github.com/sudorandom/protoc-gen-connect-openapi/blob/master/LICENSE";
}
}
components: {
security_schemes: {
additional_properties: [
{
name: "BasicAuth";
value: {
security_scheme: {
type: "http";
scheme: "basic";
}
}
}
]
name: "MIT License"
url: "https://github.com/sudorandom/protoc-gen-connect-openapi/blob/master/LICENSE"
}
}
};
Expand All @@ -63,7 +48,7 @@ service Greeter {
message HelloRequest {
string name = 1 [
(buf.validate.field).string = {
min_len: 3,
min_len: 3
max_len: 100
},
(gnostic.openapi.v3.property) = {
Expand Down
100 changes: 55 additions & 45 deletions internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,41 @@ func Convert(req *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRespons
return nil, err
}

newSpec := func() (v3.Document, error) {
return initializeDoc(v3.Document{}), nil
newSpec := func() (*v3.Document, error) {
model := &v3.Document{}
initializeDoc(model)
return model, nil
}
if opts.BaseOpenAPIPath != "" {
newSpec = func() (v3.Document, error) {
newSpec = func() (*v3.Document, error) {
base, err := os.ReadFile(opts.BaseOpenAPIPath)
if err != nil {
return v3.Document{}, err
return &v3.Document{}, err
}

document, err := libopenapi.NewDocument(base)
if err != nil {
return v3.Document{}, fmt.Errorf("unmarshalling base: %w", err)
return &v3.Document{}, fmt.Errorf("unmarshalling base: %w", err)
}
v3Document, errs := document.BuildV3Model()
if len(errs) > 0 {
var merr error
for _, err := range errs {
merr = errors.Join(merr, err)
}
return v3.Document{}, merr
return &v3.Document{}, merr
}
return initializeDoc(v3Document.Model), nil
model := &v3Document.Model
initializeDoc(model)
return model, nil
}
}

spec, err := newSpec()
if err != nil {
return nil, err
}
outFiles := map[string]v3.Document{}
outFiles := map[string]*v3.Document{}

for _, fileDesc := range req.GetProtoFile() {
if _, ok := genFiles[fileDesc.GetName()]; !ok {
Expand All @@ -192,7 +196,7 @@ func Convert(req *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRespons
spec.Info.Description = util.FormatComments(fd.SourceLocations().ByDescriptor(fd))
}

if err := appendToSpec(opts, &spec, fd); err != nil {
if err := appendToSpec(opts, spec, fd); err != nil {
return nil, err
}

Expand Down Expand Up @@ -228,7 +232,7 @@ func Convert(req *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRespons
}, nil
}

func specToFile(opts options.Options, spec v3.Document) (string, error) {
func specToFile(opts options.Options, spec *v3.Document) (string, error) {
switch opts.Format {
case "yaml":
return string(spec.RenderWithIndention(2)), nil
Expand All @@ -249,6 +253,8 @@ func appendToSpec(opts options.Options, spec *v3.Document, fd protoreflect.FileD
if err != nil {
return err
}
initializeDoc(spec)
initializeComponents(components)
for pair := components.Schemas.First(); pair != nil; pair = pair.Next() {
spec.Components.Schemas.Set(pair.Key(), pair.Value())
}
Expand Down Expand Up @@ -288,7 +294,8 @@ func appendToSpec(opts options.Options, spec *v3.Document, fd protoreflect.FileD
return nil
}

func initializeDoc(doc v3.Document) v3.Document {
func initializeDoc(doc *v3.Document) {
slog.Debug("initializeDoc")
if doc.Version == "" {
doc.Version = "3.1.0"
}
Expand All @@ -313,39 +320,6 @@ func initializeDoc(doc v3.Document) v3.Document {
if doc.Paths.Extensions == nil {
doc.Paths.Extensions = orderedmap.New[string, *yaml.Node]()
}
if doc.Components == nil {
doc.Components = &v3.Components{}
}
if doc.Components.Schemas == nil {
doc.Components.Schemas = orderedmap.New[string, *base.SchemaProxy]()
}
if doc.Components.Responses == nil {
doc.Components.Responses = orderedmap.New[string, *v3.Response]()
}
if doc.Components.Parameters == nil {
doc.Components.Parameters = orderedmap.New[string, *v3.Parameter]()
}
if doc.Components.Examples == nil {
doc.Components.Examples = orderedmap.New[string, *base.Example]()
}
if doc.Components.RequestBodies == nil {
doc.Components.RequestBodies = orderedmap.New[string, *v3.RequestBody]()
}
if doc.Components.Headers == nil {
doc.Components.Headers = orderedmap.New[string, *v3.Header]()
}
if doc.Components.SecuritySchemes == nil {
doc.Components.SecuritySchemes = orderedmap.New[string, *v3.SecurityScheme]()
}
if doc.Components.Links == nil {
doc.Components.Links = orderedmap.New[string, *v3.Link]()
}
if doc.Components.Callbacks == nil {
doc.Components.Callbacks = orderedmap.New[string, *v3.Callback]()
}
if doc.Components.Extensions == nil {
doc.Components.Extensions = orderedmap.New[string, *yaml.Node]()
}
if doc.Security == nil {
doc.Security = []*base.SecurityRequirement{}
}
Expand All @@ -365,5 +339,41 @@ func initializeDoc(doc v3.Document) v3.Document {
doc.Rolodex = &index.Rolodex{}
}

return doc
if doc.Components == nil {
doc.Components = &v3.Components{}
}
initializeComponents(doc.Components)
}

func initializeComponents(components *v3.Components) {
if components.Schemas == nil {
components.Schemas = orderedmap.New[string, *base.SchemaProxy]()
}
if components.Responses == nil {
components.Responses = orderedmap.New[string, *v3.Response]()
}
if components.Parameters == nil {
components.Parameters = orderedmap.New[string, *v3.Parameter]()
}
if components.Examples == nil {
components.Examples = orderedmap.New[string, *base.Example]()
}
if components.RequestBodies == nil {
components.RequestBodies = orderedmap.New[string, *v3.RequestBody]()
}
if components.Headers == nil {
components.Headers = orderedmap.New[string, *v3.Header]()
}
if components.SecuritySchemes == nil {
components.SecuritySchemes = orderedmap.New[string, *v3.SecurityScheme]()
}
if components.Links == nil {
components.Links = orderedmap.New[string, *v3.Link]()
}
if components.Callbacks == nil {
components.Callbacks = orderedmap.New[string, *v3.Callback]()
}
if components.Extensions == nil {
components.Extensions = orderedmap.New[string, *yaml.Node]()
}
}
Binary file modified internal/converter/fixtures/fileset.binpb
Binary file not shown.
26 changes: 13 additions & 13 deletions internal/converter/fixtures/gnostic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import "gnostic/openapi/v3/annotations.proto";

option (gnostic.openapi.v3.document) = {
info: {
title: "Title from annotation";
version: "Version from annotation";
description: "Description from annotation";
title: "Title from annotation"
version: "Version from annotation"
description: "Description from annotation"
contact: {
name: "Contact Name";
url: "https://github.com/sudorandom/protoc-gen-connect-openapi";
email: "[email protected]";
name: "Contact Name"
url: "https://github.com/sudorandom/protoc-gen-connect-openapi"
email: "[email protected]"
}
license: {
name: "MIT License";
url: "https://github.com/sudorandom/protoc-gen-connect-openapi/blob/master/LICENSE";
name: "MIT License"
url: "https://github.com/sudorandom/protoc-gen-connect-openapi/blob/master/LICENSE"
}
}
components: {
security_schemes: {
additional_properties: [
{
name: "BasicAuth";
name: "BasicAuth"
value: {
security_scheme: {
type: "http";
scheme: "basic";
type: "http"
scheme: "basic"
}
}
}
Expand All @@ -42,12 +42,12 @@ service Greeter {
rpc SayHello(HelloRequest) returns (HelloReply) {
option idempotency_level = NO_SIDE_EFFECTS;
option (gnostic.openapi.v3.operation) = {
deprecated: true,
deprecated: true
security: [
{
additional_properties: [
{
name: "BasicAuth";
name: "BasicAuth"
value: {
value: []
}
Expand Down

0 comments on commit 7ce64e7

Please sign in to comment.