Skip to content

Commit

Permalink
feat: Respect json_name option
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Nov 8, 2024
1 parent 3cf3558 commit e08295e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/modules/1_middleend_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func buildFromMessage(pluginOptions *proto.PluginOptions, message pgs.Message, m
// continue
//}

propName := toPropertyName(field.Name())
propName := toPropertyName(field)

fieldSchema := &jsonschema.Schema{Ref: toRefId(field)}
if !pluginOptions.GetMandatoryNullable() && (field.InRealOneOf() || field.HasOptionalKeyword()) || proto.GetFieldOptions(field).GetNullable() {
fieldSchema = &jsonschema.Schema{OneOf: []*jsonschema.Schema{
Expand All @@ -46,7 +47,7 @@ func buildFromMessage(pluginOptions *proto.PluginOptions, message pgs.Message, m
// Convert Protobuf OneOfs to JSONSchema keywords
for _, oneOf := range message.OneOfs() {
propertyNames := lo.Map[pgs.Field, string](oneOf.Fields(), func(item pgs.Field, _ int) string {
return toPropertyName(item.Name())
return toPropertyName(item)
})
oneOfSchemas := lo.Map[string, *jsonschema.Schema](propertyNames, func(item string, _ int) *jsonschema.Schema {
return &jsonschema.Schema{Required: []string{item}}
Expand Down Expand Up @@ -419,8 +420,12 @@ func isScalarType(field pgs.Field) bool {
return false
}

func toPropertyName(name pgs.Name) string {
return name.String()
func toPropertyName(field pgs.Field) string {
if field.Descriptor().JsonName != nil {
return field.Descriptor().GetJsonName()
}

return field.Name().String()
}

type FqdnResolver interface {
Expand Down

0 comments on commit e08295e

Please sign in to comment.