Skip to content

Commit

Permalink
feat: add ability to use fully qualified names for message names
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Jan 22, 2025
1 parent ef10cf0 commit 559095b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,11 @@ func WithServices(serviceNames []protoreflect.FullName) Option {
return nil
}
}

// FullyQualifiedMessageNames decides if you want to use the full path in message names.
func FullyQualifiedMessageNames(enabled bool) Option {
return func(g *generator) error {
g.options.FullyQualifiedMessageNames = enabled
return nil
}
}
5 changes: 5 additions & 0 deletions internal/converter/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Options struct {
TrimUnusedTypes bool
// WithProtoAnnotations will add some protobuf annotations for descriptions
WithProtoAnnotations bool
// FullyQualifiedMessageNames uses the full path for message types: {pkg}.{name} instead of just the name. This
// is helpful if you are mixing types from multiple services.
FullyQualifiedMessageNames bool
// Services filters which services will be used for generating OpenAPI spec.
Services []protoreflect.FullName

Expand Down Expand Up @@ -88,6 +91,8 @@ func FromString(s string) (Options, error) {
opts.WithProtoAnnotations = true
case param == "trim-unused-types":
opts.TrimUnusedTypes = true
case param == "fully-qualified-message-names":
opts.FullyQualifiedMessageNames = true
case strings.HasPrefix(param, "content-types="):
for _, contentType := range strings.Split(param[14:], ";") {
contentType = strings.TrimSpace(contentType)
Expand Down
6 changes: 5 additions & 1 deletion internal/converter/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ func MessageToSchema(opts options.Options, tt protoreflect.MessageDescriptor) (s
}
return wk.ID, wk.Schema
}
title := string(tt.Name())
if opts.FullyQualifiedMessageNames {
title = string(tt.FullName())
}
s := &base.Schema{
Title: string(tt.Name()),
Title: title,
Description: util.FormatComments(tt.ParentFile().SourceLocations().ByDescriptor(tt)),
Type: []string{"object"},
AdditionalProperties: &base.DynamicValue[*base.SchemaProxy, bool]{N: 1, B: false},
Expand Down

0 comments on commit 559095b

Please sign in to comment.