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

(fix) remove internal comments from generated protoComments #3864

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
1 change: 1 addition & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,7 @@ func protoComments(reg *descriptor.Registry, file *descriptor.File, outers []str
// - trim every line only if that is the case
// - join by \n
comments = strings.ReplaceAll(comments, "\n ", "\n")
comments = removeInternalComments(comments)
}
if loc.TrailingComments != nil {
trailing := strings.TrimSpace(*loc.TrailingComments)
Expand Down
30 changes: 30 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10473,6 +10473,36 @@ func TestUpdatePaths(t *testing.T) {
}
}

// Test that enum values have internal comments removed
func TestEnumValueProtoComments(t *testing.T) {
reg := descriptor.NewRegistry()
name := "kind"
comments := "(-- this is a comment --)"

enum := &descriptor.Enum{
EnumDescriptorProto: &descriptorpb.EnumDescriptorProto{
Name: &name,
},
File: &descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
Name: new(string),
Package: new(string),
SourceCodeInfo: &descriptorpb.SourceCodeInfo{
Location: []*descriptorpb.SourceCodeInfo_Location{
&descriptorpb.SourceCodeInfo_Location{
LeadingComments: &comments,
},
},
},
},
},
}
comments = enumValueProtoComments(reg, enum)
if comments != "" {
t.Errorf("expected '', got '%v'", comments)
}
}
Comment on lines +10476 to +10504
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test function TestEnumValueProtoComments is well-structured and tests the removal of internal comments from enum values. However, it would be beneficial to include a case where comments are expected to remain to ensure that only internal comments are removed and other comments are left intact.

+ // Test that non-internal comments are not removed
+ func TestEnumValueNonInternalComments(t *testing.T) {
+     // ... setup similar to TestEnumValueProtoComments ...
+     comments := "This is a public comment."
+     // ... create enum with comments ...
+     comments = enumValueProtoComments(reg, enum)
+     if comments == "" {
+         t.Errorf("expected 'This is a public comment.', got '%v'", comments)
+     }
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Test that enum values have internal comments removed
func TestEnumValueProtoComments(t *testing.T) {
reg := descriptor.NewRegistry()
name := "kind"
comments := "(-- this is a comment --)"
enum := &descriptor.Enum{
EnumDescriptorProto: &descriptorpb.EnumDescriptorProto{
Name: &name,
},
File: &descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
Name: new(string),
Package: new(string),
SourceCodeInfo: &descriptorpb.SourceCodeInfo{
Location: []*descriptorpb.SourceCodeInfo_Location{
&descriptorpb.SourceCodeInfo_Location{
LeadingComments: &comments,
},
},
},
},
},
}
comments = enumValueProtoComments(reg, enum)
if comments != "" {
t.Errorf("expected '', got '%v'", comments)
}
}
// Test that enum values have internal comments removed
func TestEnumValueProtoComments(t *testing.T) {
reg := descriptor.NewRegistry()
name := "kind"
comments := "(-- this is a comment --)"
enum := &descriptor.Enum{
EnumDescriptorProto: &descriptorpb.EnumDescriptorProto{
Name: &name,
},
File: &descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
Name: new(string),
Package: new(string),
SourceCodeInfo: &descriptorpb.SourceCodeInfo{
Location: []*descriptorpb.SourceCodeInfo_Location{
&descriptorpb.SourceCodeInfo_Location{
LeadingComments: &comments,
},
},
},
},
},
}
comments = enumValueProtoComments(reg, enum)
if comments != "" {
t.Errorf("expected '', got '%v'", comments)
}
}
// Test that non-internal comments are not removed
func TestEnumValueNonInternalComments(t *testing.T) {
// ... setup similar to TestEnumValueProtoComments ...
comments := "This is a public comment."
// ... create enum with comments ...
comments = enumValueProtoComments(reg, enum)
if comments == "" {
t.Errorf("expected 'This is a public comment.', got '%v'", comments)
}
}


func MustMarshal(v interface{}) []byte {
b, err := json.Marshal(v)
if err != nil {
Expand Down
Loading